| Conditions | 13 |
| Paths | 336 |
| Total Lines | 36 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 19 |
| CRAP Score | 16.2969 |
| Changes | 4 | ||
| Bugs | 1 | Features | 2 |
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 |
||
| 101 | 3 | protected function getParameterCode(ReflectionParameter $parameter) |
|
| 102 | { |
||
| 103 | 3 | $type = ''; |
|
| 104 | 3 | if (PHP_VERSION_ID >= 50700) { |
|
| 105 | 3 | $reflectionType = $parameter->getType(); |
|
|
1 ignored issue
–
show
|
|||
| 106 | 3 | if ($reflectionType) { |
|
| 107 | 1 | $nsPrefix = $reflectionType->isBuiltin() ? '' : '\\'; |
|
| 108 | 3 | $type = $nsPrefix . (string) $reflectionType; |
|
| 109 | } |
||
| 110 | } else { |
||
| 111 | if ($parameter->isArray()) { |
||
| 112 | $type = 'array'; |
||
| 113 | } elseif ($parameter->isCallable()) { |
||
| 114 | $type = 'callable'; |
||
| 115 | } elseif ($parameter->getClass()) { |
||
| 116 | $type = '\\' . $parameter->getClass()->name; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | 3 | $defaultValue = null; |
|
| 120 | 3 | $isDefaultValueAvailable = $parameter->isDefaultValueAvailable(); |
|
| 121 | 3 | if ($isDefaultValueAvailable) { |
|
| 122 | 2 | $defaultValue = var_export($parameter->getDefaultValue(), true); |
|
| 123 | 3 | } elseif ($parameter->isOptional()) { |
|
| 124 | $defaultValue = 'null'; |
||
| 125 | } |
||
| 126 | $code = ( |
||
| 127 | 3 | ($type ? "$type " : '') . // Typehint |
|
| 128 | 3 | ($parameter->isPassedByReference() ? '&' : '') . // By reference sign |
|
| 129 | 3 | ($parameter->isVariadic() ? '...' : '') . // Variadic symbol |
|
| 130 | 3 | '$' . // Variable symbol |
|
| 131 | 3 | ($parameter->name) . // Name of the argument |
|
| 132 | 3 | ($defaultValue !== null ? (" = " . $defaultValue) : '') // Default value if present |
|
| 133 | ); |
||
| 134 | |||
| 135 | 3 | return $code; |
|
| 136 | } |
||
| 137 | |||
| 195 |
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: