diff --git a/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php b/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php index 1ae4822569..bf93c55338 100644 --- a/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php +++ b/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php @@ -109,17 +109,25 @@ public function checkSpacing(File $phpcsFile, $stackPtr, $openBracket) $find = [ T_COMMA, T_CLOSURE, + T_FN, T_ANON_CLASS, T_OPEN_SHORT_ARRAY, + T_MATCH, ]; while (($nextSeparator = $phpcsFile->findNext($find, ($nextSeparator + 1), $closeBracket)) !== false) { if ($tokens[$nextSeparator]['code'] === T_CLOSURE || $tokens[$nextSeparator]['code'] === T_ANON_CLASS + || $tokens[$nextSeparator]['code'] === T_MATCH ) { - // Skip closures. + // Skip closures, anon class declarations and match control structures. $nextSeparator = $tokens[$nextSeparator]['scope_closer']; continue; + } else if ($tokens[$nextSeparator]['code'] === T_FN) { + // Skip arrow functions, but don't skip the arrow function closer as it is likely to + // be the comma separating it from the next function call argument (or the parenthesis closer). + $nextSeparator = ($tokens[$nextSeparator]['scope_closer'] - 1); + continue; } else if ($tokens[$nextSeparator]['code'] === T_OPEN_SHORT_ARRAY) { // Skips arrays using short notation. $nextSeparator = $tokens[$nextSeparator]['bracket_closer']; diff --git a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.1.inc b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.1.inc index 29ef802d55..393ee10b66 100644 --- a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.1.inc +++ b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.1.inc @@ -177,3 +177,23 @@ $foo = new MyClass( #[AttributeName(1,2)] $callable = myCallable(...); + +// Skip over PHP 7.4 arrow functions. +// While any commas belonging to the code within the arrow function would always need to be within parentheses +// or within a short array, so there aren't any false positives, the sniff also does not need to examine these, +// so will be more efficient skipping over arrow functions. +$foobar = functionCallFnParamA( + fn ($foo,$bar) => [1,2,3], + $args, +); + +$foobar = functionCallFnParamB(fn ($foo,$bar) => [1,2,3] ,$args); +$foobar = functionCallFnParamC($args, fn ($foo,$bar) => [1,2,3] , ); + +// Ignore spacing within PHP 8.0 match control structures, which may have their own rules. +$foobar = functionCallMatchParam( + match($foo) { + 1,2,3 => 'something',4,5,6 => 'else',default => 'works' + } , // But check the spacing again once the match expression has finished. + $args +); diff --git a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.1.inc.fixed b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.1.inc.fixed index 3f0cb0fca6..a50f695e45 100644 --- a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.1.inc.fixed +++ b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.1.inc.fixed @@ -177,3 +177,23 @@ $foo = new MyClass( #[AttributeName(1, 2)] $callable = myCallable(...); + +// Skip over PHP 7.4 arrow functions. +// While any commas belonging to the code within the arrow function would always need to be within parentheses +// or within a short array, so there aren't any false positives, the sniff also does not need to examine these, +// so will be more efficient skipping over arrow functions. +$foobar = functionCallFnParamA( + fn ($foo,$bar) => [1,2,3], + $args, +); + +$foobar = functionCallFnParamB(fn ($foo,$bar) => [1,2,3], $args); +$foobar = functionCallFnParamC($args, fn ($foo,$bar) => [1,2,3], ); + +// Ignore spacing within PHP 8.0 match control structures, which may have their own rules. +$foobar = functionCallMatchParam( + match($foo) { + 1,2,3 => 'something',4,5,6 => 'else',default => 'works' + }, // But check the spacing again once the match expression has finished. + $args +); diff --git a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php index 7088428772..35bf11cb72 100644 --- a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php +++ b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php @@ -66,6 +66,9 @@ public function getErrorList($testFile='') 162 => 2, 170 => 1, 177 => 1, + 190 => 2, + 191 => 2, + 197 => 1, ]; default: