Skip to content

Commit 03428fd

Browse files
committed
Generic/FunctionCallArgumentSpacing: efficiency fix - skip over arrow functions
PHP 7.4 introduced arrow functions. While arrow functions being passed as an argument in a function call will not lead to false positives for this sniff - at least, I haven't been able to come up with a code sample in which it would [^1] -, skipping over them is still beneficial as it prevents unnecessary token walking. Mind: the `scope_closer` of the arrow function _may_ be the comma separating the arrow function argument from the next function call argument, so we need to step one token back to prevent false negatives on those comma's. Either way, this is now handled. Includes unit tests. [^1]: comma's in arrow functions will always be nested within parentheses, within a short array or within a nested closure or anonymous class, all of which the sniff already ignores.
1 parent 0c6c929 commit 03428fd

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function checkSpacing(File $phpcsFile, $stackPtr, $openBracket)
109109
$find = [
110110
T_COMMA,
111111
T_CLOSURE,
112+
T_FN,
112113
T_ANON_CLASS,
113114
T_OPEN_SHORT_ARRAY,
114115
];
@@ -120,6 +121,11 @@ public function checkSpacing(File $phpcsFile, $stackPtr, $openBracket)
120121
// Skip closures.
121122
$nextSeparator = $tokens[$nextSeparator]['scope_closer'];
122123
continue;
124+
} else if ($tokens[$nextSeparator]['code'] === T_FN) {
125+
// Skip arrow functions, but don't skip the arrow function closer as it is likely to
126+
// be the comma separating it from the next function call argument (or the parenthesis closer).
127+
$nextSeparator = ($tokens[$nextSeparator]['scope_closer'] - 1);
128+
continue;
123129
} else if ($tokens[$nextSeparator]['code'] === T_OPEN_SHORT_ARRAY) {
124130
// Skips arrays using short notation.
125131
$nextSeparator = $tokens[$nextSeparator]['bracket_closer'];

src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.1.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,15 @@ $foo = new MyClass(
177177
#[AttributeName(1,2)]
178178

179179
$callable = myCallable(...);
180+
181+
// Skip over PHP 7.4 arrow functions.
182+
// While any commas belonging to the code within the arrow function would always need to be within parentheses
183+
// or within a short array, so there aren't any false positives, the sniff also does not need to examine these,
184+
// so will be more efficient skipping over arrow functions.
185+
$foobar = functionCallFnParamA(
186+
fn ($foo,$bar) => [1,2,3],
187+
$args,
188+
);
189+
190+
$foobar = functionCallFnParamB(fn ($foo,$bar) => [1,2,3] ,$args);
191+
$foobar = functionCallFnParamC($args, fn ($foo,$bar) => [1,2,3] , );

src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.1.inc.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,15 @@ $foo = new MyClass(
177177
#[AttributeName(1, 2)]
178178

179179
$callable = myCallable(...);
180+
181+
// Skip over PHP 7.4 arrow functions.
182+
// While any commas belonging to the code within the arrow function would always need to be within parentheses
183+
// or within a short array, so there aren't any false positives, the sniff also does not need to examine these,
184+
// so will be more efficient skipping over arrow functions.
185+
$foobar = functionCallFnParamA(
186+
fn ($foo,$bar) => [1,2,3],
187+
$args,
188+
);
189+
190+
$foobar = functionCallFnParamB(fn ($foo,$bar) => [1,2,3], $args);
191+
$foobar = functionCallFnParamC($args, fn ($foo,$bar) => [1,2,3], );

src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public function getErrorList($testFile='')
6666
162 => 2,
6767
170 => 1,
6868
177 => 1,
69+
190 => 2,
70+
191 => 2,
6971
];
7072

7173
default:

0 commit comments

Comments
 (0)