| Conditions | 24 |
| Paths | 64 |
| Total Lines | 77 |
| 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 |
||
| 71 | private static function parseUsesAndAliases(string $code, string $forClass = null): array |
||
| 72 | { |
||
| 73 | try { |
||
| 74 | $tokens = token_get_all($code, TOKEN_PARSE); |
||
| 75 | } catch (\Exception $e) { |
||
| 76 | $tokens = []; |
||
| 77 | } |
||
| 78 | $namespace = $class = $classLevel = $level = null; |
||
| 79 | $res = $uses = []; |
||
| 80 | |||
| 81 | while ($token = current($tokens)) { |
||
| 82 | next($tokens); |
||
| 83 | switch (is_array($token) ? $token[0] : $token) { |
||
| 84 | case T_NAMESPACE: |
||
| 85 | $namespace = ltrim(self::fetch($tokens, [T_STRING, T_NS_SEPARATOR]).'\\', '\\'); |
||
| 86 | $uses = []; |
||
| 87 | break; |
||
| 88 | |||
| 89 | case T_CLASS: |
||
| 90 | case T_INTERFACE: |
||
| 91 | case T_TRAIT: |
||
| 92 | if ($name = self::fetch($tokens, T_STRING)) { |
||
| 93 | $class = $namespace.$name; |
||
| 94 | $classLevel = $level + 1; |
||
| 95 | $res[$class] = $uses; |
||
| 96 | if ($class === $forClass) { |
||
| 97 | return $res; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | break; |
||
| 101 | |||
| 102 | case T_USE: |
||
| 103 | while (! $class && ($name = self::fetch($tokens, [T_STRING, T_NS_SEPARATOR]))) { |
||
| 104 | $name = ltrim($name, '\\'); |
||
| 105 | if (self::fetch($tokens, '{')) { |
||
| 106 | while ($suffix = self::fetch($tokens, [T_STRING, T_NS_SEPARATOR])) { |
||
| 107 | /* @noinspection NotOptimalIfConditionsInspection */ |
||
| 108 | if (self::fetch($tokens, T_AS)) { |
||
| 109 | $uses[self::fetch($tokens, T_STRING)] = $name.$suffix; |
||
| 110 | } else { |
||
| 111 | $tmp = explode('\\', $suffix); |
||
| 112 | $uses[end($tmp)] = $name.$suffix; |
||
| 113 | } |
||
| 114 | if (! self::fetch($tokens, ',')) { |
||
| 115 | break; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | } elseif (self::fetch($tokens, T_AS)) { |
||
| 120 | $uses[self::fetch($tokens, T_STRING)] = $name; |
||
| 121 | |||
| 122 | } else { |
||
| 123 | $tmp = explode('\\', $name); |
||
| 124 | $uses[end($tmp)] = $name; |
||
| 125 | } |
||
| 126 | if (! self::fetch($tokens, ',')) { |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | break; |
||
| 131 | |||
| 132 | case T_CURLY_OPEN: |
||
| 133 | case T_DOLLAR_OPEN_CURLY_BRACES: |
||
| 134 | case '{': |
||
| 135 | $level++; |
||
| 136 | break; |
||
| 137 | |||
| 138 | case '}': |
||
| 139 | if ($level === $classLevel) { |
||
| 140 | $class = $classLevel = null; |
||
| 141 | } |
||
| 142 | $level--; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | return $res; |
||
| 147 | } |
||
| 148 | |||
| 164 |