Skip to content

Commit e99e983

Browse files
committed
Squiz/FunctionDeclarationArgumentSpacing: special case "spacing after comma" vs constructor property promotion
While incorrect spacing after a comma for constructor property promotion parameters would already be flagged and fixed by the sniff, the error message and code were incorrect/unclear. Given the following test code: ```php class PropertyPromotionSpacingAfterComma { public function __construct(private string|int $propA, protected bool $correctSpace, public MyClass $tooMuchSpace, readonly string $noSpace) {} } ``` Previously the following would be reported: ``` 198 | ERROR | [x] Expected 1 space between comma and type hint "MyClass"; 2 found | | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeHint) 198 | ERROR | [x] Expected 1 space between comma and type hint "string"; 0 found | | (Squiz.Functions.FunctionDeclarationArgumentSpacing.NoSpaceBeforeHint) ``` Take note of the "type hint" in the message and the "Hint" suffix for the error codes. With the fix from this commit in place, this will now be reported as follows: ``` 198 | ERROR | [x] Expected 1 space between comma and property modifier "public"; 2 found | | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforePropertyModifier) 198 | ERROR | [x] Expected 1 space between comma and property modifier "readonly"; 0 found | | (Squiz.Functions.FunctionDeclarationArgumentSpacing.NoSpaceBeforePropertyModifier) ``` Includes tests.
1 parent 5828d26 commit e99e983

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,12 @@ public function processBracket($phpcsFile, $openBracket)
374374
$typeOfNextShort = 'Arg';
375375
$contentOfNext = $param['name'];
376376

377-
if ($param['type_hint_token'] !== false) {
377+
if (isset($param['property_visibility']) === true) {
378+
$typeOfNext = 'property modifier';
379+
$typeOfNextShort = 'PropertyModifier';
380+
$modifier = $phpcsFile->findNext(Tokens::$emptyTokens, ($commaToken + 1), $param['token'], true);
381+
$contentOfNext = $tokens[$modifier]['content'];
382+
} else if ($param['type_hint_token'] !== false) {
378383
$typeOfNext = 'type hint';
379384
$typeOfNextShort = 'Hint';
380385
$contentOfNext = $param['type_hint'];

src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,7 @@ function newlineBeforeCommaFixerRespectsComments(
195195
, $paramC=30
196196
, string $paramC='foo'
197197
) {}
198+
199+
class PropertyPromotionSpacingAfterComma {
200+
public function __construct(private string|int $propA, protected bool $correctSpace, public MyClass $tooMuchSpace,readonly string $noSpace) {}
201+
}

src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,7 @@ function newlineBeforeCommaFixerRespectsComments(
171171
$paramC=30,
172172
string $paramC='foo'
173173
) {}
174+
175+
class PropertyPromotionSpacingAfterComma {
176+
public function __construct(private string|int $propA, protected bool $correctSpace, public MyClass $tooMuchSpace, readonly string $noSpace) {}
177+
}

src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function getErrorList($testFile='')
8989
193 => 1,
9090
195 => 1,
9191
196 => 1,
92+
200 => 2,
9293
];
9394

9495
default:

0 commit comments

Comments
 (0)