Skip to content

Commit e6b37df

Browse files
committed
Hotfix: correct array indicies when using ternary or coalesce ops
Fixes #2694 I have find out that the issue is also when using coalesce operator. Example, which is valid code, but has no sens would be: ```php $array = [ 'foo' => 'foo', 'bar' => $baz ? ['abc'] : ['def'], 'hey' => $baz ?? ['one'] ?? ['two'], ]; ``` Without fix we have 5 elements in `$indices` but we should have 3. Added tests to cover changes.
1 parent ed879f1 commit e6b37df

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

src/Sniffs/AbstractArraySniff.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ public function process(File $phpcsFile, $stackPtr)
8989
continue;
9090
}
9191

92+
if ($tokens[$checkToken]['code'] === T_INLINE_THEN
93+
|| $tokens[$checkToken]['code'] === T_COALESCE
94+
) {
95+
$checkToken = $phpcsFile->findEndOfStatement($checkToken);
96+
continue;
97+
}
98+
9299
if ($tokens[$checkToken]['code'] === T_ARRAY
93100
|| $tokens[$checkToken]['code'] === T_OPEN_SHORT_ARRAY
94101
|| $tokens[$checkToken]['code'] === T_CLOSURE

src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,13 @@ $var = [
5656
2 => 'two',
5757
/* three */ 3 => 'three',
5858
];
59+
60+
$array = [
61+
'foo' => 'foo',
62+
'bar' => $baz ?
63+
['abc'] :
64+
['def'],
65+
'hey' => $baz ??
66+
['one'] ??
67+
['two'],
68+
];

src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@ $var = [
5757
2 => 'two',
5858
/* three */ 3 => 'three',
5959
];
60+
61+
$array = [
62+
'foo' => 'foo',
63+
'bar' => $baz ?
64+
['abc'] :
65+
['def'],
66+
'hey' => $baz ??
67+
['one'] ??
68+
['two'],
69+
];

src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public function getErrorList()
3737
56 => 1,
3838
57 => 1,
3939
58 => 1,
40+
61 => 1,
41+
62 => 1,
42+
65 => 1,
4043
];
4144

4245
}//end getErrorList()

0 commit comments

Comments
 (0)