Skip to content

Commit

Permalink
Merge pull request PHPCSStandards#513 from PHPCSStandards/feature/gen…
Browse files Browse the repository at this point in the history
…eric-functioncallargument-spacing-bugfix-effciency-fix

Generic/FunctionCallArgumentSpacing: bug fix - ignore commas in nested match structures + minor efficiency tweak
  • Loading branch information
jrfnl committed Jun 2, 2024
2 parents 0c6c929 + 57a9ed7 commit 1114d35
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
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

0 comments on commit 1114d35

Please sign in to comment.