| Conditions | 21 |
| Paths | 207 |
| Total Lines | 69 |
| 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 |
||
| 90 | private function getMethodParameters(\ReflectionMethod $method, $forCall = false) |
||
| 91 | { |
||
| 92 | $parameters = []; |
||
| 93 | foreach ($method->getParameters() as $i => $parameter) { |
||
| 94 | $name = '$' . $parameter->name; |
||
| 95 | /* Note: PHP extensions may use empty names for reference arguments |
||
| 96 | * or "..." for methods taking a variable number of arguments. |
||
| 97 | */ |
||
| 98 | if ($name === '$' || $name === '$...') { |
||
| 99 | $name = '$arg' . $i; |
||
| 100 | } |
||
| 101 | if ($parameter->isVariadic()) { |
||
| 102 | if ($forCall) { |
||
| 103 | continue; |
||
| 104 | } |
||
| 105 | $name = '...' . $name; |
||
| 106 | } |
||
| 107 | $nullable = ''; |
||
| 108 | $default = ''; |
||
| 109 | $reference = ''; |
||
| 110 | $typeDeclaration = ''; |
||
| 111 | if (!$forCall) { |
||
| 112 | if ($parameter->hasType() && (string) $parameter->getType() !== 'self') { |
||
| 113 | if (version_compare(PHP_VERSION, '7.1', '>=') |
||
| 114 | && $parameter->allowsNull() |
||
| 115 | && !$parameter->isVariadic() |
||
| 116 | ) { |
||
| 117 | $nullable = '?'; |
||
| 118 | } |
||
| 119 | $typeDeclaration = (string) $parameter->getType() . ' '; |
||
| 120 | } elseif ($parameter->isArray()) { |
||
| 121 | $typeDeclaration = 'array '; |
||
| 122 | } elseif ($parameter->isCallable()) { |
||
| 123 | $typeDeclaration = 'callable '; |
||
| 124 | } else { |
||
| 125 | try { |
||
| 126 | $class = $parameter->getClass(); |
||
| 127 | } catch (\ReflectionException $e) { |
||
| 128 | throw new \RuntimeException( |
||
| 129 | sprintf( |
||
| 130 | 'Cannot mock %s::%s() because a class or ' . |
||
| 131 | 'interface used in the signature is not loaded', |
||
| 132 | $method->class, |
||
| 133 | $method->name |
||
| 134 | ), |
||
| 135 | 0, |
||
| 136 | $e |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | if ($class !== null) { |
||
| 140 | $typeDeclaration = $class->name . ' '; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | if (!$parameter->isVariadic()) { |
||
| 144 | if ($parameter->isDefaultValueAvailable()) { |
||
| 145 | $value = $parameter->getDefaultValue(); |
||
| 146 | $default = ' = ' . var_export($value, true); |
||
| 147 | } elseif ($parameter->isOptional()) { |
||
| 148 | $default = ' = null'; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | if (false === $forCall && $parameter->isPassedByReference()) { |
||
| 153 | $reference = '&'; |
||
| 154 | } |
||
| 155 | $parameters[] = $nullable . $typeDeclaration . $reference . $name . $default; |
||
| 156 | } |
||
| 157 | return implode(', ', $parameters); |
||
| 158 | } |
||
| 159 | |||
| 211 |