|
14 | 14 | use PhpCsFixer\Fixer\FixerInterface; |
15 | 15 | use PhpCsFixer\FixerDefinition\FixerDefinition; |
16 | 16 | use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
17 | | -use PhpCsFixer\Tokenizer\CT; |
18 | 17 | use PhpCsFixer\Tokenizer\Tokens; |
19 | | -use PhpCsFixer\Tokenizer\TokensAnalyzer; |
| 18 | +use PhpCsFixer\Tokenizer\CT; |
20 | 19 | use SplFileInfo; |
21 | 20 |
|
22 | 21 |
|
@@ -54,13 +53,36 @@ public function isCandidate(Tokens $tokens): bool |
54 | 53 |
|
55 | 54 | public function fix(SplFileInfo $file, Tokens $tokens): void |
56 | 55 | { |
57 | | - $tokensAnalyzer = new TokensAnalyzer($tokens); |
58 | | - |
59 | 56 | for ($idx = $tokens->count() - 1; $idx >= 0; --$idx) { |
60 | | - if ($tokensAnalyzer->isArray($idx) && $tokensAnalyzer->isArrayMultiLine($idx)) { |
61 | | - $this->fixArray($tokens, $idx); |
| 57 | + if (!$this->isMultilineArray($tokens, $idx)) { continue; } |
| 58 | + $this->fixArray($tokens, $idx); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private function isMultilineArray(Tokens $tokens, int $idx): bool |
| 63 | + { |
| 64 | + $isArray = $tokens[$idx]->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN]); |
| 65 | + if (!$isArray) { return false; } |
| 66 | + |
| 67 | + $openBrace = $tokens[$idx]->isGivenKind(T_ARRAY) ? $tokens->getNextMeaningfulToken($idx) : $idx; |
| 68 | + $blockType = Tokens::detectBlockType($tokens[$openBrace]); |
| 69 | + if (!$blockType || !$blockType['isStart']) { return false; } |
| 70 | + |
| 71 | + $endIndex = $tokens->findBlockEnd($blockType['type'], $openBrace); |
| 72 | + for ($index = $openBrace + 1; $index < $endIndex; ++$index) { |
| 73 | + $token = $tokens[$index]; |
| 74 | + $blockType = Tokens::detectBlockType($token); |
| 75 | + |
| 76 | + if ($blockType && $blockType['isStart']) { |
| 77 | + $index = $tokens->findBlockEnd($blockType['type'], $index); |
| 78 | + continue; |
62 | 79 | } |
| 80 | + |
| 81 | + $isLineBreak = $token->isWhitespace() && strpos($token->getContent(), "\n") !== false; |
| 82 | + if ($isLineBreak) { return true; } |
63 | 83 | } |
| 84 | + |
| 85 | + return false; |
64 | 86 | } |
65 | 87 |
|
66 | 88 | private function fixArray(Tokens $tokens, int $idx): void |
|
0 commit comments