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