Skip to content

Commit

Permalink
Fixed bug squizlabs#3362 : Generic.WhiteSpace.ScopeIndent false posit…
Browse files Browse the repository at this point in the history
…ive for arrow functions inside arrays
  • Loading branch information
gsherwood committed Jun 17, 2021
1 parent 9633c8a commit 3964931
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #3357 : Generic.Functions.OpeningFunctionBraceBsdAllman removes return type when additional lines are present
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #3362 : Generic.WhiteSpace.ScopeIndent false positive for arrow functions inside arrays
</notes>
<contents>
<dir name="/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,15 @@ $foo = match ($type) {
],
};

$a = [
'a' => [
'a' => fn () => foo()
],
'a' => [
'a' => 'a',
]
];

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,15 @@ $foo = match ($type) {
],
};

$a = [
'a' => [
'a' => fn () => foo()
],
'a' => [
'a' => 'a',
]
];

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,15 @@ $foo = match ($type) {
],
};

$a = [
'a' => [
'a' => fn () => foo()
],
'a' => [
'a' => 'a',
]
];

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,15 @@ $foo = match ($type) {
],
};

$a = [
'a' => [
'a' => fn () => foo()
],
'a' => [
'a' => 'a',
]
];

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
1527 => 1,
1529 => 1,
1530 => 1,
1558 => 1,
1559 => 1,
1560 => 1,
1561 => 1,
1567 => 1,
1568 => 1,
1569 => 1,
1570 => 1,
];

}//end getErrorList()
Expand Down
6 changes: 4 additions & 2 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -2344,8 +2344,10 @@ protected function processAdditional()

if (isset($endTokens[$this->tokens[$scopeCloser]['code']]) === true) {
if ($lastEndToken !== null
&& $this->tokens[$scopeCloser]['code'] === T_CLOSE_PARENTHESIS
&& $this->tokens[$scopeCloser]['parenthesis_opener'] < $arrow
&& ((isset($this->tokens[$scopeCloser]['parenthesis_opener']) === true
&& $this->tokens[$scopeCloser]['parenthesis_opener'] < $arrow)
|| (isset($this->tokens[$scopeCloser]['bracket_opener']) === true
&& $this->tokens[$scopeCloser]['bracket_opener'] < $arrow))
) {
for ($lastNonEmpty = ($scopeCloser - 1); $lastNonEmpty > $arrow; $lastNonEmpty--) {
if (isset(Util\Tokens::$emptyTokens[$this->tokens[$lastNonEmpty]['code']]) === false) {
Expand Down
5 changes: 5 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ $a = [
'a' => fn() => return 1,
];

/* testArrayValueNoTrailingComma */
$a = [
'a' => fn() => foo()
];

/* testYield */
$a = fn($x) => yield 'k' => $x;

Expand Down
16 changes: 16 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,22 @@ public function testArrayValue()
}//end testArrayValue()


/**
* Test arrow functions that are used as array values with no trailing comma.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testArrayValueNoTrailingComma()
{
$token = $this->getTargetToken('/* testArrayValueNoTrailingComma */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 4, 8, 'closing parenthesis');

}//end testArrayValueNoTrailingComma()


/**
* Test arrow functions that use the yield keyword.
*
Expand Down

0 comments on commit 3964931

Please sign in to comment.