| Conditions | 20 |
| Paths | 52 |
| Total Lines | 65 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 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 |
||
| 78 | private static function findClasses($path): array |
||
| 79 | { |
||
| 80 | $contents = file_get_contents($path); |
||
| 81 | $tokens = token_get_all($contents); |
||
| 82 | $classes = []; |
||
| 83 | $namespace = ''; |
||
| 84 | for ($i = 0; isset($tokens[$i]); ++$i) { |
||
| 85 | $token = $tokens[$i]; |
||
| 86 | if (!isset($token[1])) { |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | $class = ''; |
||
| 90 | switch ($token[0]) { |
||
| 91 | case T_NAMESPACE: |
||
| 92 | $namespace = ''; |
||
| 93 | // If there is a namespace, extract it |
||
| 94 | while (isset($tokens[++$i][1])) { |
||
| 95 | if (PHP_MAJOR_VERSION >= 8) { |
||
| 96 | if (\in_array($tokens[$i][0], [T_NAME_QUALIFIED], false)) { |
||
| 97 | $namespace .= $tokens[$i][1]; |
||
| 98 | } |
||
| 99 | } else { |
||
| 100 | if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR], false)) { |
||
| 101 | $namespace .= $tokens[$i][1]; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | $namespace .= '\\'; |
||
| 106 | break; |
||
| 107 | case T_CLASS: |
||
| 108 | case T_INTERFACE: |
||
| 109 | case T_TRAIT: |
||
| 110 | // Skip usage of ::class constant |
||
| 111 | $isClassConstant = false; |
||
| 112 | for ($j = $i - 1; $j > 0; --$j) { |
||
| 113 | if (!isset($tokens[$j][1])) { |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | if (T_DOUBLE_COLON === $tokens[$j][0]) { |
||
| 117 | $isClassConstant = true; |
||
| 118 | break; |
||
| 119 | } elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT], false)) { |
||
| 120 | break; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | if ($isClassConstant) { |
||
| 124 | break; |
||
| 125 | } |
||
| 126 | // Find the classname |
||
| 127 | while (isset($tokens[++$i][1])) { |
||
| 128 | $t = $tokens[$i]; |
||
| 129 | if (T_STRING === $t[0]) { |
||
| 130 | $class .= $t[1]; |
||
| 131 | } elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
||
| 132 | break; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | $classes[] = ltrim($namespace . $class, '\\'); |
||
| 136 | break; |
||
| 137 | default: |
||
| 138 | break; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | return $classes; |
||
| 143 | } |
||
| 145 |