Skip to content

Commit

Permalink
Ignore tokens that belong to array element declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Aug 29, 2020
1 parent 5b0b6ab commit d24aeba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,16 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array

$valuePointer = $value['value'];

$previous = $phpcsFile->findPrevious([T_WHITESPACE, T_COMMA], ($valuePointer - 1), ($arrayStart + 1), true);
$ignoreTokens = ([
T_WHITESPACE => T_WHITESPACE,
T_COMMA => T_COMMA,
] + Tokens::$castTokens);

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

$previous = $phpcsFile->findPrevious($ignoreTokens, ($valuePointer - 1), ($arrayStart + 1), true);
if ($previous === false) {
$previous = $stackPtr;
}
Expand Down
13 changes: 13 additions & 0 deletions src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,19 @@ $c];
['a' => $a, 'b' => $b,
'c' => $c];

[
static function() {
return null;
},
(array) [],
(bool) [],
(double) [],
(int) [],
(object) [],
(string) [],
(unset) [],
];

// Intentional syntax error.
$a = [
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,19 @@ $foo = [
'c' => $c,
];

[
static function() {
return null;
},
(array) [],
(bool) [],
(double) [],
(int) [],
(object) [],
(string) [],
(unset) [],
];

// Intentional syntax error.
$a = [
'a' =>
Expand Down

0 comments on commit d24aeba

Please sign in to comment.