Skip to content

Commit fb23cb8

Browse files
committed
ClassAncestorsRule - refactoring to use virtual InClassNode
1 parent 2af6aef commit fb23cb8

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/Rules/Generics/ClassAncestorsRule.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Node\InClassNode;
78
use PHPStan\PhpDoc\Tag\ExtendsTag;
89
use PHPStan\PhpDoc\Tag\ImplementsTag;
910
use PHPStan\Rules\Rule;
1011
use PHPStan\Type\FileTypeMapper;
1112
use PHPStan\Type\Type;
1213

1314
/**
14-
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\Class_>
15+
* @implements \PHPStan\Rules\Rule<InClassNode>
1516
*/
1617
class ClassAncestorsRule implements Rule
1718
{
@@ -31,21 +32,27 @@ public function __construct(
3132

3233
public function getNodeType(): string
3334
{
34-
return Node\Stmt\Class_::class;
35+
return InClassNode::class;
3536
}
3637

3738
public function processNode(Node $node, Scope $scope): array
3839
{
39-
if (!isset($node->namespacedName)) {
40-
// anonymous class
40+
$originalNode = $node->getOriginalNode();
41+
if (!$originalNode instanceof Node\Stmt\Class_) {
4142
return [];
4243
}
43-
44-
$className = (string) $node->namespacedName;
44+
if (!$scope->isInClass()) {
45+
return [];
46+
}
47+
$classReflection = $scope->getClassReflection();
48+
if ($classReflection->isAnonymous()) {
49+
return [];
50+
}
51+
$className = $classReflection->getName();
4552

4653
$extendsTags = [];
4754
$implementsTags = [];
48-
$docComment = $node->getDocComment();
55+
$docComment = $originalNode->getDocComment();
4956
if ($docComment !== null) {
5057
$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
5158
$scope->getFile(),
@@ -59,7 +66,7 @@ public function processNode(Node $node, Scope $scope): array
5966
}
6067

6168
$extendsErrors = $this->genericAncestorsCheck->check(
62-
$node->extends !== null ? [$node->extends] : [],
69+
$originalNode->extends !== null ? [$originalNode->extends] : [],
6370
array_map(static function (ExtendsTag $tag): Type {
6471
return $tag->getType();
6572
}, $extendsTags),
@@ -76,7 +83,7 @@ public function processNode(Node $node, Scope $scope): array
7683
);
7784

7885
$implementsErrors = $this->genericAncestorsCheck->check(
79-
$node->implements,
86+
$originalNode->implements,
8087
array_map(static function (ImplementsTag $tag): Type {
8188
return $tag->getType();
8289
}, $implementsTags),

0 commit comments

Comments
 (0)