Skip to content

Commit fde8118

Browse files
committed
SlevomatCodingStandard.Classes.RequireSelfReference: Fixed false positives for anonymous classes with extends or implements
1 parent 8f11e0f commit fde8118

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

SlevomatCodingStandard/Sniffs/Classes/RequireSelfReferenceSniff.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,17 @@ public function process(File $phpcsFile, $openTagPointer): void
5353
continue;
5454
}
5555

56-
$classPointer = ClassHelper::getClassPointer($phpcsFile, $referencedName->getStartPointer());
57-
if ($classPointer === null) {
56+
$anonymousClassPointer = TokenHelper::findPrevious($phpcsFile, T_ANON_CLASS, $referencedName->getStartPointer() - 1);
57+
58+
if (
59+
$anonymousClassPointer !== null
60+
&& $tokens[$anonymousClassPointer]['scope_closer'] > $referencedName->getEndPointer()
61+
) {
5862
continue;
5963
}
6064

61-
if ($tokens[$classPointer]['code'] === T_ANON_CLASS) {
65+
$classPointer = ClassHelper::getClassPointer($phpcsFile, $referencedName->getStartPointer());
66+
if ($classPointer === null) {
6267
continue;
6368
}
6469

tests/Sniffs/Classes/data/requireSelfReferenceNoErrors.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ public function doSomethingElse(): Whatever
1919
};
2020
}
2121

22+
public function doSomethingElse()
23+
{
24+
return new class extends Whatever {
25+
26+
};
27+
}
28+
29+
public function doAnything()
30+
{
31+
return new class implements Whatever {
32+
33+
};
34+
}
35+
2236
}
2337

2438
$function = function (): Whatever {

0 commit comments

Comments
 (0)