Skip to content

Commit 26509fd

Browse files
jrfnlgsherwood
andcommitted
BCFile::findEndOfStatement(): arrow functions as function argument
Allow for arrow functions being used as function argument as per upstream commit squizlabs/PHP_CodeSniffer/commit/2914011675cd13f8a7d5eae16ed1d5ee4f0e701d which is included in PHPCS 3.5.5. Also see: squizlabs/PHP_CodeSniffer 2895 and squizlabs/PHP_CodeSniffer 2523 Co-authored-by: Greg Sherwood <[email protected]>
1 parent d84cfcf commit 26509fd

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

PHPCSUtils/BackCompat/BCFile.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ public static function findStartOfStatement(File $phpcsFile, $start, $ignore = n
12511251
* - PHPCS 3.5.0: Improved handling of group use statements.
12521252
* - PHPCS 3.5.3: Added support for PHP 7.4 T_FN arrow functions.
12531253
* - PHPCS 3.5.4: Improved support for PHP 7.4 T_FN arrow functions.
1254+
* - PHPCS 3.5.5: Improved support for PHP 7.4 T_FN arrow functions, PHPCS #2895.
12541255
*
12551256
* @see \PHP_CodeSniffer\Files\File::findEndOfStatement() Original source.
12561257
*
@@ -1288,7 +1289,6 @@ public static function findEndOfStatement(File $phpcsFile, $start, $ignore = nul
12881289
}
12891290

12901291
$lastNotEmpty = $start;
1291-
12921292
for ($i = $start; $i < $phpcsFile->numTokens; $i++) {
12931293
if ($i !== $start && isset($endTokens[$tokens[$i]['code']]) === true) {
12941294
// Found the end of the statement.
@@ -1311,6 +1311,8 @@ public static function findEndOfStatement(File $phpcsFile, $start, $ignore = nul
13111311
|| $i === $tokens[$i]['scope_condition'])
13121312
) {
13131313
if ($tokens[$i]['type'] === 'T_FN') {
1314+
$lastNotEmpty = $tokens[$i]['scope_closer'];
1315+
13141316
// Minus 1 as the closer can be shared.
13151317
$i = ($tokens[$i]['scope_closer'] - 1);
13161318
continue;
@@ -1342,8 +1344,11 @@ public static function findEndOfStatement(File $phpcsFile, $start, $ignore = nul
13421344
return $arrowFunctionOpenClose['scope_closer'];
13431345
}
13441346

1347+
$lastNotEmpty = $arrowFunctionOpenClose['scope_closer'];
1348+
13451349
// Minus 1 as the closer can be shared.
13461350
$i = ($arrowFunctionOpenClose['scope_closer'] - 1);
1351+
continue;
13471352
}
13481353
}
13491354

Tests/BackCompat/BCFile/FindEndOfStatementTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,14 @@ static fn ($a) => $a;
4242
/* testArrowFunctionReturnValue */
4343
fn(): array => [a($a, $b)];
4444

45+
$foo = foo(
46+
/* testArrowFunctionAsArgument */
47+
fn() => bar()
48+
);
49+
50+
$foo = foo(
51+
/* testArrowFunctionWithArrayAsArgument */
52+
fn() => [$row[0], $row[3]]
53+
);
54+
4555
return 0;

Tests/BackCompat/BCFile/FindEndOfStatementTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,33 @@ public function testArrowFunctionReturnValue()
207207

208208
$this->assertSame(($start + 18), $found);
209209
}
210+
211+
/**
212+
* Test arrow function used as a function argument.
213+
*
214+
* @return void
215+
*/
216+
public function testArrowFunctionAsArgument()
217+
{
218+
$start = $this->getTargetToken('/* testArrowFunctionAsArgument */', Collections::arrowFunctionTokensBC());
219+
$found = BCFile::findEndOfStatement(self::$phpcsFile, $start);
220+
221+
$this->assertSame(($start + 8), $found);
222+
}
223+
224+
/**
225+
* Test arrow function with arrays used as a function argument.
226+
*
227+
* @return void
228+
*/
229+
public function testArrowFunctionWithArrayAsArgument()
230+
{
231+
$start = $this->getTargetToken(
232+
'/* testArrowFunctionWithArrayAsArgument */',
233+
Collections::arrowFunctionTokensBC()
234+
);
235+
$found = BCFile::findEndOfStatement(self::$phpcsFile, $start);
236+
237+
$this->assertSame(($start + 17), $found);
238+
}
210239
}

0 commit comments

Comments
 (0)