Skip to content

Commit 36d019c

Browse files
fix: Squiz.Arrays.ArrayDeclaration for static closures (#369)
Fixes #368
1 parent 0eee691 commit 36d019c

File tree

6 files changed

+37
-6
lines changed

6 files changed

+37
-6
lines changed

src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
378378

379379
if ($tokens[$nextToken]['code'] === T_ARRAY
380380
|| $tokens[$nextToken]['code'] === T_OPEN_SHORT_ARRAY
381+
|| $tokens[$nextToken]['code'] === T_STATIC
381382
|| $tokens[$nextToken]['code'] === T_CLOSURE
382383
|| $tokens[$nextToken]['code'] === T_FN
383384
|| $tokens[$nextToken]['code'] === T_MATCH
@@ -388,6 +389,10 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
388389
$lastToken = $nextToken;
389390
}
390391

392+
if ($tokens[$nextToken]['code'] === T_STATIC) {
393+
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextToken + 1), null, true);
394+
}
395+
391396
if ($tokens[$nextToken]['code'] === T_ARRAY) {
392397
$nextToken = $tokens[$tokens[$nextToken]['parenthesis_opener']]['parenthesis_closer'];
393398
} else if ($tokens[$nextToken]['code'] === T_OPEN_SHORT_ARRAY) {
@@ -651,12 +656,6 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
651656
];
652657
$ignoreTokens += Tokens::$castTokens;
653658

654-
if ($tokens[$valuePointer]['code'] === T_CLOSURE
655-
|| $tokens[$valuePointer]['code'] === T_FN
656-
) {
657-
$ignoreTokens += [T_STATIC => T_STATIC];
658-
}
659-
660659
$previous = $phpcsFile->findPrevious($ignoreTokens, ($valuePointer - 1), ($arrayStart + 1), true);
661660
if ($previous === false) {
662661
$previous = $stackPtr;

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,10 @@ $x = array(
535535
'a',
536536
'b',
537537
);
538+
539+
$x = array(
540+
1, static fn (float $item): float => match ($item) {
541+
2.0 => 3.0,
542+
default => $item
543+
},
544+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,11 @@ $x = array(
571571
'a',
572572
'b',
573573
);
574+
575+
$x = array(
576+
1,
577+
static fn (float $item): float => match ($item) {
578+
2.0 => 3.0,
579+
default => $item
580+
},
581+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,10 @@ $x = [
524524
'a',
525525
'b',
526526
];
527+
528+
$x = [
529+
1, static fn (float $item): float => match ($item) {
530+
2.0 => 3.0,
531+
default => $item
532+
},
533+
];

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,11 @@ $x = [
558558
'a',
559559
'b',
560560
];
561+
562+
$x = [
563+
1,
564+
static fn (float $item): float => match ($item) {
565+
2.0 => 3.0,
566+
default => $item
567+
},
568+
];

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public function getErrorList($testFile='')
134134
523 => 1,
135135
530 => 1,
136136
537 => 1,
137+
540 => 1,
137138
];
138139
case 'ArrayDeclarationUnitTest.2.inc':
139140
return [
@@ -225,6 +226,7 @@ public function getErrorList($testFile='')
225226
512 => 1,
226227
519 => 1,
227228
526 => 1,
229+
529 => 1,
228230
];
229231
default:
230232
return [];

0 commit comments

Comments
 (0)