| Conditions | 10 |
| Paths | 76 |
| Total Lines | 38 |
| Code Lines | 22 |
| 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 |
||
| 43 | public static function autowireArguments( |
||
| 44 | ReflectionFunctionAbstract $method, |
||
| 45 | array $arguments, |
||
| 46 | callable $getter |
||
| 47 | ): array { |
||
| 48 | $optCount = 0; |
||
| 49 | $num = -1; |
||
| 50 | $res = []; |
||
| 51 | |||
| 52 | foreach ($method->getParameters() as $num => $param) { |
||
| 53 | $paramName = $param->name; |
||
| 54 | if (!$param->isVariadic() && array_key_exists($paramName, $arguments)) { |
||
| 55 | $res[$num] = $arguments[$paramName]; |
||
| 56 | unset($arguments[$paramName], $arguments[$num]); |
||
| 57 | } elseif (array_key_exists($num, $arguments)) { |
||
| 58 | $res[$num] = $arguments[$num]; |
||
| 59 | unset($arguments[$num]); |
||
| 60 | } else { |
||
| 61 | $res[$num] = self::autowireArgument($param, $getter); |
||
| 62 | } |
||
| 63 | |||
| 64 | $optCount = $param->isOptional() && $res[$num] === ($param->isDefaultValueAvailable() ? Reflection::getParameterDefaultValue($param) : null) |
||
| 65 | ? $optCount + 1 |
||
| 66 | : 0; |
||
| 67 | } |
||
| 68 | |||
| 69 | // extra parameters |
||
| 70 | while (array_key_exists(++$num, $arguments)) { |
||
| 71 | $res[$num] = $arguments[$num]; |
||
| 72 | unset($arguments[$num]); |
||
| 73 | $optCount = 0; |
||
| 74 | } |
||
| 75 | |||
| 76 | if ($optCount) { |
||
| 77 | $res = array_slice($res, 0, -$optCount); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $res; |
||
| 81 | } |
||
| 140 |