Skip to content

Commit 98c1ce8

Browse files
committed
Squiz/FunctionDeclarationArgumentSpacing: handle modifiers for constructor property promotion
The spacing after visibility/`readonly` modifiers for constructor property promotion were so far not checked by this sniff. While the `Squiz.WhiteSpace.ScopeKeywordSpacing` sniff will already handle this, that sniff may not be in use in all standards using this sniff. As things were, this sniff was just no longer feature complete for the task this sniff is supposed to handle: spacing of function declaration arguments. This commit adds handling the spacing after modifiers used for constructor property promotion to this sniff. The spacing requirements are aligned with the spacing expectations of the `Squiz.WhiteSpace.ScopeKeywordSpacing` sniff, so the sniffs should not conflict with each other. Additionally, the new checks in this sniff have dedicated error codes, which means that - if there would be a conflict anywhere - the modifier spacing checks within this sniff can easily be turned off. Includes tests.
1 parent e99e983 commit 98c1ce8

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,71 @@ public function processBracket($phpcsFile, $openBracket)
306306
}//end if
307307
}//end if
308308

309+
if (isset($param['visibility_token']) === true && $param['visibility_token'] !== false) {
310+
$visibilityToken = $param['visibility_token'];
311+
$afterVisibilityToken = $phpcsFile->findNext(T_WHITESPACE, ($visibilityToken + 1), $param['token'], true);
312+
313+
$spacesAfter = 0;
314+
if ($afterVisibilityToken !== false
315+
&& $tokens[$visibilityToken]['line'] !== $tokens[$afterVisibilityToken]['line']
316+
) {
317+
$spacesAfter = 'newline';
318+
} else if ($tokens[($visibilityToken + 1)]['code'] === T_WHITESPACE) {
319+
$spacesAfter = $tokens[($visibilityToken + 1)]['length'];
320+
}
321+
322+
if ($spacesAfter !== 1) {
323+
$error = 'Expected 1 space after visibility modifier "%s"; %s found';
324+
$data = [
325+
$tokens[$visibilityToken]['content'],
326+
$spacesAfter,
327+
];
328+
329+
$fix = $phpcsFile->addFixableError($error, $visibilityToken, 'SpacingAfterVisbility', $data);
330+
if ($fix === true) {
331+
$phpcsFile->fixer->beginChangeset();
332+
$phpcsFile->fixer->addContent($visibilityToken, ' ');
333+
334+
for ($i = ($visibilityToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) {
335+
$phpcsFile->fixer->replaceToken($i, '');
336+
}
337+
338+
$phpcsFile->fixer->endChangeset();
339+
}
340+
}//end if
341+
}//end if
342+
343+
if (isset($param['readonly_token']) === true) {
344+
$readonlyToken = $param['readonly_token'];
345+
$afterReadonlyToken = $phpcsFile->findNext(T_WHITESPACE, ($readonlyToken + 1), $param['token'], true);
346+
347+
$spacesAfter = 0;
348+
if ($afterReadonlyToken !== false
349+
&& $tokens[$readonlyToken]['line'] !== $tokens[$afterReadonlyToken]['line']
350+
) {
351+
$spacesAfter = 'newline';
352+
} else if ($tokens[($readonlyToken + 1)]['code'] === T_WHITESPACE) {
353+
$spacesAfter = $tokens[($readonlyToken + 1)]['length'];
354+
}
355+
356+
if ($spacesAfter !== 1) {
357+
$error = 'Expected 1 space after readonly modifier; %s found';
358+
$data = [$spacesAfter];
359+
360+
$fix = $phpcsFile->addFixableError($error, $readonlyToken, 'SpacingAfterReadonly', $data);
361+
if ($fix === true) {
362+
$phpcsFile->fixer->beginChangeset();
363+
$phpcsFile->fixer->addContent($readonlyToken, ' ');
364+
365+
for ($i = ($readonlyToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) {
366+
$phpcsFile->fixer->replaceToken($i, '');
367+
}
368+
369+
$phpcsFile->fixer->endChangeset();
370+
}
371+
}//end if
372+
}//end if
373+
309374
$commaToken = false;
310375
if ($paramNumber > 0 && $params[($paramNumber - 1)]['comma_token'] !== false) {
311376
$commaToken = $params[($paramNumber - 1)]['comma_token'];

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,14 @@ function newlineBeforeCommaFixerRespectsComments(
199199
class PropertyPromotionSpacingAfterComma {
200200
public function __construct(private string|int $propA, protected bool $correctSpace, public MyClass $tooMuchSpace,readonly string $noSpace) {}
201201
}
202+
203+
class PropertyPromotionSpacingAfterModifier {
204+
public function __construct(
205+
private$noSpace,
206+
public MyClass $tooMuchSpace,
207+
protected readonly string $tooMuchSpaceX2,
208+
readonly
209+
public
210+
string $tooMuchSpaceNewLines,
211+
) {}
212+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,12 @@ function newlineBeforeCommaFixerRespectsComments(
175175
class PropertyPromotionSpacingAfterComma {
176176
public function __construct(private string|int $propA, protected bool $correctSpace, public MyClass $tooMuchSpace, readonly string $noSpace) {}
177177
}
178+
179+
class PropertyPromotionSpacingAfterModifier {
180+
public function __construct(
181+
private $noSpace,
182+
public MyClass $tooMuchSpace,
183+
protected readonly string $tooMuchSpaceX2,
184+
readonly public string $tooMuchSpaceNewLines,
185+
) {}
186+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public function getErrorList($testFile='')
9090
195 => 1,
9191
196 => 1,
9292
200 => 2,
93+
205 => 1,
94+
206 => 1,
95+
207 => 2,
96+
208 => 1,
97+
209 => 1,
9398
];
9499

95100
default:

0 commit comments

Comments
 (0)