diff --git a/src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php b/src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php index 3f5e1fbc79..9df6b39392 100644 --- a/src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php +++ b/src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php @@ -167,11 +167,28 @@ public function processClose(File $phpcsFile, $stackPtr) }//end if if ($difference !== -1 && $difference !== 1) { - if ($tokens[$nextContent]['code'] === T_DOC_COMMENT_OPEN_TAG) { - $next = $phpcsFile->findNext(T_WHITESPACE, ($tokens[$nextContent]['comment_closer'] + 1), null, true); - if ($next !== false && $tokens[$next]['code'] === T_FUNCTION) { - return; + for ($nextSignificant = $nextContent; $nextSignificant < $phpcsFile->numTokens; $nextSignificant++) { + if ($tokens[$nextSignificant]['code'] === T_WHITESPACE) { + continue; } + + if ($tokens[$nextSignificant]['code'] === T_DOC_COMMENT_OPEN_TAG) { + $nextSignificant = $tokens[$nextSignificant]['comment_closer']; + continue; + } + + if ($tokens[$nextSignificant]['code'] === T_ATTRIBUTE + && isset($tokens[$nextSignificant]['attribute_closer']) === true + ) { + $nextSignificant = $tokens[$nextSignificant]['attribute_closer']; + continue; + } + + break; + } + + if ($tokens[$nextSignificant]['code'] === T_FUNCTION) { + return; } $error = 'Closing brace of a %s must be followed by a single blank line; found %s'; diff --git a/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc b/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc index 2af42d3721..25d8d30211 100644 --- a/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc +++ b/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc @@ -128,3 +128,25 @@ class Test readonly class Test { } + +class TooMuchSpacingBelowClassButShouldNotBeFlaggedWhenNextThingIsFunctionWithAttribute +{ + var $x; +} + + +#[AttributesShouldBeJumpedOver] +function ThisIsFineAndHasAttribute() {} + +class TooMuchSpacingBelowClassButShouldNotBeFlaggedWhenNextThingIsFunctionWithDocblockAndAttribute +{ + var $x; +} + + +/** + * No error. + */ +#[AttributesShouldBeJumpedOver] +#[ASecondAttributeShouldBeJumpedOverToo]#[AndAThirdAsWell] +function ThisIsFineAndHasDocblockAndAttribute() {} diff --git a/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc.fixed index 5d01b68e0a..bf042f932d 100644 --- a/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc.fixed @@ -138,3 +138,25 @@ readonly class Test readonly class Test { } + +class TooMuchSpacingBelowClassButShouldNotBeFlaggedWhenNextThingIsFunctionWithAttribute +{ + var $x; +} + + +#[AttributesShouldBeJumpedOver] +function ThisIsFineAndHasAttribute() {} + +class TooMuchSpacingBelowClassButShouldNotBeFlaggedWhenNextThingIsFunctionWithDocblockAndAttribute +{ + var $x; +} + + +/** + * No error. + */ +#[AttributesShouldBeJumpedOver] +#[ASecondAttributeShouldBeJumpedOverToo]#[AndAThirdAsWell] +function ThisIsFineAndHasDocblockAndAttribute() {} diff --git a/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.php b/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.php index 635fd5d10a..be3fd59970 100644 --- a/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.php +++ b/src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.php @@ -68,6 +68,8 @@ public function getErrorList() 121 => 1, 124 => 2, 128 => 2, + 132 => 1, + 141 => 1, ]; }//end getErrorList()