From af2465a543cfd3d84f88b4ef2d71b7a0eb308d9e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 6 Jun 2025 01:54:33 +0200 Subject: [PATCH] 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. --- ...unctionDeclarationArgumentSpacingSniff.php | 34 +++++++++++++++++++ ...onDeclarationArgumentSpacingUnitTest.1.inc | 15 ++++++++ ...arationArgumentSpacingUnitTest.1.inc.fixed | 13 +++++++ ...tionDeclarationArgumentSpacingUnitTest.php | 6 ++++ 4 files changed, 68 insertions(+) diff --git a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php index 26fd541f06..1fd34fccb5 100644 --- a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php @@ -340,6 +340,40 @@ public function processBracket($phpcsFile, $openBracket) }//end if }//end if + if (isset($param['set_visibility_token']) === true && $param['set_visibility_token'] !== false) { + $visibilityToken = $param['set_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 set-visibility modifier "%s"; %s found'; + $data = [ + $tokens[$visibilityToken]['content'], + $spacesAfter, + ]; + + $fix = $phpcsFile->addFixableError($error, $visibilityToken, 'SpacingAfterSetVisbility', $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); diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc index f80a567557..af872e73d4 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc @@ -210,3 +210,18 @@ class PropertyPromotionSpacingAfterModifier { string $tooMuchSpaceNewLines, ) {} } + +class AsymVisibilityPropertyPromotionSpacingAfterComma { + public function __construct(private(set) string|int $propA, protected(set) bool $correctSpace, public(set) MyClass $tooMuchSpace,public(set) string $noSpace) {} +} + +class AsymVisibilityPropertyPromotionSpacingAfterModifier { + public function __construct( + private(set)$noSpace, + public(set) MyClass $tooMuchSpace, + protected(set) public string $tooMuchSpaceX2, + private + public(set) + 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 c82e3fb016..356b374212 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc.fixed +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.1.inc.fixed @@ -184,3 +184,16 @@ class PropertyPromotionSpacingAfterModifier { readonly public string $tooMuchSpaceNewLines, ) {} } + +class AsymVisibilityPropertyPromotionSpacingAfterComma { + public function __construct(private(set) string|int $propA, protected(set) bool $correctSpace, public(set) MyClass $tooMuchSpace, public(set) string $noSpace) {} +} + +class AsymVisibilityPropertyPromotionSpacingAfterModifier { + public function __construct( + private(set) $noSpace, + public(set) MyClass $tooMuchSpace, + protected(set) public string $tooMuchSpaceX2, + private public(set) string $tooMuchSpaceNewLines, + ) {} +} diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php index 9dcade35a9..2bd81ca241 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php @@ -95,6 +95,12 @@ public function getErrorList($testFile='') 207 => 2, 208 => 1, 209 => 1, + 215 => 2, + 220 => 1, + 221 => 1, + 222 => 2, + 223 => 1, + 224 => 1, ]; default: