merged branch Tobion/cast-optimization (PR #8201)

This PR was submitted for the 2.3 branch but it was merged into the master branch instead (closes #8201).

Discussion
----------

Cast optimization

bc break: no
test pass: yes

I searched for some unneeded casts esp. for `$array[(string) $stringOrInteger]`. But found only a few. When doing so I mainly fixed some phpdocs.

Commits
-------

2c41a31 [Serializer] fix phpdoc and add typehints to private methods
ebe7015 optimize some unneeded casts (esp. when casting something to string for array access)
This commit is contained in:
Fabien Potencier 2013-08-09 08:59:23 +02:00
commit 2f34c052b1
6 changed files with 46 additions and 51 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

@ -107,24 +107,16 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
}
/**
* Checks whether the serializer can encode to given format
*
* @param string $format format name
*
* @return Boolean
*/
* {@inheritdoc}
*/
public function supportsEncoding($format)
{
return 'xml' === $format;
}
/**
* Checks whether the serializer can decode from given format
*
* @param string $format format name
*
* @return Boolean
*/
* {@inheritdoc}
*/
public function supportsDecoding($format)
{
return 'xml' === $format;
@ -150,12 +142,12 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
}
/**
* @param DOMNode $node
* @param string $val
* @param \DOMNode $node
* @param string $val
*
* @return Boolean
*/
final protected function appendXMLString($node, $val)
final protected function appendXMLString(\DOMNode $node, $val)
{
if (strlen($val) > 0) {
$frag = $this->dom->createDocumentFragment();
@ -169,12 +161,12 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
}
/**
* @param DOMNode $node
* @param string $val
* @param \DOMNode $node
* @param string $val
*
* @return Boolean
*/
final protected function appendText($node, $val)
final protected function appendText(\DOMNode $node, $val)
{
$nodeText = $this->dom->createTextNode($val);
$node->appendChild($nodeText);
@ -183,12 +175,12 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
}
/**
* @param DOMNode $node
* @param string $val
* @param \DOMNode $node
* @param string $val
*
* @return Boolean
*/
final protected function appendCData($node, $val)
final protected function appendCData(\DOMNode $node, $val)
{
$nodeText = $this->dom->createCDATASection($val);
$node->appendChild($nodeText);
@ -197,12 +189,12 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
}
/**
* @param DOMNode $node
* @param DOMDocumentFragment $fragment
* @param \DOMNode $node
* @param \DOMDocumentFragment $fragment
*
* @return Boolean
*/
final protected function appendDocumentFragment($node, $fragment)
final protected function appendDocumentFragment(\DOMNode $node, $fragment)
{
if ($fragment instanceof \DOMDocumentFragment) {
$node->appendChild($fragment);
@ -230,11 +222,11 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
/**
* Parse the input SimpleXmlElement into an array.
*
* @param SimpleXmlElement $node xml to parse
* @param \SimpleXmlElement $node xml to parse
*
* @return array
*/
private function parseXml($node)
private function parseXml(\SimpleXmlElement $node)
{
$data = array();
if ($node->attributes()) {
@ -258,9 +250,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;
@ -281,15 +273,15 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
/**
* Parse the data and convert it to DOMElements
*
* @param DOMNode $parentNode
* @param array|object $data data
* @param string $xmlRootNodeName
* @param \DOMNode $parentNode
* @param array|object $data
* @param string|null $xmlRootNodeName
*
* @return Boolean
*
* @throws UnexpectedValueException
*/
private function buildXml($parentNode, $data, $xmlRootNodeName = null)
private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
{
$append = true;
@ -349,14 +341,14 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
/**
* Selects the type of node to create and appends it to the parent.
*
* @param DOMNode $parentNode
* @param \DOMNode $parentNode
* @param array|object $data
* @param string $nodeName
* @param string $key
*
* @return Boolean
*/
private function appendNode($parentNode, $data, $nodeName, $key = null)
private function appendNode(\DOMNode $parentNode, $data, $nodeName, $key = null)
{
$node = $this->dom->createElement($nodeName);
if (null !== $key) {
@ -386,12 +378,12 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
/**
* Tests the value being passed and decide what sort of element to create
*
* @param DOMNode $node
* @param mixed $val
* @param \DOMNode $node
* @param mixed $val
*
* @return Boolean
*/
private function selectNodeType($node, $val)
private function selectNodeType(\DOMNode $node, $val)
{
if (is_array($val)) {
return $this->buildXml($node, $val);

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
*/