Skip to content

Commit 2f09ce7

Browse files
jrfnlgsherwood
authored andcommitted
Generic/FunctionCallArgumentSpacing: prevent fixer conflict over PHP 7.3+ trailing comma's in function calls
As of PHP 7.3, trailing comma's in function calls are allowed. This sniff should leave those alone to prevent fixer conflicts with sniffs dealing with the spacing around the parentheses of a function call, like the `PEAR.Functions.FunctionCallSignature` sniff, which this sniff is often combined with. While the sniff did take trailing comma's into account for multi-line function calls, it did not handle them correctly for single-line function calls. Note: too _much_ space can still be removed, but space should be added. Fixed now. Includes tests.
1 parent afe2026 commit 2f09ce7

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,13 @@ public function checkSpacing(File $phpcsFile, $stackPtr, $openBracket)
156156
}//end if
157157

158158
if ($tokens[($nextSeparator + 1)]['code'] !== T_WHITESPACE) {
159-
$error = 'No space found after comma in argument list';
160-
$fix = $phpcsFile->addFixableError($error, $nextSeparator, 'NoSpaceAfterComma');
161-
if ($fix === true) {
162-
$phpcsFile->fixer->addContent($nextSeparator, ' ');
159+
// Ignore trailing comma's after last argument as that's outside the scope of this sniff.
160+
if (($nextSeparator + 1) !== $closeBracket) {
161+
$error = 'No space found after comma in argument list';
162+
$fix = $phpcsFile->addFixableError($error, $nextSeparator, 'NoSpaceAfterComma');
163+
if ($fix === true) {
164+
$phpcsFile->fixer->addContent($nextSeparator, ' ');
165+
}
163166
}
164167
} else {
165168
// If there is a newline in the space, then they must be formatting

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

+10
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,13 @@ class Testing extends Bar
162162
$a = new parent($foo ,$bar);
163163
}
164164
}
165+
166+
// Ignore spacing after PHP 7.3+ trailing comma in single-line function calls to prevent fixer conflicts.
167+
// This is something which should be decided by a sniff dealing with the function call parentheses.
168+
$foo = new MyClass($obj, 'getMethod',);
169+
$foo = new MyClass($obj, 'getMethod', );
170+
$foo = new MyClass($obj, 'getMethod', );
171+
$foo = new MyClass(
172+
$obj,
173+
'getMethod',
174+
);

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

+10
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,13 @@ class Testing extends Bar
162162
$a = new parent($foo, $bar);
163163
}
164164
}
165+
166+
// Ignore spacing after PHP 7.3+ trailing comma in single-line function calls to prevent fixer conflicts.
167+
// This is something which should be decided by a sniff dealing with the function call parentheses.
168+
$foo = new MyClass($obj, 'getMethod',);
169+
$foo = new MyClass($obj, 'getMethod', );
170+
$foo = new MyClass($obj, 'getMethod', );
171+
$foo = new MyClass(
172+
$obj,
173+
'getMethod',
174+
);

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

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function getErrorList()
5555
154 => 2,
5656
155 => 1,
5757
162 => 2,
58+
170 => 1,
5859
];
5960

6061
}//end getErrorList()

0 commit comments

Comments
 (0)