Skip to content

Generic/FunctionCallArgumentSpacing: bug fix - ignore commas in nested match structures + minor efficiency tweak #513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public function getErrorList($testFile='')
162 => 2,
170 => 1,
177 => 1,
190 => 2,
191 => 2,
197 => 1,
];

default:
Expand Down