Skip to content

Commit

Permalink
Follow-up #369 - Squiz.Arrays.ArrayDeclaration for static
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra authored and jrfnl committed Mar 31, 2024
1 parent eeb5c29 commit fff9d82
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ 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 @@ -389,10 +388,6 @@ 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 @@ -656,6 +651,16 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
];
$ignoreTokens += Tokens::$castTokens;

if ($tokens[$valuePointer]['code'] === T_CLOSURE
|| $tokens[$valuePointer]['code'] === T_FN
) {
// Check if the closure is static, if it is, override the value pointer as indices before skip static.
$staticPointer = $phpcsFile->findPrevious($ignoreTokens, ($valuePointer - 1), ($arrayStart + 1), true);
if ($staticPointer !== false && $tokens[$staticPointer]['code'] === T_STATIC) {
$valuePointer = $staticPointer;
}
}

$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 @@ -542,3 +542,8 @@ $x = array(
default => $item
},
);

$x = array(
1, static::helloWorld(), $class instanceof static,
2,
);
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,10 @@ $x = array(
default => $item
},
);

$x = array(
1,
static::helloWorld(),
$class instanceof static,
2,
);
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,8 @@ $x = [
default => $item
},
];

$x = [
1, static::helloWorld(), $class instanceof static,
2,
];
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,10 @@ $x = [
default => $item
},
];

$x = [
1,
static::helloWorld(),
$class instanceof static,
2,
];
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 @@ -135,6 +135,7 @@ public function getErrorList($testFile='')
530 => 1,
537 => 1,
540 => 1,
547 => 2,
];
case 'ArrayDeclarationUnitTest.2.inc':
return [
Expand Down Expand Up @@ -227,6 +228,7 @@ public function getErrorList($testFile='')
519 => 1,
526 => 1,
529 => 1,
536 => 2,
];
default:
return [];
Expand Down

0 comments on commit fff9d82

Please sign in to comment.