Skip to content

Commit

Permalink
Generic/FunctionCallArgumentSpacing: bug fix - ignore commas in neste…
Browse files Browse the repository at this point in the history
…d match structures

PHP 8.0 introduced match control structures, which can be passed in a function call (though probably/hopefully this is not very common as it makes for hard to comprehend code).

The comma's within match control structures should be checked by a sniff which handled that control structure and should not be treated as comma's belonging to the function call.

As things are, this is currently not the case, which leads to false positives.

Fixed now.

Includes test.
  • Loading branch information
jrfnl committed Jun 2, 2024
1 parent 207ec5e commit 40298d9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ public function checkSpacing(File $phpcsFile, $stackPtr, $openBracket)
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,11 @@ $foobar = functionCallFnParamA(

$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 @@ -189,3 +189,11 @@ $foobar = functionCallFnParamA(

$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 @@ -68,6 +68,7 @@ public function getErrorList($testFile='')
177 => 1,
190 => 2,
191 => 2,
197 => 1,
];

default:
Expand Down

0 comments on commit 40298d9

Please sign in to comment.