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