Conditions | 13 |
Paths | 22 |
Total Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Tests | 22 |
CRAP Score | 13.0139 |
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 |
||
21 | 2 | public static function findClassInFile(string $file): ?string |
|
22 | { |
||
23 | 2 | $class = false; |
|
24 | 2 | $namespace = false; |
|
25 | 2 | $tokens = token_get_all(file_get_contents($file)); |
|
26 | |||
27 | 2 | if (defined('T_NAME_QUALIFIED')) { |
|
28 | $namespaceToken = T_NAME_QUALIFIED; |
||
29 | } else { |
||
30 | 2 | $namespaceToken = T_STRING; |
|
31 | } |
||
32 | |||
33 | 2 | for ($i = 0, $count = count($tokens); $i < $count; ++$i) { |
|
34 | 2 | $token = $tokens[$i]; |
|
35 | |||
36 | 2 | if (!is_array($token)) { |
|
37 | 2 | continue; |
|
38 | } |
||
39 | |||
40 | 2 | if (true === $class && T_STRING === $token[0]) { |
|
41 | 2 | return $namespace.'\\'.$token[1]; |
|
42 | } |
||
43 | |||
44 | 2 | if (true === $namespace && $namespaceToken === $token[0]) { |
|
45 | 2 | $namespace = ''; |
|
46 | do { |
||
47 | 2 | $namespace .= $token[1]; |
|
48 | 2 | $token = $tokens[++$i]; |
|
49 | 2 | } while ($i < $count && is_array($token) && in_array($token[0], [T_NS_SEPARATOR, $namespaceToken])); |
|
50 | } |
||
51 | |||
52 | 2 | if (T_CLASS === $token[0]) { |
|
53 | 2 | $class = true; |
|
54 | } |
||
55 | |||
56 | 2 | if (T_NAMESPACE === $token[0]) { |
|
57 | 2 | $namespace = true; |
|
58 | } |
||
59 | } |
||
60 | |||
61 | 1 | return null; |
|
62 | } |
||
63 | } |
||
64 |