| Conditions | 14 |
| Paths | 66 |
| Total Lines | 31 |
| 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 |
||
| 16 | public static function getClassName(string $path): string |
||
| 17 | { |
||
| 18 | $namespace = $class = ''; |
||
| 19 | $contents = file_get_contents($path); |
||
| 20 | $hasNamespace = $hasClass = false; |
||
| 21 | |||
| 22 | foreach (token_get_all($contents) as $token) { |
||
| 23 | if (is_array($token) && $token[0] === T_NAMESPACE) { |
||
| 24 | $hasNamespace = true; |
||
| 25 | } |
||
| 26 | |||
| 27 | if (is_array($token) && $token[0] === T_CLASS) { |
||
| 28 | $hasClass = true; |
||
| 29 | } |
||
| 30 | |||
| 31 | if ($hasNamespace) { |
||
| 32 | if (is_array($token) && in_array($token[0], [T_STRING, T_NS_SEPARATOR], true)) { |
||
| 33 | $namespace .= $token[1]; |
||
| 34 | } else if ($token === ';') { |
||
| 35 | $hasNamespace = false; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | if ($hasClass && is_array($token) && $token[0] === T_STRING) { |
||
| 40 | $class = $token[1]; |
||
| 41 | break; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | return $namespace ? $namespace . '\\' . $class : $class; |
||
| 46 | } |
||
| 47 | |||
| 70 |