Skip to content

Commit 3964931

Browse files
committed
Fixed bug #3362 : Generic.WhiteSpace.ScopeIndent false positive for arrow functions inside arrays
1 parent 9633c8a commit 3964931

File tree

9 files changed

+66
-6
lines changed

9 files changed

+66
-6
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
6767
-- Thanks to Juliette Reinders Folmer for the patch
6868
- Fixed bug #3357 : Generic.Functions.OpeningFunctionBraceBsdAllman removes return type when additional lines are present
6969
-- Thanks to Juliette Reinders Folmer for the patch
70+
- Fixed bug #3362 : Generic.WhiteSpace.ScopeIndent false positive for arrow functions inside arrays
7071
</notes>
7172
<contents>
7273
<dir name="/">

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,15 @@ $foo = match ($type) {
15471547
],
15481548
};
15491549

1550+
$a = [
1551+
'a' => [
1552+
'a' => fn () => foo()
1553+
],
1554+
'a' => [
1555+
'a' => 'a',
1556+
]
1557+
];
1558+
15501559
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15511560
?>
15521561

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,15 @@ $foo = match ($type) {
15471547
],
15481548
};
15491549

1550+
$a = [
1551+
'a' => [
1552+
'a' => fn () => foo()
1553+
],
1554+
'a' => [
1555+
'a' => 'a',
1556+
]
1557+
];
1558+
15501559
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15511560
?>
15521561

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,15 @@ $foo = match ($type) {
15471547
],
15481548
};
15491549

1550+
$a = [
1551+
'a' => [
1552+
'a' => fn () => foo()
1553+
],
1554+
'a' => [
1555+
'a' => 'a',
1556+
]
1557+
];
1558+
15501559
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15511560
?>
15521561

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,15 @@ $foo = match ($type) {
15471547
],
15481548
};
15491549

1550+
$a = [
1551+
'a' => [
1552+
'a' => fn () => foo()
1553+
],
1554+
'a' => [
1555+
'a' => 'a',
1556+
]
1557+
];
1558+
15501559
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15511560
?>
15521561

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
187187
1527 => 1,
188188
1529 => 1,
189189
1530 => 1,
190-
1558 => 1,
191-
1559 => 1,
192-
1560 => 1,
193-
1561 => 1,
190+
1567 => 1,
191+
1568 => 1,
192+
1569 => 1,
193+
1570 => 1,
194194
];
195195

196196
}//end getErrorList()

src/Tokenizers/PHP.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,8 +2344,10 @@ protected function processAdditional()
23442344

23452345
if (isset($endTokens[$this->tokens[$scopeCloser]['code']]) === true) {
23462346
if ($lastEndToken !== null
2347-
&& $this->tokens[$scopeCloser]['code'] === T_CLOSE_PARENTHESIS
2348-
&& $this->tokens[$scopeCloser]['parenthesis_opener'] < $arrow
2347+
&& ((isset($this->tokens[$scopeCloser]['parenthesis_opener']) === true
2348+
&& $this->tokens[$scopeCloser]['parenthesis_opener'] < $arrow)
2349+
|| (isset($this->tokens[$scopeCloser]['bracket_opener']) === true
2350+
&& $this->tokens[$scopeCloser]['bracket_opener'] < $arrow))
23492351
) {
23502352
for ($lastNonEmpty = ($scopeCloser - 1); $lastNonEmpty > $arrow; $lastNonEmpty--) {
23512353
if (isset(Util\Tokens::$emptyTokens[$this->tokens[$lastNonEmpty]['code']]) === false) {

tests/Core/Tokenizer/BackfillFnTokenTest.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ $a = [
6363
'a' => fn() => return 1,
6464
];
6565

66+
/* testArrayValueNoTrailingComma */
67+
$a = [
68+
'a' => fn() => foo()
69+
];
70+
6671
/* testYield */
6772
$a = fn($x) => yield 'k' => $x;
6873

tests/Core/Tokenizer/BackfillFnTokenTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,22 @@ public function testArrayValue()
307307
}//end testArrayValue()
308308

309309

310+
/**
311+
* Test arrow functions that are used as array values with no trailing comma.
312+
*
313+
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
314+
*
315+
* @return void
316+
*/
317+
public function testArrayValueNoTrailingComma()
318+
{
319+
$token = $this->getTargetToken('/* testArrayValueNoTrailingComma */', T_FN);
320+
$this->backfillHelper($token);
321+
$this->scopePositionTestHelper($token, 4, 8, 'closing parenthesis');
322+
323+
}//end testArrayValueNoTrailingComma()
324+
325+
310326
/**
311327
* Test arrow functions that use the yield keyword.
312328
*

0 commit comments

Comments
 (0)