Skip to content
This repository
Browse code

merged branch Tobion/is_object_instance_of (PR #6051)

This PR was merged into the master branch.

Commits
-------

966e7d6 [DI] removed unneeded is_object() calls

Discussion
----------

[DI] removed unneeded is_object() calls

I searched through all of symfony for occurences of the coding style `(is_object($value) && $value instanceof Object)` with a regex like `is_object\(\$[a-zA-z0-9]+\) && \$[a-zA-z0-9]+ instanceof `.
The `is_object` calls are not needed in this case. Only the DI component made such duplicate checks.
  • Loading branch information...
commit 7e88eccfb6f48ae864e3b711eeb56ac5e500d066 2 parents 5127492 + 966e7d6
Fabien Potencier fabpot authored
12 src/Symfony/Component/DependencyInjection/ContainerBuilder.php
@@ -806,10 +806,8 @@ private function createService(Definition $definition, $id)
806 806 }
807 807
808 808 if ($callable = $definition->getConfigurator()) {
809   - if (is_array($callable) && is_object($callable[0]) && $callable[0] instanceof Reference) {
810   - $callable[0] = $this->get((string) $callable[0]);
811   - } elseif (is_array($callable)) {
812   - $callable[0] = $parameterBag->resolveValue($callable[0]);
  809 + if (is_array($callable)) {
  810 + $callable[0] = $callable[0] instanceof Reference ? $this->get((string) $callable[0]) : $parameterBag->resolveValue($callable[0]);
813 811 }
814 812
815 813 if (!is_callable($callable)) {
@@ -835,9 +833,9 @@ public function resolveServices($value)
835 833 foreach ($value as &$v) {
836 834 $v = $this->resolveServices($v);
837 835 }
838   - } elseif (is_object($value) && $value instanceof Reference) {
  836 + } elseif ($value instanceof Reference) {
839 837 $value = $this->get((string) $value, $value->getInvalidBehavior());
840   - } elseif (is_object($value) && $value instanceof Definition) {
  838 + } elseif ($value instanceof Definition) {
841 839 $value = $this->createService($value, null);
842 840 }
843 841
@@ -895,7 +893,7 @@ public static function getServiceConditionals($value)
895 893 foreach ($value as $v) {
896 894 $services = array_unique(array_merge($services, self::getServiceConditionals($v)));
897 895 }
898   - } elseif (is_object($value) && $value instanceof Reference && $value->getInvalidBehavior() === ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
  896 + } elseif ($value instanceof Reference && $value->getInvalidBehavior() === ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
899 897 $services[] = (string) $value;
900 898 }
901 899
2  src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php
@@ -130,7 +130,7 @@ private function findEdges($id, $arguments, $required, $name)
130 130 {
131 131 $edges = array();
132 132 foreach ($arguments as $argument) {
133   - if (is_object($argument) && $argument instanceof Parameter) {
  133 + if ($argument instanceof Parameter) {
134 134 $argument = $this->container->hasParameter($argument) ? $this->container->getParameter($argument) : null;
135 135 } elseif (is_string($argument) && preg_match('/^%([^%]+)%$/', $argument, $match)) {
136 136 $argument = $this->container->hasParameter($match[1]) ? $this->container->getParameter($match[1]) : null;
10 src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
@@ -452,7 +452,7 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta
452 452 }
453 453
454 454 if (is_array($callable)) {
455   - if (is_object($callable[0]) && $callable[0] instanceof Reference) {
  455 + if ($callable[0] instanceof Reference) {
456 456 return sprintf(" %s->%s(\$%s);\n", $this->getServiceCall((string) $callable[0]), $callable[1], $variableName);
457 457 }
458 458
@@ -997,7 +997,7 @@ private function dumpValue($value, $interpolate = true)
997 997 }
998 998
999 999 return sprintf('array(%s)', implode(', ', $code));
1000   - } elseif (is_object($value) && $value instanceof Definition) {
  1000 + } elseif ($value instanceof Definition) {
1001 1001 if (null !== $this->definitionVariables && $this->definitionVariables->contains($value)) {
1002 1002 return $this->dumpValue($this->definitionVariables->offsetGet($value), $interpolate);
1003 1003 }
@@ -1029,15 +1029,15 @@ private function dumpValue($value, $interpolate = true)
1029 1029 }
1030 1030
1031 1031 return sprintf("new \\%s(%s)", substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
1032   - } elseif (is_object($value) && $value instanceof Variable) {
  1032 + } elseif ($value instanceof Variable) {
1033 1033 return '$'.$value;
1034   - } elseif (is_object($value) && $value instanceof Reference) {
  1034 + } elseif ($value instanceof Reference) {
1035 1035 if (null !== $this->referenceVariables && isset($this->referenceVariables[$id = (string) $value])) {
1036 1036 return $this->dumpValue($this->referenceVariables[$id], $interpolate);
1037 1037 }
1038 1038
1039 1039 return $this->getServiceCall((string) $value, $value);
1040   - } elseif (is_object($value) && $value instanceof Parameter) {
  1040 + } elseif ($value instanceof Parameter) {
1041 1041 return $this->dumpParameter($value);
1042 1042 } elseif (true === $interpolate && is_string($value)) {
1043 1043 if (preg_match('/^%([^%]+)%$/', $value, $match)) {
8 src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php
@@ -158,7 +158,7 @@ private function addService($definition, $id, \DOMElement $parent)
158 158 if ($callable = $definition->getConfigurator()) {
159 159 $configurator = $this->document->createElement('configurator');
160 160 if (is_array($callable)) {
161   - $configurator->setAttribute((is_object($callable[0]) && $callable[0] instanceof Reference ? 'service' : 'class'), $callable[0]);
  161 + $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
162 162 $configurator->setAttribute('method', $callable[1]);
163 163 } else {
164 164 $configurator->setAttribute('function', $callable);
@@ -230,7 +230,7 @@ private function convertParameters($parameters, $type, \DOMElement $parent, $key
230 230 if (is_array($value)) {
231 231 $element->setAttribute('type', 'collection');
232 232 $this->convertParameters($value, $type, $element, 'key');
233   - } elseif (is_object($value) && $value instanceof Reference) {
  233 + } elseif ($value instanceof Reference) {
234 234 $element->setAttribute('type', 'service');
235 235 $element->setAttribute('id', (string) $value);
236 236 $behaviour = $value->getInvalidBehavior();
@@ -239,7 +239,7 @@ private function convertParameters($parameters, $type, \DOMElement $parent, $key
239 239 } elseif ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
240 240 $element->setAttribute('on-invalid', 'ignore');
241 241 }
242   - } elseif (is_object($value) && $value instanceof Definition) {
  242 + } elseif ($value instanceof Definition) {
243 243 $element->setAttribute('type', 'service');
244 244 $this->addService($value, null, $element);
245 245 } else {
@@ -294,7 +294,7 @@ public static function phpToXml($value)
294 294 return 'true';
295 295 case false === $value:
296 296 return 'false';
297   - case is_object($value) && $value instanceof Parameter:
  297 + case $value instanceof Parameter:
298 298 return '%'.$value.'%';
299 299 case is_object($value) || is_resource($value):
300 300 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
6 src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php
@@ -120,7 +120,7 @@ private function addService($id, $definition)
120 120
121 121 if ($callable = $definition->getConfigurator()) {
122 122 if (is_array($callable)) {
123   - if (is_object($callable[0]) && $callable[0] instanceof Reference) {
  123 + if ($callable[0] instanceof Reference) {
124 124 $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]);
125 125 } else {
126 126 $callable = array($callable[0], $callable[1]);
@@ -211,9 +211,9 @@ private function dumpValue($value)
211 211 }
212 212
213 213 return $code;
214   - } elseif (is_object($value) && $value instanceof Reference) {
  214 + } elseif ($value instanceof Reference) {
215 215 return $this->getServiceCall((string) $value, $value);
216   - } elseif (is_object($value) && $value instanceof Parameter) {
  216 + } elseif ($value instanceof Parameter) {
217 217 return $this->getParameterCall((string) $value);
218 218 } elseif (is_object($value) || is_resource($value)) {
219 219 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');

0 comments on commit 7e88ecc

Please sign in to comment.
Something went wrong with that request. Please try again.