| Conditions | 19 |
| Paths | 225 |
| Total Lines | 93 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 123 | protected function applyFix(\SplFileInfo $file, Tokens $tokens): void |
||
| 124 | { |
||
| 125 | foreach ($tokens as $index => $token) { |
||
| 126 | if (!$token->isGivenKind(T_IMPLEMENTS)) { |
||
| 127 | if (!$token->isGivenKind(T_EXTENDS)) { |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | |||
| 131 | $nameTokenIndex = $tokens->getPrevMeaningfulToken($index); |
||
| 132 | $interfaceTokenIndex = $tokens->getPrevMeaningfulToken($nameTokenIndex); |
||
|
|
|||
| 133 | $interfaceToken = $tokens[$interfaceTokenIndex]; |
||
| 134 | |||
| 135 | if (!$interfaceToken->isGivenKind(T_INTERFACE)) { |
||
| 136 | continue; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | $interfaceIndex = 0; |
||
| 141 | $interfaces = [['tokens' => []]]; |
||
| 142 | |||
| 143 | $implementsStart = $index + 1; |
||
| 144 | $classStart = $tokens->getNextTokenOfKind($implementsStart, ['{']); |
||
| 145 | $implementsEnd = $tokens->getPrevNonWhitespace($classStart); |
||
| 146 | |||
| 147 | for ($i = $implementsStart; $i <= $implementsEnd; ++$i) { |
||
| 148 | if ($tokens[$i]->equals(',')) { |
||
| 149 | ++$interfaceIndex; |
||
| 150 | $interfaces[$interfaceIndex] = ['tokens' => []]; |
||
| 151 | |||
| 152 | continue; |
||
| 153 | } |
||
| 154 | |||
| 155 | $interfaces[$interfaceIndex]['tokens'][] = $tokens[$i]; |
||
| 156 | } |
||
| 157 | |||
| 158 | if (1 === \count($interfaces)) { |
||
| 159 | continue; |
||
| 160 | } |
||
| 161 | |||
| 162 | foreach ($interfaces as $interfaceIndex => $interface) { |
||
| 163 | $interfaceTokens = Tokens::fromArray($interface['tokens'], false); |
||
| 164 | |||
| 165 | $normalized = ''; |
||
| 166 | $actualInterfaceIndex = $interfaceTokens->getNextMeaningfulToken(-1); |
||
| 167 | |||
| 168 | while ($interfaceTokens->offsetExists($actualInterfaceIndex)) { |
||
| 169 | $token = $interfaceTokens[$actualInterfaceIndex]; |
||
| 170 | |||
| 171 | if (null === $token || $token->isComment() || $token->isWhitespace()) { |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | |||
| 175 | $normalized .= str_replace('\\', ' ', $token->getContent()); |
||
| 176 | ++$actualInterfaceIndex; |
||
| 177 | } |
||
| 178 | |||
| 179 | $interfaces[$interfaceIndex]['normalized'] = $normalized; |
||
| 180 | $interfaces[$interfaceIndex]['originalIndex'] = $interfaceIndex; |
||
| 181 | } |
||
| 182 | |||
| 183 | usort($interfaces, function (array $first, array $second) { |
||
| 184 | $score = self::ORDER_LENGTH === $this->configuration[self::OPTION_ORDER] |
||
| 185 | ? \strlen($first['normalized']) - \strlen($second['normalized']) |
||
| 186 | : strcasecmp($first['normalized'], $second['normalized']); |
||
| 187 | |||
| 188 | if (self::DIRECTION_DESCEND === $this->configuration[self::OPTION_DIRECTION]) { |
||
| 189 | $score *= -1; |
||
| 190 | } |
||
| 191 | |||
| 192 | return $score; |
||
| 193 | }); |
||
| 194 | |||
| 195 | $changed = false; |
||
| 196 | |||
| 197 | foreach ($interfaces as $interfaceIndex => $interface) { |
||
| 198 | if ($interface['originalIndex'] !== $interfaceIndex) { |
||
| 199 | $changed = true; |
||
| 200 | |||
| 201 | break; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | if (!$changed) { |
||
| 206 | continue; |
||
| 207 | } |
||
| 208 | |||
| 209 | $newTokens = array_shift($interfaces)['tokens']; |
||
| 210 | |||
| 211 | foreach ($interfaces as $interface) { |
||
| 212 | array_push($newTokens, new Token(','), ...$interface['tokens']); |
||
| 213 | } |
||
| 214 | |||
| 215 | $tokens->overrideRange($implementsStart, $implementsEnd, $newTokens); |
||
| 216 | } |
||
| 236 |