Skip to content

Commit 2ca8c32

Browse files
committed
IsShortArrayOrList: add extra safeguard for mixed long/short array/lists
Includes additional tests.
1 parent 1a901c9 commit 2ca8c32

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

PHPCSUtils/Internal/IsShortArrayOrList.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,12 @@ private function walkOutside()
613613
if ($i === $this->tokens[$i]['parenthesis_opener']
614614
&& $this->tokens[$i]['parenthesis_closer'] > $this->closer
615615
) {
616+
$beforeParensOpen = $this->phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
617+
if ($this->tokens[$beforeParensOpen]['code'] === \T_LIST) {
618+
// Parse error, mixing long and short list, but that's not our concern.
619+
return self::SHORT_LIST;
620+
}
621+
616622
// Found parentheses wrapping this set of brackets before finding a outer set of brackets.
617623
// This will be a short array.
618624
return self::SHORT_ARRAY;

Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ $match = match($foo) {
182182
default => 20,
183183
};
184184

185+
/* testShortArrayNestedInLongArray */
186+
$val = array($a, [$b, $c], $d);
187+
185188

186189
/* ===== Tests involving various illegal syntaxes/parse errors. ===== */
187190

@@ -196,6 +199,10 @@ $match = match($foo) {
196199
// Invalid list as mixing short list syntax with list() is not allowed, but it is short list syntax.
197200
[list($a, $b), list($c, $d)] = [[1, 2], [3, 4]];
198201

202+
/* testLongListNestedShortList */
203+
// Invalid list as mixing short list syntax with list() is not allowed, but it is short list syntax.
204+
list($a, [$b, $c], $d) = $array;
205+
199206
/* testNestedAnonClassWithTraitUseAs */
200207
// Parse error, but not our concern, it is short array syntax.
201208
array_map(function($a) {

Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ public function dataIsShortArrayOrList()
338338
'testMarker' => '/* testNestedShortListReturnedFromMatchExpression */',
339339
'expected' => IsShortArrayOrList::SHORT_LIST,
340340
],
341+
'short-array-nested-in-long-array' => [
342+
'testMarker' => '/* testShortArrayNestedInLongArray */',
343+
'expected' => IsShortArrayOrList::SHORT_ARRAY,
344+
],
341345

342346
// Invalid syntaxes.
343347
'short-list-nested-empty' => [
@@ -352,6 +356,10 @@ public function dataIsShortArrayOrList()
352356
'testMarker' => '/* testShortListNestedLongList */',
353357
'expected' => IsShortArrayOrList::SHORT_LIST,
354358
],
359+
'long-list-with-nested-short-list' => [
360+
'testMarker' => '/* testLongListNestedShortList */',
361+
'expected' => IsShortArrayOrList::SHORT_LIST,
362+
],
355363
'parse-error-anon-class-trait-use-as' => [
356364
'testMarker' => '/* testNestedAnonClassWithTraitUseAs */',
357365
'expected' => IsShortArrayOrList::SHORT_ARRAY,

Tests/Internal/IsShortArrayOrList/WalkOutsideTest.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ $var = $closure($arg, [$arg], $arg);
9999
/* testOuterShortArrayStopAtParensFnCall */
100100
$var = $fn($arg, [$arg], $arg);
101101

102+
/* testInnerShortListStopAtParensLongList */
103+
// Invalid list as mixing short list syntax with list() is not allowed, but it is short list syntax.
104+
list($a, [$b, $c], $d) = $array;
105+
102106

103107
/* ===== Tests verifying that the loop stops as quickly as possible for INNER brackets. ===== */
104108

Tests/Internal/IsShortArrayOrList/WalkOutsideTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public function dataWalkOutside()
9797
'testMarker' => '/* testOuterShortArrayStopAtParensFnCall */',
9898
'expected' => IsShortArrayOrList::SHORT_ARRAY,
9999
],
100+
'inner-short-list-stop-test-parens-open-4' => [
101+
'testMarker' => '/* testInnerShortListStopAtParensLongList */',
102+
'expected' => IsShortArrayOrList::SHORT_LIST,
103+
],
100104

101105
'inner-short-array-stop-test-curly-open' => [
102106
'testMarker' => '/* testShortArrayInShortArrayStopAtCurly */',

0 commit comments

Comments
 (0)