| Conditions | 18 |
| Paths | 101 |
| Total Lines | 45 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 342 |
| 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 |
||
| 9 | public static function findClass($path) |
||
| 10 | { |
||
| 11 | if (!file_exists($path) || !is_file($path)) { |
||
| 12 | throw new RuntimeException(sprintf('There is no file on path "%s".', $path)); |
||
| 13 | } |
||
| 14 | |||
| 15 | if (!is_readable($path)) { |
||
| 16 | throw new RuntimeException(sprintf('There is a file on path "%s", but it is not readable.', $path)); |
||
| 17 | } |
||
| 18 | |||
| 19 | $contents = file_get_contents($path); |
||
| 20 | $namespace = $class = ''; |
||
| 21 | $getting_namespace = $getting_class = false; |
||
| 22 | |||
| 23 | foreach (token_get_all($contents) as $token) { |
||
| 24 | |||
| 25 | if (is_array($token) && $token[0] === T_NAMESPACE) { |
||
| 26 | $getting_namespace = true; |
||
| 27 | } |
||
| 28 | |||
| 29 | if (is_array($token) && $token[0] === T_CLASS) { |
||
| 30 | $getting_class = true; |
||
| 31 | } |
||
| 32 | |||
| 33 | if ($getting_namespace === true) { |
||
| 34 | |||
| 35 | if(is_array($token) && in_array($token[0], [T_STRING, T_NS_SEPARATOR], true)) { |
||
| 36 | $namespace .= $token[1]; |
||
| 37 | } elseif ($token === ';') { |
||
| 38 | $getting_namespace = false; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | if ($getting_class === true && is_array($token) && $token[0] === T_STRING) { |
||
| 43 | $class = $token[1]; |
||
| 44 | break; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | if ('' === $namespace.$class) { |
||
| 49 | throw new RuntimeException(sprintf('There is no class definition in file on path "%s".', $path)); |
||
| 50 | } |
||
| 51 | |||
| 52 | return $namespace ? $namespace . '\\' . $class : $class; |
||
| 53 | } |
||
| 54 | } |
||
| 55 |