Skip to content

Commit 9fa77a2

Browse files
committed
Squiz/FunctionDeclarationArgumentSpacing: handle asym modifiers for constructor property promotion
This commit adds handling of the spacing after asymmetric visibility 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 check has a dedicated error code, which means that - if there would be a conflict anywhere - the asym visibility spacing check within this sniff can easily be turned off. Includes tests.
1 parent 2e9da03 commit 9fa77a2

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,40 @@ public function processBracket($phpcsFile, $openBracket)
340340
}//end if
341341
}//end if
342342

343+
if (isset($param['set_visibility_token']) === true && $param['set_visibility_token'] !== false) {
344+
$visibilityToken = $param['set_visibility_token'];
345+
$afterVisibilityToken = $phpcsFile->findNext(T_WHITESPACE, ($visibilityToken + 1), $param['token'], true);
346+
347+
$spacesAfter = 0;
348+
if ($afterVisibilityToken !== false
349+
&& $tokens[$visibilityToken]['line'] !== $tokens[$afterVisibilityToken]['line']
350+
) {
351+
$spacesAfter = 'newline';
352+
} else if ($tokens[($visibilityToken + 1)]['code'] === T_WHITESPACE) {
353+
$spacesAfter = $tokens[($visibilityToken + 1)]['length'];
354+
}
355+
356+
if ($spacesAfter !== 1) {
357+
$error = 'Expected 1 space after set-visibility modifier "%s"; %s found';
358+
$data = [
359+
$tokens[$visibilityToken]['content'],
360+
$spacesAfter,
361+
];
362+
363+
$fix = $phpcsFile->addFixableError($error, $visibilityToken, 'SpacingAfterSetVisbility', $data);
364+
if ($fix === true) {
365+
$phpcsFile->fixer->beginChangeset();
366+
$phpcsFile->fixer->addContent($visibilityToken, ' ');
367+
368+
for ($i = ($visibilityToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) {
369+
$phpcsFile->fixer->replaceToken($i, '');
370+
}
371+
372+
$phpcsFile->fixer->endChangeset();
373+
}
374+
}//end if
375+
}//end if
376+
343377
if (isset($param['readonly_token']) === true) {
344378
$readonlyToken = $param['readonly_token'];
345379
$afterReadonlyToken = $phpcsFile->findNext(T_WHITESPACE, ($readonlyToken + 1), $param['token'], true);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,18 @@ class PropertyPromotionSpacingAfterModifier {
210210
string $tooMuchSpaceNewLines,
211211
) {}
212212
}
213+
214+
class AsymVisibilityPropertyPromotionSpacingAfterComma {
215+
public function __construct(private(set) string|int $propA, protected(set) bool $correctSpace, public(set) MyClass $tooMuchSpace,public(set) string $noSpace) {}
216+
}
217+
218+
class AsymVisibilityPropertyPromotionSpacingAfterModifier {
219+
public function __construct(
220+
private(set)$noSpace,
221+
public(set) MyClass $tooMuchSpace,
222+
protected(set) public string $tooMuchSpaceX2,
223+
private
224+
public(set)
225+
string $tooMuchSpaceNewLines,
226+
) {}
227+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,16 @@ class PropertyPromotionSpacingAfterModifier {
184184
readonly public string $tooMuchSpaceNewLines,
185185
) {}
186186
}
187+
188+
class AsymVisibilityPropertyPromotionSpacingAfterComma {
189+
public function __construct(private(set) string|int $propA, protected(set) bool $correctSpace, public(set) MyClass $tooMuchSpace, public(set) string $noSpace) {}
190+
}
191+
192+
class AsymVisibilityPropertyPromotionSpacingAfterModifier {
193+
public function __construct(
194+
private(set) $noSpace,
195+
public(set) MyClass $tooMuchSpace,
196+
protected(set) public string $tooMuchSpaceX2,
197+
private public(set) string $tooMuchSpaceNewLines,
198+
) {}
199+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ public function getErrorList($testFile='')
9595
207 => 2,
9696
208 => 1,
9797
209 => 1,
98+
215 => 2,
99+
220 => 1,
100+
221 => 1,
101+
222 => 2,
102+
223 => 1,
103+
224 => 1,
98104
];
99105

100106
default:

0 commit comments

Comments
 (0)