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