Skip to content

Commit 4595ba1

Browse files
committed
Removed internal class dependency
1 parent 56db760 commit 4595ba1

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/Fixer/NoTrailingCommaInMultilineArrayFixer.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
use PhpCsFixer\Fixer\FixerInterface;
1515
use PhpCsFixer\FixerDefinition\FixerDefinition;
1616
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
17-
use PhpCsFixer\Tokenizer\CT;
1817
use PhpCsFixer\Tokenizer\Tokens;
19-
use PhpCsFixer\Tokenizer\TokensAnalyzer;
18+
use PhpCsFixer\Tokenizer\CT;
2019
use SplFileInfo;
2120

2221

@@ -54,13 +53,36 @@ public function isCandidate(Tokens $tokens): bool
5453

5554
public function fix(SplFileInfo $file, Tokens $tokens): void
5655
{
57-
$tokensAnalyzer = new TokensAnalyzer($tokens);
58-
5956
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;
6279
}
80+
81+
$isLineBreak = $token->isWhitespace() && strpos($token->getContent(), "\n") !== false;
82+
if ($isLineBreak) { return true; }
6383
}
84+
85+
return false;
6486
}
6587

6688
private function fixArray(Tokens $tokens, int $idx): void

0 commit comments

Comments
 (0)