optimize some unneeded casts (esp. when casting something to string for array access)

This commit is contained in:
Tobias Schultze 2013-06-05 01:48:30 +02:00 committed by Fabien Potencier
parent 0f16673765
commit 49c4a79a1d
6 changed files with 19 additions and 16 deletions

View File

@ -1109,7 +1109,7 @@ EOF;
*
* @return Boolean
*/
private function hasReference($id, array $arguments, $deep = false, $visited = array())
private function hasReference($id, array $arguments, $deep = false, array $visited = array())
{
foreach ($arguments as $argument) {
if (is_array($argument)) {
@ -1117,14 +1117,15 @@ EOF;
return true;
}
} elseif ($argument instanceof Reference) {
if ($id === (string) $argument) {
$argumentId = (string) $argument;
if ($id === $argumentId) {
return true;
}
if ($deep && !isset($visited[(string) $argument])) {
$visited[(string) $argument] = true;
if ($deep && !isset($visited[$argumentId])) {
$visited[$argumentId] = true;
$service = $this->container->getDefinition((string) $argument);
$service = $this->container->getDefinition($argumentId);
$arguments = array_merge($service->getMethodCalls(), $service->getArguments(), $service->getProperties());
if ($this->hasReference($id, $arguments, $deep, $visited)) {

View File

@ -233,10 +233,11 @@ class XmlFileLoader extends FileLoader
if (false !== $nodes = $xml->xpath('//container:argument[@type="service"][not(@id)]|//container:property[@type="service"][not(@id)]')) {
foreach ($nodes as $node) {
// give it a unique name
$node['id'] = sprintf('%s_%d', md5($file), ++$count);
$id = sprintf('%s_%d', md5($file), ++$count);
$node['id'] = $id;
$definitions[(string) $node['id']] = array($node->service, $file, false);
$node->service['id'] = (string) $node['id'];
$definitions[$id] = array($node->service, $file, false);
$node->service['id'] = $id;
}
}
@ -244,10 +245,11 @@ class XmlFileLoader extends FileLoader
if (false !== $nodes = $xml->xpath('//container:services/container:service[not(@id)]')) {
foreach ($nodes as $node) {
// give it a unique name
$node['id'] = sprintf('%s_%d', md5($file), ++$count);
$id = sprintf('%s_%d', md5($file), ++$count);
$node['id'] = $id;
$definitions[(string) $node['id']] = array($node, $file, true);
$node->service['id'] = (string) $node['id'];
$definitions[$id] = array($node, $file, true);
$node->service['id'] = $id;
}
}

View File

@ -47,7 +47,7 @@ class Reference
*/
public function __toString()
{
return (string) $this->id;
return $this->id;
}
/**

View File

@ -258,9 +258,9 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
if ($key === 'item') {
if (isset($value['@key'])) {
if (isset($value['#'])) {
$data[(string) $value['@key']] = $value['#'];
$data[$value['@key']] = $value['#'];
} else {
$data[(string) $value['@key']] = $value;
$data[$value['@key']] = $value;
}
} else {
$data['item'][] = $value;

View File

@ -19,7 +19,7 @@ abstract class AbstractLoader implements LoaderInterface
* Contains all known namespaces indexed by their prefix
* @var array
*/
protected $namespaces;
protected $namespaces = array();
/**
* Adds a namespace alias.

View File

@ -180,7 +180,7 @@ class XmlFileLoader extends FileLoader
*
* @param string $file Path of file
*
* @return SimpleXMLElement
* @return \SimpleXMLElement
*
* @throws MappingException
*/