| Conditions | 15 |
| Paths | 46 |
| Total Lines | 62 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 103 | public static function getResolved(Resolver $resolver, object $service, \ReflectionClass $reflection): object |
||
| 104 | { |
||
| 105 | if (\PHP_VERSION_ID < 80000) { |
||
| 106 | return $service; |
||
|
|
|||
| 107 | } |
||
| 108 | |||
| 109 | $properties = []; |
||
| 110 | |||
| 111 | foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { |
||
| 112 | if (empty($propertyAttributes = $property->getAttributes(Inject::class))) { |
||
| 113 | continue; |
||
| 114 | } |
||
| 115 | |||
| 116 | if (\count($propertyAttributes) > 1) { |
||
| 117 | throw new ContainerResolutionException( |
||
| 118 | \sprintf('Property "%s" has more than one #[Inject] attribute.', $property->getName()) |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | if (!empty($pValue = $propertyAttributes[0]->getArguments()[0] ?? null)) { |
||
| 123 | $properties[0][$property->getName()] = $propertyAttributes[0]->newInstance()->resolve($resolver); |
||
| 124 | continue; |
||
| 125 | } |
||
| 126 | |||
| 127 | foreach (Resolver::getTypes($property) as $pType) { |
||
| 128 | if (Reflection::isBuiltinType($pType)) { |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | if (!empty($pValue = $resolver->resolveReference('?' . $pType))) { |
||
| 133 | $properties[0][$property->getName()] = $pValue; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
||
| 139 | if (empty($methodAttributes = $method->getAttributes(Inject::class))) { |
||
| 140 | continue; |
||
| 141 | } |
||
| 142 | |||
| 143 | if (\count($methodAttributes) > 1) { |
||
| 144 | throw new ContainerResolutionException( |
||
| 145 | \sprintf('Method %s::%s has more than one #[Inject] attribute.', $reflection->getName(), $method->getName()) |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | if (!empty($methodAttributes[0]->getArguments())) { |
||
| 150 | throw new ContainerResolutionException(\sprintf('Method with #[Inject] attribute does not support having arguments.')); |
||
| 151 | } |
||
| 152 | |||
| 153 | $properties[1][$method->getName()] = $resolver->autowireArguments($method); |
||
| 154 | } |
||
| 155 | |||
| 156 | if (!empty($properties)) { |
||
| 157 | $service = new self($service, $properties); |
||
| 158 | |||
| 159 | if (null === $resolver->getBuilder()) { |
||
| 160 | $service = $service->resolve(); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | return $service; |
||
| 165 | } |
||
| 167 |