|
16 | 16 | use PhpCsFixer\FixerDefinition\CodeSample; |
17 | 17 | use PhpCsFixer\FixerDefinition\FixerDefinition; |
18 | 18 | use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 19 | +use PhpCsFixer\Tokenizer\CT; |
19 | 20 | use PhpCsFixer\Tokenizer\Token; |
20 | 21 | use PhpCsFixer\Tokenizer\Tokens; |
21 | 22 |
|
@@ -54,33 +55,47 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void |
54 | 55 | while (++$index < $tokens->count()) { |
55 | 56 | $index = $this->skipNamespacedCode($tokens, $index); |
56 | 57 |
|
57 | | - /** @var Token $token */ |
58 | | - $token = $tokens[$index]; |
59 | | - |
60 | | - if (!$token->isGivenKind(T_NS_SEPARATOR)) { |
| 58 | + if (!$this->isToRemove($tokens, $index)) { |
61 | 59 | continue; |
62 | 60 | } |
63 | 61 |
|
64 | | - /** @var int $prevIndex */ |
65 | | - $prevIndex = $tokens->getPrevMeaningfulToken($index); |
| 62 | + $tokens->clearTokenAndMergeSurroundingWhitespace($index); |
| 63 | + } |
| 64 | + } |
66 | 65 |
|
67 | | - /** @var Token $prevToken */ |
68 | | - $prevToken = $tokens[$prevIndex]; |
| 66 | + private function isToRemove(Tokens $tokens, int $index): bool |
| 67 | + { |
| 68 | + /** @var Token $token */ |
| 69 | + $token = $tokens[$index]; |
69 | 70 |
|
70 | | - if ($prevToken->isGivenKind(T_STRING)) { |
71 | | - continue; |
72 | | - } |
| 71 | + if (!$token->isGivenKind(T_NS_SEPARATOR)) { |
| 72 | + return false; |
| 73 | + } |
73 | 74 |
|
74 | | - /** @var int $nextIndex */ |
75 | | - $nextIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[T_COMMENT], [T_DOC_COMMENT], [T_NS_SEPARATOR], [T_STRING], [T_WHITESPACE]]); |
| 75 | + /** @var int $prevIndex */ |
| 76 | + $prevIndex = $tokens->getPrevMeaningfulToken($index); |
76 | 77 |
|
77 | | - /** @var Token $nextToken */ |
78 | | - $nextToken = $tokens[$nextIndex]; |
| 78 | + /** @var Token $prevToken */ |
| 79 | + $prevToken = $tokens[$prevIndex]; |
79 | 80 |
|
80 | | - if ($prevToken->isGivenKind(T_NEW) || $nextToken->isGivenKind(T_DOUBLE_COLON)) { |
81 | | - $tokens->clearTokenAndMergeSurroundingWhitespace($index); |
82 | | - } |
| 81 | + if ($prevToken->isGivenKind(T_STRING)) { |
| 82 | + return false; |
83 | 83 | } |
| 84 | + if ($prevToken->isGivenKind([T_NEW, CT::T_NULLABLE_TYPE, CT::T_TYPE_COLON])) { |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + /** @var int $nextIndex */ |
| 89 | + $nextIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[T_COMMENT], [T_DOC_COMMENT], [T_NS_SEPARATOR], [T_STRING], [T_WHITESPACE]]); |
| 90 | + |
| 91 | + /** @var Token $nextToken */ |
| 92 | + $nextToken = $tokens[$nextIndex]; |
| 93 | + |
| 94 | + if ($nextToken->isGivenKind(T_DOUBLE_COLON)) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + return $prevToken->equalsAny(['(', ',']) && $nextToken->isGivenKind(T_VARIABLE); |
84 | 99 | } |
85 | 100 |
|
86 | 101 | private function skipNamespacedCode(Tokens $tokens, int $index): int |
|
0 commit comments