From 62c11006882436634c868f21e29c4e922f7c2d33 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 10 Jan 2025 10:58:28 +0100 Subject: [PATCH] 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. --- ...unctionDeclarationArgumentSpacingSniff.php | 65 +++++++++++++++++++ ...onDeclarationArgumentSpacingUnitTest.1.inc | 11 ++++ ...arationArgumentSpacingUnitTest.1.inc.fixed | 9 +++ ...tionDeclarationArgumentSpacingUnitTest.php | 5 ++ 4 files changed, 90 insertions(+) diff --git a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php index 19e76fa4f0..26fd541f06 100644 --- a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php @@ -306,6 +306,71 @@ public function processBracket($phpcsFile, $openBracket) }//end if }//end if + if (isset($param['visibility_token']) === true && $param['visibility_token'] !== false) { + $visibilityToken = $param['visibility_token']; + $afterVisibilityToken = $phpcsFile->findNext(T_WHITESPACE, ($visibilityToken + 1), $param['token'], true); + + $spacesAfter = 0; + if ($afterVisibilityToken !== false + && $tokens[$visibilityToken]['line'] !== $tokens[$afterVisibilityToken]['line'] + ) { + $spacesAfter = 'newline'; + } else if ($tokens[($visibilityToken + 1)]['code'] === T_WHITESPACE) { + $spacesAfter = $tokens[($visibilityToken + 1)]['length']; + } + + if ($spacesAfter !== 1) { + $error = 'Expected 1 space after visibility modifier "%s"; %s found'; + $data = [ + $tokens[$visibilityToken]['content'], + $spacesAfter, + ]; + + $fix = $phpcsFile->addFixableError($error, $visibilityToken, 'SpacingAfterVisbility', $data); + if ($fix === true) { + $phpcsFile->fixer->beginChangeset(); + $phpcsFile->fixer->addContent($visibilityToken, ' '); + + for ($i = ($visibilityToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) { + $phpcsFile->fixer->replaceToken($i, ''); + } + + $phpcsFile->fixer->endChangeset(); + } + }//end if + }//end if + + if (isset($param['readonly_token']) === true) { + $readonlyToken = $param['readonly_token']; + $afterReadonlyToken = $phpcsFile->findNext(T_WHITESPACE, ($readonlyToken + 1), $param['token'], true); + + $spacesAfter = 0; + if ($afterReadonlyToken !== false + && $tokens[$readonlyToken]['line'] !== $tokens[$afterReadonlyToken]['line'] + ) { + $spacesAfter = 'newline'; + } else if ($tokens[($readonlyToken + 1)]['code'] === T_WHITESPACE) { + $spacesAfter = $tokens[($readonlyToken + 1)]['length']; + } + + if ($spacesAfter !== 1) { + $error = 'Expected 1 space after readonly modifier; %s found'; + $data = [$spacesAfter]; + + $fix = $phpcsFile->addFixableError($error, $readonlyToken, 'SpacingAfterReadonly', $data); + if ($fix === true) { + $phpcsFile->fixer->beginChangeset(); + $phpcsFile->fixer->addContent($readonlyToken, ' '); + + for ($i = ($readonlyToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) { + $phpcsFile->fixer->replaceToken($i, ''); + } + + $phpcsFile->fixer->endChangeset(); + } + }//end if + }//end if + $commaToken = false; if ($paramNumber > 0 && $params[($paramNumber - 1)]['comma_token'] !== false) { $commaToken = $params[($paramNumber - 1)]['comma_token']; diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc index c7f6c27c6b..f80a567557 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc @@ -199,3 +199,14 @@ function newlineBeforeCommaFixerRespectsComments( class PropertyPromotionSpacingAfterComma { public function __construct(private string|int $propA, protected bool $correctSpace, public MyClass $tooMuchSpace,readonly string $noSpace) {} } + +class PropertyPromotionSpacingAfterModifier { + public function __construct( + private$noSpace, + public MyClass $tooMuchSpace, + protected readonly string $tooMuchSpaceX2, + readonly + public + string $tooMuchSpaceNewLines, + ) {} +} diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc.fixed b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc.fixed index 57d25a4783..c82e3fb016 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc.fixed +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc.fixed @@ -175,3 +175,12 @@ function newlineBeforeCommaFixerRespectsComments( class PropertyPromotionSpacingAfterComma { public function __construct(private string|int $propA, protected bool $correctSpace, public MyClass $tooMuchSpace, readonly string $noSpace) {} } + +class PropertyPromotionSpacingAfterModifier { + public function __construct( + private $noSpace, + public MyClass $tooMuchSpace, + protected readonly string $tooMuchSpaceX2, + readonly public string $tooMuchSpaceNewLines, + ) {} +} diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php index 205639c3da..9dcade35a9 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php @@ -90,6 +90,11 @@ public function getErrorList($testFile='') 195 => 1, 196 => 1, 200 => 2, + 205 => 1, + 206 => 1, + 207 => 2, + 208 => 1, + 209 => 1, ]; default: