Skip to content

Commit 3e12480

Browse files
committed
fix: improve handling of attributes in anonymous class detection
1 parent 48b016a commit 3e12480

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/Rules/DocBlockHeaderFixer.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,29 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
134134
private function isAnonymousClass(Tokens $tokens, int $classIndex): bool
135135
{
136136
// Look backwards for 'new' keyword
137+
$insideAttribute = false;
137138
for ($i = $classIndex - 1; $i >= 0; --$i) {
138139
$token = $tokens[$i];
139140

140-
// Skip whitespace and attributes
141-
if ($token->isWhitespace() || $token->isGivenKind(T_ATTRIBUTE)) {
141+
// Skip whitespace
142+
if ($token->isWhitespace()) {
143+
continue;
144+
}
145+
146+
// When going backwards, ']' marks the end of an attribute (we enter it)
147+
if (']' === $token->getContent()) {
148+
$insideAttribute = true;
149+
continue;
150+
}
151+
152+
// T_ATTRIBUTE '#[' marks the start of an attribute (we exit it when going backwards)
153+
if ($token->isGivenKind([T_ATTRIBUTE, T_FINAL, T_READONLY])) {
154+
$insideAttribute = false;
155+
continue;
156+
}
157+
158+
// Skip everything inside attributes
159+
if ($insideAttribute) {
142160
continue;
143161
}
144162

0 commit comments

Comments
 (0)