Skip to content

Commit 57a9ed7

Browse files
committed
Generic/FunctionCallArgumentSpacing: bug fix - ignore commas in nested 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.
1 parent 03428fd commit 57a9ed7

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@ public function checkSpacing(File $phpcsFile, $stackPtr, $openBracket)
112112
T_FN,
113113
T_ANON_CLASS,
114114
T_OPEN_SHORT_ARRAY,
115+
T_MATCH,
115116
];
116117

117118
while (($nextSeparator = $phpcsFile->findNext($find, ($nextSeparator + 1), $closeBracket)) !== false) {
118119
if ($tokens[$nextSeparator]['code'] === T_CLOSURE
119120
|| $tokens[$nextSeparator]['code'] === T_ANON_CLASS
121+
|| $tokens[$nextSeparator]['code'] === T_MATCH
120122
) {
121-
// Skip closures.
123+
// Skip closures, anon class declarations and match control structures.
122124
$nextSeparator = $tokens[$nextSeparator]['scope_closer'];
123125
continue;
124126
} else if ($tokens[$nextSeparator]['code'] === T_FN) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,11 @@ $foobar = functionCallFnParamA(
189189

190190
$foobar = functionCallFnParamB(fn ($foo,$bar) => [1,2,3] ,$args);
191191
$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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,11 @@ $foobar = functionCallFnParamA(
189189

190190
$foobar = functionCallFnParamB(fn ($foo,$bar) => [1,2,3], $args);
191191
$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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function getErrorList($testFile='')
6868
177 => 1,
6969
190 => 2,
7070
191 => 2,
71+
197 => 1,
7172
];
7273

7374
default:

0 commit comments

Comments
 (0)