Skip to content

Commit

Permalink
fix: Squiz.Arrays.ArrayDeclaration for static closures (#369)
Browse files Browse the repository at this point in the history
Fixes #368
  • Loading branch information
michalbundyra authored Mar 1, 2024
1 parent 0eee691 commit 36d019c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array

if ($tokens[$nextToken]['code'] === T_ARRAY
|| $tokens[$nextToken]['code'] === T_OPEN_SHORT_ARRAY
|| $tokens[$nextToken]['code'] === T_STATIC
|| $tokens[$nextToken]['code'] === T_CLOSURE
|| $tokens[$nextToken]['code'] === T_FN
|| $tokens[$nextToken]['code'] === T_MATCH
Expand All @@ -388,6 +389,10 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
$lastToken = $nextToken;
}

if ($tokens[$nextToken]['code'] === T_STATIC) {
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextToken + 1), null, true);
}

if ($tokens[$nextToken]['code'] === T_ARRAY) {
$nextToken = $tokens[$tokens[$nextToken]['parenthesis_opener']]['parenthesis_closer'];
} else if ($tokens[$nextToken]['code'] === T_OPEN_SHORT_ARRAY) {
Expand Down Expand Up @@ -651,12 +656,6 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
];
$ignoreTokens += Tokens::$castTokens;

if ($tokens[$valuePointer]['code'] === T_CLOSURE
|| $tokens[$valuePointer]['code'] === T_FN
) {
$ignoreTokens += [T_STATIC => T_STATIC];
}

$previous = $phpcsFile->findPrevious($ignoreTokens, ($valuePointer - 1), ($arrayStart + 1), true);
if ($previous === false) {
$previous = $stackPtr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,10 @@ $x = array(
'a',
'b',
);

$x = array(
1, static fn (float $item): float => match ($item) {
2.0 => 3.0,
default => $item
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,11 @@ $x = array(
'a',
'b',
);

$x = array(
1,
static fn (float $item): float => match ($item) {
2.0 => 3.0,
default => $item
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,10 @@ $x = [
'a',
'b',
];

$x = [
1, static fn (float $item): float => match ($item) {
2.0 => 3.0,
default => $item
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,11 @@ $x = [
'a',
'b',
];

$x = [
1,
static fn (float $item): float => match ($item) {
2.0 => 3.0,
default => $item
},
];
2 changes: 2 additions & 0 deletions src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public function getErrorList($testFile='')
523 => 1,
530 => 1,
537 => 1,
540 => 1,
];
case 'ArrayDeclarationUnitTest.2.inc':
return [
Expand Down Expand Up @@ -225,6 +226,7 @@ public function getErrorList($testFile='')
512 => 1,
519 => 1,
526 => 1,
529 => 1,
];
default:
return [];
Expand Down

0 comments on commit 36d019c

Please sign in to comment.