| Conditions | 11 |
| Paths | 384 |
| Total Lines | 28 |
| Code Lines | 22 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 17 |
| CRAP Score | 11.8364 |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 114 | 2 | protected function getParameterCode(ReflectionParameter $parameter) |
|
| 115 | { |
||
| 116 | 2 | $type = ''; |
|
| 117 | 2 | if ($parameter->isArray()) { |
|
| 118 | $type = 'array'; |
||
| 119 | 2 | } elseif ($parameter->isCallable()) { |
|
| 120 | $type = 'callable'; |
||
| 121 | 2 | } elseif ($parameter->getClass()) { |
|
| 122 | $type = '\\' . $parameter->getClass()->name; |
||
| 123 | } |
||
| 124 | 2 | $defaultValue = null; |
|
| 125 | 2 | $isDefaultValueAvailable = $parameter->isDefaultValueAvailable(); |
|
| 126 | 2 | if ($isDefaultValueAvailable) { |
|
| 127 | 2 | $defaultValue = var_export($parameter->getDefaultValue(), true); |
|
| 128 | 2 | } elseif ($parameter->isOptional()) { |
|
| 129 | $defaultValue = 'null'; |
||
| 130 | } |
||
| 131 | $code = ( |
||
| 132 | 2 | ($type ? "$type " : '') . // Typehint |
|
| 133 | 2 | ($parameter->isPassedByReference() ? '&' : '') . // By reference sign |
|
| 134 | 2 | ($this->useVariadics && $parameter->isVariadic() ? '...' : '') . // Variadic symbol |
|
|
1 ignored issue
–
show
|
|||
| 135 | 2 | '$' . // Variable symbol |
|
| 136 | 2 | ($parameter->name) . // Name of the argument |
|
| 137 | 2 | ($defaultValue !== null ? (" = " . $defaultValue) : '') // Default value if present |
|
| 138 | ); |
||
| 139 | |||
| 140 | 2 | return $code; |
|
| 141 | } |
||
| 142 | |||
| 182 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: