Skip to content

Commit d84cfcf

Browse files
jrfnlgsherwood
andcommitted
FunctionDeclarations::getArrowFunctionOpenClose(): 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 66a48e9 commit d84cfcf

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

PHPCSUtils/Utils/FunctionDeclarations.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr)
676676

677677
if ($tokens[$stackPtr]['type'] === 'T_FN'
678678
&& isset($tokens[$stackPtr]['scope_closer']) === true
679+
&& \version_compare(Helper::getVersion(), '3.5.4', '>') === true
679680
) {
680681
// The keys will either all be set or none will be set, so no additional checks needed.
681682
return [
@@ -736,12 +737,20 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr)
736737

737738
$returnValue['scope_opener'] = $arrow;
738739
$inTernary = false;
740+
$lastEndToken = null;
739741

740742
for ($scopeCloser = ($arrow + 1); $scopeCloser < $phpcsFile->numTokens; $scopeCloser++) {
741743
if (isset(self::$arrowFunctionEndTokens[$tokens[$scopeCloser]['code']]) === true
742744
// BC for misidentified ternary else in some PHPCS versions.
743745
&& ($tokens[$scopeCloser]['code'] !== \T_COLON || $inTernary === false)
744746
) {
747+
if ($lastEndToken !== null
748+
&& $tokens[$scopeCloser]['code'] === \T_CLOSE_PARENTHESIS
749+
&& $tokens[$scopeCloser]['parenthesis_opener'] < $arrow
750+
) {
751+
$scopeCloser = $lastEndToken;
752+
}
753+
745754
break;
746755
}
747756

@@ -763,12 +772,14 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr)
763772
}
764773

765774
if (isset($tokens[$scopeCloser]['parenthesis_closer']) === true) {
766-
$scopeCloser = $tokens[$scopeCloser]['parenthesis_closer'];
775+
$scopeCloser = $tokens[$scopeCloser]['parenthesis_closer'];
776+
$lastEndToken = $scopeCloser;
767777
continue;
768778
}
769779

770780
if (isset($tokens[$scopeCloser]['bracket_closer']) === true) {
771-
$scopeCloser = $tokens[$scopeCloser]['bracket_closer'];
781+
$scopeCloser = $tokens[$scopeCloser]['bracket_closer'];
782+
$lastEndToken = $scopeCloser;
772783
continue;
773784
}
774785

Tests/Utils/FunctionDeclarations/IsArrowFunctionTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ array_map(
8888
/* testTernary */
8989
$fn = fn($a) => $a ? /* testTernaryThen */ fn() : string => 'a' : /* testTernaryElse */ fn() : string => 'b';
9090

91+
$foo = foo(
92+
/* testArrowFunctionAsArgument */
93+
fn() => bar()
94+
);
95+
96+
$foo = foo(
97+
/* testArrowFunctionWithArrayAsArgument */
98+
fn() => [$row[0], $row[3]]
99+
);
100+
91101
/* testConstantDeclaration */
92102
const FN = 'a';
93103

Tests/Utils/FunctionDeclarations/IsArrowFunctionTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,30 @@ public function dataArrowFunction()
456456
],
457457
],
458458
],
459+
'arrow-function-as-function-call-argument' => [
460+
'/* testArrowFunctionAsArgument */',
461+
[
462+
'is' => true,
463+
'get' => [
464+
'parenthesis_opener' => 1,
465+
'parenthesis_closer' => 2,
466+
'scope_opener' => 4,
467+
'scope_closer' => 8,
468+
],
469+
],
470+
],
471+
'arrow-function-as-function-call-argument-with-array-return' => [
472+
'/* testArrowFunctionWithArrayAsArgument */',
473+
[
474+
'is' => true,
475+
'get' => [
476+
'parenthesis_opener' => 1,
477+
'parenthesis_closer' => 2,
478+
'scope_opener' => 4,
479+
'scope_closer' => 17,
480+
],
481+
],
482+
],
459483
'arrow-function-nested-in-method' => [
460484
'/* testNestedInMethod */',
461485
[

0 commit comments

Comments
 (0)