Skip to content

Commit

Permalink
Merge 2ca8c32 into 1a901c9
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Mar 28, 2023
2 parents 1a901c9 + 2ca8c32 commit 1aa55fb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions PHPCSUtils/Internal/IsShortArrayOrList.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,12 @@ private function walkOutside()
if ($i === $this->tokens[$i]['parenthesis_opener']
&& $this->tokens[$i]['parenthesis_closer'] > $this->closer
) {
$beforeParensOpen = $this->phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
if ($this->tokens[$beforeParensOpen]['code'] === \T_LIST) {
// Parse error, mixing long and short list, but that's not our concern.
return self::SHORT_LIST;
}

// Found parentheses wrapping this set of brackets before finding a outer set of brackets.
// This will be a short array.
return self::SHORT_ARRAY;
Expand Down
7 changes: 7 additions & 0 deletions Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ $match = match($foo) {
default => 20,
};

/* testShortArrayNestedInLongArray */
$val = array($a, [$b, $c], $d);


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

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

/* testLongListNestedShortList */
// Invalid list as mixing short list syntax with list() is not allowed, but it is short list syntax.
list($a, [$b, $c], $d) = $array;

/* testNestedAnonClassWithTraitUseAs */
// Parse error, but not our concern, it is short array syntax.
array_map(function($a) {
Expand Down
8 changes: 8 additions & 0 deletions Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ public function dataIsShortArrayOrList()
'testMarker' => '/* testNestedShortListReturnedFromMatchExpression */',
'expected' => IsShortArrayOrList::SHORT_LIST,
],
'short-array-nested-in-long-array' => [
'testMarker' => '/* testShortArrayNestedInLongArray */',
'expected' => IsShortArrayOrList::SHORT_ARRAY,
],

// Invalid syntaxes.
'short-list-nested-empty' => [
Expand All @@ -352,6 +356,10 @@ public function dataIsShortArrayOrList()
'testMarker' => '/* testShortListNestedLongList */',
'expected' => IsShortArrayOrList::SHORT_LIST,
],
'long-list-with-nested-short-list' => [
'testMarker' => '/* testLongListNestedShortList */',
'expected' => IsShortArrayOrList::SHORT_LIST,
],
'parse-error-anon-class-trait-use-as' => [
'testMarker' => '/* testNestedAnonClassWithTraitUseAs */',
'expected' => IsShortArrayOrList::SHORT_ARRAY,
Expand Down
4 changes: 4 additions & 0 deletions Tests/Internal/IsShortArrayOrList/WalkOutsideTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ $var = $closure($arg, [$arg], $arg);
/* testOuterShortArrayStopAtParensFnCall */
$var = $fn($arg, [$arg], $arg);

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


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

Expand Down
4 changes: 4 additions & 0 deletions Tests/Internal/IsShortArrayOrList/WalkOutsideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public function dataWalkOutside()
'testMarker' => '/* testOuterShortArrayStopAtParensFnCall */',
'expected' => IsShortArrayOrList::SHORT_ARRAY,
],
'inner-short-list-stop-test-parens-open-4' => [
'testMarker' => '/* testInnerShortListStopAtParensLongList */',
'expected' => IsShortArrayOrList::SHORT_LIST,
],

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

0 comments on commit 1aa55fb

Please sign in to comment.