Skip to content

Commit 16047ce

Browse files
committed
Squiz/ClassDeclaration: allow for function attributes
The `Squiz.Classes.ClassDeclaration` sniff checks the number of blank lines _after_ a class closing brace to be exactly one blank line. To prevent conflicts with other sniffs checking blank lines before function declarations, it bows out when the next thing after the class declaration is a function declaration and defers to the function declaration sniff to handle the "blank lines before function" check. See squizlabs/PHP_CodeSniffer 2033 for historic context. However, the above "bowing out" breaks when a function after a class has PHP 8.0+ attributes attached to it and the sniff would still the `NewlinesAfterCloseBrace` error. Fixed now. Includes tests. Note: the new "errors" caused by the new tests are for the `MultipleClasses` code, which is unrelated to this fix and valid.
1 parent f117c2b commit 16047ce

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,28 @@ public function processClose(File $phpcsFile, $stackPtr)
167167
}//end if
168168

169169
if ($difference !== -1 && $difference !== 1) {
170-
if ($tokens[$nextContent]['code'] === T_DOC_COMMENT_OPEN_TAG) {
171-
$next = $phpcsFile->findNext(T_WHITESPACE, ($tokens[$nextContent]['comment_closer'] + 1), null, true);
172-
if ($next !== false && $tokens[$next]['code'] === T_FUNCTION) {
173-
return;
170+
for ($nextSignificant = $nextContent; $nextSignificant < $phpcsFile->numTokens; $nextSignificant++) {
171+
if ($tokens[$nextSignificant]['code'] === T_WHITESPACE) {
172+
continue;
174173
}
174+
175+
if ($tokens[$nextSignificant]['code'] === T_DOC_COMMENT_OPEN_TAG) {
176+
$nextSignificant = $tokens[$nextSignificant]['comment_closer'];
177+
continue;
178+
}
179+
180+
if ($tokens[$nextSignificant]['code'] === T_ATTRIBUTE
181+
&& isset($tokens[$nextSignificant]['attribute_closer']) === true
182+
) {
183+
$nextSignificant = $tokens[$nextSignificant]['attribute_closer'];
184+
continue;
185+
}
186+
187+
break;
188+
}
189+
190+
if ($tokens[$nextSignificant]['code'] === T_FUNCTION) {
191+
return;
175192
}
176193

177194
$error = 'Closing brace of a %s must be followed by a single blank line; found %s';

src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,25 @@ class Test
128128
readonly class Test
129129
{
130130
}
131+
132+
class TooMuchSpacingBelowClassButShouldNotBeFlaggedWhenNextThingIsFunctionWithAttribute
133+
{
134+
var $x;
135+
}
136+
137+
138+
#[AttributesShouldBeJumpedOver]
139+
function ThisIsFineAndHasAttribute() {}
140+
141+
class TooMuchSpacingBelowClassButShouldNotBeFlaggedWhenNextThingIsFunctionWithDocblockAndAttribute
142+
{
143+
var $x;
144+
}
145+
146+
147+
/**
148+
* No error.
149+
*/
150+
#[AttributesShouldBeJumpedOver]
151+
#[ASecondAttributesShouldBeJumpedOverToo]#[AndAThirdAsWell]
152+
function ThisIsFineAndHasDocblockAndAttribute() {}

src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc.fixed

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,25 @@ readonly class Test
138138
readonly class Test
139139
{
140140
}
141+
142+
class TooMuchSpacingBelowClassButShouldNotBeFlaggedWhenNextThingIsFunctionWithAttribute
143+
{
144+
var $x;
145+
}
146+
147+
148+
#[AttributesShouldBeJumpedOver]
149+
function ThisIsFineAndHasAttribute() {}
150+
151+
class TooMuchSpacingBelowClassButShouldNotBeFlaggedWhenNextThingIsFunctionWithDocblockAndAttribute
152+
{
153+
var $x;
154+
}
155+
156+
157+
/**
158+
* No error.
159+
*/
160+
#[AttributesShouldBeJumpedOver]
161+
#[ASecondAttributesShouldBeJumpedOverToo]#[AndAThirdAsWell]
162+
function ThisIsFineAndHasDocblockAndAttribute() {}

src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public function getErrorList()
6868
121 => 1,
6969
124 => 2,
7070
128 => 2,
71+
132 => 1,
72+
141 => 1,
7173
];
7274

7375
}//end getErrorList()

0 commit comments

Comments
 (0)