Conditions | 11 |
Paths | 12 |
Total Lines | 34 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
79 | protected function doLoad(string $id, int $invalidBehavior) |
||
80 | { |
||
81 | if (\array_key_exists($id, $this->types)) { |
||
82 | if (1 === \count($autowired = $this->types[$id])) { |
||
83 | return $this->get($autowired[1]); |
||
84 | } |
||
85 | |||
86 | if (AbstractContainer::IGNORE_MULTIPLE_SERVICE !== $invalidBehavior && AbstractContainer::EXCEPTION_ON_MULTIPLE_SERVICE === $invalidBehavior) { |
||
87 | \natsort($autowired); |
||
88 | $autowired = \count($autowired) <= 3 ? \implode(', ', $autowired) : $autowired[0] . ', ...' . \end($autowired); |
||
89 | |||
90 | throw new ContainerResolutionException(\sprintf('Multiple services of type %s found: %s.', $id, $autowired)); |
||
91 | } |
||
92 | |||
93 | return \array_map([$this, 'get'], $autowired); |
||
94 | } |
||
95 | |||
96 | if (\preg_match('/\[(.*?)?\]$/', $id, $matches, \PREG_UNMATCHED_AS_NULL)) { |
||
97 | $autowired = $this->types[\str_replace($matches[0], '', $id)] ?? []; |
||
98 | |||
99 | if (!empty($autowired)) { |
||
100 | if (\is_numeric($k = $matches[1])) { |
||
101 | $k = $autowired[$k] ?? null; |
||
102 | } |
||
103 | |||
104 | return $k ? $this->get($k) : \array_map([$this, 'get'], $autowired); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | if (AbstractContainer::NULL_ON_INVALID_SERVICE === $invalidBehavior) { |
||
109 | return null; |
||
110 | } |
||
111 | |||
112 | throw new NotFoundServiceException(\sprintf('The "%s" requested service is not defined in container.', $id)); |
||
113 | } |
||
119 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: