8000 merged branch Tobion/cast-optimization (PR #8201) · daifma/symfony@2f34c05 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f34c05

Browse files
committed
merged branch Tobion/cast-optimization (PR symfony#8201)
This PR was submitted for the 2.3 branch but it was merged into the master branch instead (closes symfony#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)
2 parents 0f16673 + 869fd2c commit 2f34c05

File tree

6 files changed

+46
-51
lines changed

6 files changed

+46
-51
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,22 +1109,23 @@ private function getDefinitionsFromArguments(array $arguments)
11091109
*
11101110
* @return Boolean
11111111
*/
1112-
private function hasReference($id, array $arguments, $deep = false, $visited = array())
1112+
private function hasReference($id, array $arguments, $deep = false, array $visited = array())
11131113
{
11141114
foreach ($arguments as $argument) {
11151115
if (is_array($argument)) {
11161116
if ($this->hasReference($id, $argument, $deep, $visited)) {
11171117
return true;
11181118
}
11191119
} elseif ($argument instanceof Reference) {
1120-
if ($id === (string) $argument) {
1120+
$argumentId = (string) $argument;
1121+
if ($id === $argumentId) {
11211122
return true;
11221123
}
11231124

1124-
if ($deep && !isset($visited[(string) $argument])) {
1125-
$visited[(string) $argument] = true;
1125+
if ($deep && !isset($visited[$argumentId])) {
1126+
$visited[$argumentId] = true;
11261127

1127-
$service = $this->container->getDefinition((string) $argument);
1128+
$service = $this->container->getDefinition($argumentId);
11281129
$arguments = array_merge($service->getMethodCalls(), $service->getArguments(), $service->getProperties());
11291130

11301131
if ($this->hasReference($id, $arguments, $deep, $visited)) {

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,23 @@ private function processAnonymousServices(SimpleXMLElement $xml, $file)
233233
if (false !== $nodes = $xml->xpath('//container:argument[@type="service"][not(@id)]|//container:property[@type="service"][not(@id)]')) {
234234
foreach ($nodes as $node) {
235235
// give it a unique name
236-
$node['id'] = sprintf('%s_%d', md5($file), ++$count);
236+
$id = sprintf('%s_%d', md5($file), ++$count);
237+
$node['id'] = $id;
237238

238-
$definitions[(string) $node['id']] = array($node->service, $file, false);
239-
$node->service['id'] = (string) $node['id'];
239+
$definitions[$id] = array($node->service, $file, false);
240+
$node->service['id'] = $id;
240241
}
241242
}
242243

243244
// anonymous services "in the wild"
244245
if (false !== $nodes = $xml->xpath('//container:services/container:service[not(@id)]')) {
245246
foreach ($nodes as $node) {
246247
// give it a unique name
247-
$node['id'] = sprintf('%s_%d', md5($file), ++$count);
248+
$id = sprintf('%s_%d', md5($file), ++$count);
249+
$node['id'] = $id;
248250

249-
$definitions[(string) $node['id']] = array($node, $file, true);
250-
$node->service['id'] = (string) $node['id'];
251+
$definitions[$id] = array($node, $file, true);
252+
$node->service['id'] = $id;
251253
}
252254
}
253255

src/Symfony/Component/DependencyInjection/Reference.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct($id, $invalidBehavior = ContainerInterface::EXCEPTIO
4747
*/
4848
public function __toString()
4949
{
50-
return (string) $this->id;
50+
return $this->id;
5151
}
5252

5353
/**

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,16 @@ public function decode($data, $format, array $context = array())
107107
}
108108

109109
/**
110-
* Checks whether the serializer can encode to given format
111-
*
112-
* @param string $format format name
113-
*
114-
* @return Boolean
115-
*/
110+
* {@inheritdoc}
111+
*/
116112
public function supportsEncoding($format)
117113
{
118114
return 'xml' === $format;
119115
}
120116

121117
/**
122-
* Checks whether the serializer can decode from given format
123-
*
124-
* @param string $format format name
125-
*
126-
* @return Boolean
127-
*/
118+
* {@inheritdoc}
119+
*/
128120
public function supportsDecoding($format)
129121
{
130122
return 'xml' === $format;
@@ -150,12 +142,12 @@ public function getRootNodeName()
150142
}
151143

152144
/**
153-
* @param DOMNode $node
154-
* @param string $val
145+
* @param \DOMNode $node
146+
* @param string $val
155147
*
156148
* @return Boolean
157149
*/
158-
final protected function appendXMLString($node, $val)
150+
final protected function appendXMLString(\DOMNode $node, $val)
159151
{
160152
if (strlen($val) > 0) {
161153
$frag = $this->dom->createDocumentFragment();
@@ -169,12 +161,12 @@ final protected function appendXMLString($node, $val)
169161
}
170162

171163
/**
172-
* @param DOMNode $node
173-
* @param string $val
164+
* @param \DOMNode $node
165+
* @param string $val
174166
*
175167
* @return Boolean
176168
*/
177-
final protected function appendText($node, $val)
169+
final protected function appendText(\DOMNode $node, $val)
178170
{
179171
$nodeText = $this->dom->createTextNode($val);
180172
$node->appendChild($nodeText);
@@ -183,12 +175,12 @@ final protected function appendText($node, $val)
183175
}
184176

185177
/**
186-
* @param DOMNode $node
187-
* @param string $val
178+
* @param \DOMNode $node
179+
* @param string $val
188180
*
189181
* @return Boolean
190182
*/
191-
final protected function appendCData($node, $val)
183+
final protected function appendCData(\DOMNode $node, $val)
192184
{
193185
$nodeText = $this->dom->createCDATASection($val);
194186
$node->appendChild($nodeText);
@@ -197,12 +189,12 @@ final protected function appendCData($node, $val)
197189
}
198190

199191
/**
200-
* @param DOMNode $node
201-
* @param DOMDocumentFragment $fragment
192+
* @param \DOMNode $node
193+
* @param \DOMDocumentFragment $fragment
202194
*
203195
* @return Boolean
204196
*/
205-
final protected function appendDocumentFragment($node, $fragment)
197+
final protected function appendDocumentFragment(\DOMNode $node, $fragment)
206198
{
207199
if ($fragment instanceof \DOMDocumentFragment) {
208200
$node->appendChild($fragment);
@@ -230,11 +222,11 @@ final protected function isElementNameValid($name)
230222
/**
231223
* Parse the input SimpleXmlElement into an array.
232224
*
233-
* @param SimpleXmlElement $node xml to parse
225+
* @param \SimpleXmlElement $node xml to parse
234226
*
235227
* @return array
236228
*/
237-
private function parseXml($node)
229+
private function parseXml(\SimpleXmlElement $node)
238230
{
239231
$data = array();
240232
if ($node->attributes()) {
@@ -258,9 +250,9 @@ private function parseXml($node)
258250
if ($key === 'item') {
259251
if (isset($value['@key'])) {
260252
if (isset($value['#'])) {
261-
$data[(string) $value['@key']] = $value['#'];
253+
$data[$value['@key']] = $value['#'];
262254
} else {
263-
$data[(string) $value['@key']] = $value;
255+
$data[$value['@key']] = $value;
264256
}
265257
} else {
266258
$data['item'][] = $value;
@@ -281,15 +273,15 @@ private function parseXml($node)
281273
/**
282274
* Parse the data and convert it to DOMElements
283275
*
284-
* @param DOMNode $parentNode
285-
* @param array|object $data data
286-
* @param string $xmlRootNodeName
276+
* @param \DOMNode $parentNode
277+
* @param array|object $data
278+
* @param string|null $xmlRootNodeName
287279
*
288280
* @return Boolean
289281
*
290282
* @throws UnexpectedValueException
291283
*/
292-
private function buildXml($parentNode, $data, $xmlRootNodeName = null)
284+
private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
293285
{
294286
$append = true;
295287

@@ -349,14 +341,14 @@ private function buildXml($parentNode, $data, $xmlRootNodeName = null)
349341
/**
350342
* Selects the type of node to create and appends it to the parent.
351343
*
352-
* @param DOMNode $parentNode
344+
* @param \DOMNode $parentNode
353345
* @param array|object $data
354346
* @param string $nodeName
355347
* @param string $key
356348
*
357349
* @return Boolean
358350
*/
359-
private function appendNode($parentNode, $data, $nodeName, $key = null)
351+
private function appendNode(\DOMNode $parentNode, $data, $nodeName, $key = null)
360352
{
361353
$node = $this->dom->createElement($nodeName);
362354
if (null !== $key) {
@@ -386,12 +378,12 @@ private function needsCdataWrapping($val)
386378
/**
387379
* Tests the value being passed and decide what sort of element to create
388380
*
389-
* @param DOMNode $node
390-
* @param mixed $val
381+
* @param \DOMNode $node
382+
* @param mixed $val
391383
*
392384
* @return Boolean
393385
*/
394-
private function selectNodeType($node, $val)
386+
private function selectNodeType(\DOMNode $node, $val)
395387
{
396388
if (is_array($val)) {
397389
return $this->buildXml($node, $val);

src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ abstract class AbstractLoader implements LoaderInterface
1919
* Contains all known namespaces indexed by their prefix
2020
* @var array
2121
*/
22-
protected $namespaces;
22+
protected $namespaces = array();
2323

2424
/**
2525
* Adds a namespace alias.

src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ protected function parseOptions(\SimpleXMLElement $nodes)
180180
*
181181
* @param string $file Path of file
182182
*
183-
* @return SimpleXMLElement
183+
* @return \SimpleXMLElement
184184
*
185185
* @throws MappingException
186186
*/

0 commit comments

Comments
 (0)
0