Skip to content

Commit b07cf82

Browse files
authored
Merge pull request #825 from PHPCSStandards/feature/squiz-classdeclaration-php-80-attributes
Squiz/ClassDeclaration: allow for function attributes
2 parents c0c08ad + ff62a6e commit b07cf82

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+
#[ASecondAttributeShouldBeJumpedOverToo]#[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+
#[ASecondAttributeShouldBeJumpedOverToo]#[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)