| Conditions | 10 |
| Paths | 12 |
| Total Lines | 50 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 21 |
| CRAP Score | 10.4096 |
| Changes | 2 | ||
| 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 |
||
| 52 | 1 | private function resolveDependenciesRecursively(ReflectionParameter $parameter) |
|
| 53 | { |
||
| 54 | 1 | if (!$parameter->hasType()) { |
|
| 55 | throw new RuntimeException("No parameter type for '{$parameter->getName()}'"); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** @var ReflectionNamedType $paramType */ |
||
| 59 | 1 | $paramType = $parameter->getType(); |
|
| 60 | 1 | $paramTypeName = $paramType->getName(); |
|
| 61 | 1 | if (!class_exists($paramTypeName) && !interface_exists($paramTypeName)) { |
|
| 62 | return $parameter->getDefaultValue(); |
||
| 63 | } |
||
| 64 | |||
| 65 | 1 | $reflection = new ReflectionClass($paramTypeName); |
|
| 66 | |||
| 67 | // If it's an interface we need to figure out which concrete class do we want to use. |
||
| 68 | 1 | if ($reflection->isInterface()) { |
|
| 69 | 1 | $mappingInterfaces = $this->gacelaConfigFile->getMappingInterfaces(); |
|
| 70 | 1 | $concreteClass = $mappingInterfaces[$reflection->getName()] ?? ''; |
|
| 71 | // a callable will be a way to bypass the instantiation and instead |
||
| 72 | // use the result from the callable that was defined in the gacela config file. |
||
| 73 | 1 | if (is_callable($concreteClass)) { |
|
| 74 | return $concreteClass(); |
||
| 75 | } |
||
| 76 | // an Exception will be thrown if there is no concrete class found for the interface. |
||
| 77 | 1 | if (empty($concreteClass)) { |
|
| 78 | throw new DependencyResolverNotFoundException($reflection->getName()); |
||
| 79 | } |
||
| 80 | // finally, override the $reflection (the interface) by the concrete resolved class. |
||
| 81 | /** @var class-string $concreteClass */ |
||
| 82 | 1 | $reflection = new ReflectionClass($concreteClass); |
|
| 83 | } |
||
| 84 | |||
| 85 | 1 | $constructor = $reflection->getConstructor(); |
|
| 86 | 1 | if (!$constructor) { |
|
| 87 | 1 | return $reflection->newInstance(); |
|
| 88 | } |
||
| 89 | |||
| 90 | /** @var list<mixed> $innerDependencies */ |
||
| 91 | 1 | $innerDependencies = []; |
|
| 92 | |||
| 93 | 1 | foreach ($constructor->getParameters() as $constructorParameter) { |
|
| 94 | 1 | $paramType = $constructorParameter->getType(); |
|
| 95 | 1 | if ($paramType) { |
|
| 96 | /** @psalm-suppress MixedAssignment */ |
||
| 97 | 1 | $innerDependencies[] = $this->resolveDependenciesRecursively($constructorParameter); |
|
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | 1 | return $reflection->newInstanceArgs($innerDependencies); |
|
|
1 ignored issue
–
show
|
|||
| 102 | } |
||
| 104 |