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