Skip to content

Commit 218d2a5

Browse files
authored
Merge pull request #379 from michalbundyra/fix/followup-369
Follow-up #369 - Squiz.Arrays.ArrayDeclaration for static
2 parents eeb5c29 + fff9d82 commit 218d2a5

6 files changed

+36
-5
lines changed

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ 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
382381
|| $tokens[$nextToken]['code'] === T_CLOSURE
383382
|| $tokens[$nextToken]['code'] === T_FN
384383
|| $tokens[$nextToken]['code'] === T_MATCH
@@ -389,10 +388,6 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
389388
$lastToken = $nextToken;
390389
}
391390

392-
if ($tokens[$nextToken]['code'] === T_STATIC) {
393-
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextToken + 1), null, true);
394-
}
395-
396391
if ($tokens[$nextToken]['code'] === T_ARRAY) {
397392
$nextToken = $tokens[$tokens[$nextToken]['parenthesis_opener']]['parenthesis_closer'];
398393
} else if ($tokens[$nextToken]['code'] === T_OPEN_SHORT_ARRAY) {
@@ -656,6 +651,16 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
656651
];
657652
$ignoreTokens += Tokens::$castTokens;
658653

654+
if ($tokens[$valuePointer]['code'] === T_CLOSURE
655+
|| $tokens[$valuePointer]['code'] === T_FN
656+
) {
657+
// Check if the closure is static, if it is, override the value pointer as indices before skip static.
658+
$staticPointer = $phpcsFile->findPrevious($ignoreTokens, ($valuePointer - 1), ($arrayStart + 1), true);
659+
if ($staticPointer !== false && $tokens[$staticPointer]['code'] === T_STATIC) {
660+
$valuePointer = $staticPointer;
661+
}
662+
}
663+
659664
$previous = $phpcsFile->findPrevious($ignoreTokens, ($valuePointer - 1), ($arrayStart + 1), true);
660665
if ($previous === false) {
661666
$previous = $stackPtr;

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

+5
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,8 @@ $x = array(
542542
default => $item
543543
},
544544
);
545+
546+
$x = array(
547+
1, static::helloWorld(), $class instanceof static,
548+
2,
549+
);

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

+7
Original file line numberDiff line numberDiff line change
@@ -579,3 +579,10 @@ $x = array(
579579
default => $item
580580
},
581581
);
582+
583+
$x = array(
584+
1,
585+
static::helloWorld(),
586+
$class instanceof static,
587+
2,
588+
);

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

+5
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,8 @@ $x = [
531531
default => $item
532532
},
533533
];
534+
535+
$x = [
536+
1, static::helloWorld(), $class instanceof static,
537+
2,
538+
];

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

+7
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,10 @@ $x = [
566566
default => $item
567567
},
568568
];
569+
570+
$x = [
571+
1,
572+
static::helloWorld(),
573+
$class instanceof static,
574+
2,
575+
];

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

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public function getErrorList($testFile='')
135135
530 => 1,
136136
537 => 1,
137137
540 => 1,
138+
547 => 2,
138139
];
139140
case 'ArrayDeclarationUnitTest.2.inc':
140141
return [
@@ -227,6 +228,7 @@ public function getErrorList($testFile='')
227228
519 => 1,
228229
526 => 1,
229230
529 => 1,
231+
536 => 2,
230232
];
231233
default:
232234
return [];

0 commit comments

Comments
 (0)