| Conditions | 13 |
| Paths | 11 |
| Total Lines | 38 |
| Code Lines | 22 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 104 | protected function doGet(string $id, bool $nullOnInvalid) |
||
| 105 | { |
||
| 106 | if (\preg_match('/\[(.*?)?\]$/', $id, $matches, \PREG_UNMATCHED_AS_NULL)) { |
||
| 107 | $autowired = $this->types[\str_replace($matches[0], '', $oldId = $id)] ?? []; |
||
| 108 | |||
| 109 | if (!empty($autowired)) { |
||
| 110 | if (isset($matches[1])) { |
||
| 111 | return $this->services[$oldId] = \array_map([$this, 'get'], $autowired); |
||
| 112 | } |
||
| 113 | |||
| 114 | foreach ($autowired as $serviceId) { |
||
| 115 | if ($serviceId === $matches[1]) { |
||
| 116 | return $this->get($this->aliases[$oldId] = $serviceId); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } elseif (!empty($autowired = $this->types[$id] ?? [])) { |
||
| 121 | if (\count($autowired) > 1) { |
||
| 122 | \natsort($autowired); |
||
| 123 | $autowired = \count($autowired) <= 3 ? \implode(', ', $autowired) : $autowired[0] . ', ...' . \end($autowired); |
||
| 124 | |||
| 125 | throw new ContainerResolutionException(\sprintf('Multiple services of type %s found: %s.', $id, $autowired)); |
||
| 126 | } |
||
| 127 | |||
| 128 | if (!isset($this->aliases[$id])) { |
||
| 129 | $this->aliases[$id] = $autowired[0]; |
||
| 130 | } |
||
| 131 | |||
| 132 | return $this->services[$autowired[0]] ?? $this->get($autowired[0]); |
||
| 133 | } elseif (\array_key_exists($id, $this->resolvers)) { |
||
| 134 | [$resolver, $inline] = $this->resolvers[$id]; |
||
| 135 | |||
| 136 | return \call_user_func_array($resolver, $inline ? [$id] : []); |
||
| 137 | } elseif ($nullOnInvalid) { |
||
| 138 | return null; |
||
| 139 | } |
||
| 140 | |||
| 141 | throw new NotFoundServiceException(\sprintf('The "%s" requested service is not defined in container.', $id)); |
||
| 142 | } |
||
| 148 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: