Skip to content

Commit e338674

Browse files
authored
bug: Do not treat implements list comma as array comma (#6595)
1 parent fcc32c8 commit e338674

4 files changed

+50
-13
lines changed

src/Fixer/ArrayNotation/NoWhitespaceBeforeCommaInArrayFixer.php

+25-13
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@ public function getDefinition(): FixerDefinitionInterface
4242
[
4343
new CodeSample("<?php \$x = array(1 , \"2\");\n"),
4444
new VersionSpecificCodeSample(
45-
<<<'SAMPLE'
46-
<?php
47-
$x = [<<<EOD
48-
foo
49-
EOD
50-
, 'bar'
51-
];
52-
53-
SAMPLE
54-
,
45+
<<<'PHP'
46+
<?php
47+
$x = [<<<EOD
48+
foo
49+
EOD
50+
, 'bar'
51+
];
52+
53+
PHP,
5554
new VersionSpecification(70300),
5655
['after_heredoc' => true]
5756
),
@@ -72,7 +71,7 @@ public function isCandidate(Tokens $tokens): bool
7271
*/
7372
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
7473
{
75-
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
74+
for ($index = $tokens->count() - 1; $index > 0; --$index) {
7675
if ($tokens[$index]->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN])) {
7776
$this->fixSpacing($index, $tokens);
7877
}
@@ -121,8 +120,6 @@ private function fixSpacing(int $index, Tokens $tokens): void
121120

122121
/**
123122
* Method to move index over the non-array elements like function calls or function declarations.
124-
*
125-
* @return int New index
126123
*/
127124
private function skipNonArrayElements(int $index, Tokens $tokens): int
128125
{
@@ -138,6 +135,21 @@ private function skipNonArrayElements(int $index, Tokens $tokens): int
138135
}
139136
}
140137

138+
if ($tokens[$index]->equals(',') && $this->commaIsPartOfImplementsList($index, $tokens)) {
139+
--$index;
140+
}
141+
141142
return $index;
142143
}
144+
145+
private function commaIsPartOfImplementsList(int $index, Tokens $tokens): bool
146+
{
147+
do {
148+
$index = $tokens->getPrevMeaningfulToken($index);
149+
150+
$current = $tokens[$index];
151+
} while ($current->isGivenKind(T_STRING) || $current->equals(','));
152+
153+
return $current->isGivenKind(T_IMPLEMENTS);
154+
}
143155
}

src/Fixer/ArrayNotation/WhitespaceAfterCommaInArrayFixer.php

+15
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ private function skipNonArrayElements(int $index, Tokens $tokens): int
129129
}
130130
}
131131

132+
if ($tokens[$index]->equals(',') && $this->commaIsPartOfImplementsList($index, $tokens)) {
133+
--$index;
134+
}
135+
132136
return $index;
133137
}
138+
139+
private function commaIsPartOfImplementsList(int $index, Tokens $tokens): bool
140+
{
141+
do {
142+
$index = $tokens->getPrevMeaningfulToken($index);
143+
144+
$current = $tokens[$index];
145+
} while ($current->isGivenKind(T_STRING) || $current->equals(','));
146+
147+
return $current->isGivenKind(T_IMPLEMENTS);
148+
}
134149
}

tests/Fixer/ArrayNotation/NoWhitespaceBeforeCommaInArrayFixerTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public function provideFixCases(): array
8080
'<?php $x = [ 1, "2","c" => function( $x ,$y) { return [$x, $y]; }, $y];',
8181
'<?php $x = [ 1 , "2","c" => function( $x ,$y) { return [$x , $y]; }, $y];',
8282
],
83+
// don't change anonymous class implements list but change array inside
84+
[
85+
'<?php $x = [ 1, "2","c" => new class implements Foo , Bar { const FOO = ["x", "y"]; }, $y];',
86+
'<?php $x = [ 1 , "2","c" => new class implements Foo , Bar { const FOO = ["x" , "y"]; }, $y];',
87+
],
8388
// associative array (old)
8489
[
8590
'<?php $x = array( "a" => $a, "b" => "b",3=>$this->foo(), "d" => 30);',

tests/Fixer/ArrayNotation/WhitespaceAfterCommaInArrayFixerTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public function provideFixCases(): array
7373
'<?php $x = [1, "2", "c" => function( $x ,$y) { return [$x , $y]; }, $y ];',
7474
'<?php $x = [1, "2","c" => function( $x ,$y) { return [$x ,$y]; },$y ];',
7575
],
76+
// don't change anonymous class implements list but change array inside
77+
[
78+
'<?php $x = [1, "2", "c" => new class implements Foo ,Bar { const FOO = ["x", "y"]; }, $y ];',
79+
'<?php $x = [1, "2","c" => new class implements Foo ,Bar { const FOO = ["x","y"]; },$y ];',
80+
],
7681
// associative array (old)
7782
[
7883
'<?php $x = array("a" => $a , "b" => "b", 3=>$this->foo(), "d" => 30 );',

0 commit comments

Comments
 (0)