| Conditions | 20 |
| Paths | 11 |
| Total Lines | 45 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 92 | private static function autowireArgument(ReflectionParameter $parameter, callable $getter) |
||
| 93 | { |
||
| 94 | $type = Reflection::getParameterType($parameter); |
||
| 95 | $method = $parameter->getDeclaringFunction(); |
||
| 96 | $desc = Reflection::toString($parameter); |
||
| 97 | |||
| 98 | if ($type && !Reflection::isBuiltinType($type)) { |
||
| 99 | try { |
||
| 100 | $res = $getter($type, true); |
||
| 101 | } catch (MissingServiceException $e) { |
||
| 102 | $res = null; |
||
| 103 | } catch (ServiceCreationException $e) { |
||
| 104 | throw new ServiceCreationException("{$e->getMessage()} (needed by $desc)", 0, $e); |
||
| 105 | } |
||
| 106 | |||
| 107 | if ($res !== null || $parameter->allowsNull()) { |
||
| 108 | return $res; |
||
| 109 | } elseif (class_exists($type) || interface_exists($type)) { |
||
| 110 | throw new ServiceCreationException("Service of type $type needed by $desc not found. Did you add it to configuration file?"); |
||
| 111 | } else { |
||
| 112 | throw new ServiceCreationException("Class $type needed by $desc not found. Check type hint and 'use' statements."); |
||
| 113 | } |
||
| 114 | |||
| 115 | } elseif ( |
||
| 116 | $method instanceof \ReflectionMethod |
||
| 117 | && $type === 'array' |
||
| 118 | && preg_match('#@param[ \t]+([\w\\\\]+)\[\][ \t]+\$' . $parameter->name . '#', (string) $method->getDocComment(), $m) |
||
| 119 | && ($itemType = Reflection::expandClassName($m[1], $method->getDeclaringClass())) |
||
| 120 | && (class_exists($itemType) || interface_exists($itemType)) |
||
| 121 | ) { |
||
| 122 | return $getter($itemType, false); |
||
| 123 | |||
| 124 | } elseif ( |
||
| 125 | ($type && $parameter->allowsNull()) |
||
| 126 | || $parameter->isOptional() |
||
| 127 | || $parameter->isDefaultValueAvailable() |
||
| 128 | ) { |
||
| 129 | // !optional + defaultAvailable = func($a = null, $b) since 5.4.7 |
||
| 130 | // optional + !defaultAvailable = i.e. Exception::__construct, mysqli::mysqli, ... |
||
| 131 | return $parameter->isDefaultValueAvailable() |
||
| 132 | ? Reflection::getParameterDefaultValue($parameter) |
||
| 133 | : null; |
||
| 134 | |||
| 135 | } else { |
||
| 136 | throw new ServiceCreationException("Parameter $desc has no class type hint or default value, so its value must be specified."); |
||
| 137 | } |
||
| 140 |