| Conditions | 15 |
| Paths | 96 |
| Total Lines | 62 |
| Code Lines | 31 |
| 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 |
||
| 116 | protected function resolveParameter( |
||
| 117 | ContainerInterface $container, |
||
| 118 | ReflectionParameter $parameter, |
||
| 119 | ?ParameterCollection $parameters = null |
||
| 120 | ): mixed { |
||
| 121 | $class = null; |
||
| 122 | $className = null; |
||
| 123 | $types = $this->getTypes($parameter); |
||
| 124 | |||
| 125 | // TODO: handle for union types |
||
| 126 | if (count($types) > 0) { |
||
| 127 | foreach ($types as /** @var ReflectionNamedType $type */ $type) { |
||
| 128 | $name = $type->getName(); |
||
| 129 | if ($type->isBuiltin() === false && $container->has($name)) { |
||
| 130 | $className = $name; |
||
| 131 | break; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | if ($className !== null) { |
||
| 137 | $class = new ReflectionClass($className); |
||
| 138 | } |
||
| 139 | |||
| 140 | //If the parameter is not a class |
||
| 141 | if ($class === null) { |
||
| 142 | if ($parameters !== null) { |
||
| 143 | if ( |
||
| 144 | $parameters->has($parameter->name) && |
||
| 145 | $parameters->get($parameter->name) !== null |
||
| 146 | ) { |
||
| 147 | return $parameters->get($parameter->name) |
||
| 148 | ->getValue($container); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | if ($parameter->isDefaultValueAvailable()) { |
||
| 153 | try { |
||
| 154 | return $parameter->getDefaultValue(); |
||
| 155 | } catch (ReflectionException $e) { |
||
| 156 | throw new ContainerException($e->getMessage()); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($parameter->isOptional()) { |
||
| 161 | // This branch is required to work around PHP bugs where a parameter is optional |
||
| 162 | // but has no default value available through reflection. Specifically, PDO exhibits |
||
| 163 | // this behavior. |
||
| 164 | return null; |
||
| 165 | } |
||
| 166 | |||
| 167 | if (count($types) === 1 && $types[0]->isBuiltin() === false) { |
||
| 168 | throw new NotFoundException(sprintf( |
||
| 169 | 'The type/class [%s] does not exist in the container!', |
||
| 170 | $types[0]->getName() |
||
| 171 | )); |
||
| 172 | } |
||
| 173 | |||
| 174 | throw new ContainerException(sprintf('Parameter [%s] is not bound!', $parameter->name)); |
||
| 175 | } |
||
| 176 | |||
| 177 | return $container->get($class->name); |
||
| 178 | } |
||
| 206 |