Skip to content

Commit 1114d35

Browse files
authored
Merge pull request #513 from PHPCSStandards/feature/generic-functioncallargument-spacing-bugfix-effciency-fix
Generic/FunctionCallArgumentSpacing: bug fix - ignore commas in nested match structures + minor efficiency tweak
2 parents 0c6c929 + 57a9ed7 commit 1114d35

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,25 @@ 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,
115+
T_MATCH,
114116
];
115117

116118
while (($nextSeparator = $phpcsFile->findNext($find, ($nextSeparator + 1), $closeBracket)) !== false) {
117119
if ($tokens[$nextSeparator]['code'] === T_CLOSURE
118120
|| $tokens[$nextSeparator]['code'] === T_ANON_CLASS
121+
|| $tokens[$nextSeparator]['code'] === T_MATCH
119122
) {
120-
// Skip closures.
123+
// Skip closures, anon class declarations and match control structures.
121124
$nextSeparator = $tokens[$nextSeparator]['scope_closer'];
122125
continue;
126+
} else if ($tokens[$nextSeparator]['code'] === T_FN) {
127+
// Skip arrow functions, but don't skip the arrow function closer as it is likely to
128+
// be the comma separating it from the next function call argument (or the parenthesis closer).
129+
$nextSeparator = ($tokens[$nextSeparator]['scope_closer'] - 1);
130+
continue;
123131
} else if ($tokens[$nextSeparator]['code'] === T_OPEN_SHORT_ARRAY) {
124132
// Skips arrays using short notation.
125133
$nextSeparator = $tokens[$nextSeparator]['bracket_closer'];

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,23 @@ $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] , );
192+
193+
// Ignore spacing within PHP 8.0 match control structures, which may have their own rules.
194+
$foobar = functionCallMatchParam(
195+
match($foo) {
196+
1,2,3 => 'something',4,5,6 => 'else',default => 'works'
197+
} , // But check the spacing again once the match expression has finished.
198+
$args
199+
);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,23 @@ $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], );
192+
193+
// Ignore spacing within PHP 8.0 match control structures, which may have their own rules.
194+
$foobar = functionCallMatchParam(
195+
match($foo) {
196+
1,2,3 => 'something',4,5,6 => 'else',default => 'works'
197+
}, // But check the spacing again once the match expression has finished.
198+
$args
199+
);

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

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

7174
default:

0 commit comments

Comments
 (0)