4
4
5
5
use PhpParser \Node ;
6
6
use PHPStan \Analyser \Scope ;
7
+ use PHPStan \Node \InClassNode ;
7
8
use PHPStan \PhpDoc \Tag \ExtendsTag ;
8
9
use PHPStan \PhpDoc \Tag \ImplementsTag ;
9
10
use PHPStan \Rules \Rule ;
10
11
use PHPStan \Type \FileTypeMapper ;
11
12
use PHPStan \Type \Type ;
12
13
13
14
/**
14
- * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\Class_ >
15
+ * @implements \PHPStan\Rules\Rule<InClassNode >
15
16
*/
16
17
class ClassAncestorsRule implements Rule
17
18
{
@@ -31,21 +32,27 @@ public function __construct(
31
32
32
33
public function getNodeType (): string
33
34
{
34
- return Node \ Stmt \Class_ ::class;
35
+ return InClassNode ::class;
35
36
}
36
37
37
38
public function processNode (Node $ node , Scope $ scope ): array
38
39
{
39
- if (! isset ( $ node ->namespacedName )) {
40
- // anonymous class
40
+ $ originalNode = $ node ->getOriginalNode ();
41
+ if (! $ originalNode instanceof Node \ Stmt \Class_) {
41
42
return [];
42
43
}
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 ();
45
52
46
53
$ extendsTags = [];
47
54
$ implementsTags = [];
48
- $ docComment = $ node ->getDocComment ();
55
+ $ docComment = $ originalNode ->getDocComment ();
49
56
if ($ docComment !== null ) {
50
57
$ resolvedPhpDoc = $ this ->fileTypeMapper ->getResolvedPhpDoc (
51
58
$ scope ->getFile (),
@@ -59,7 +66,7 @@ public function processNode(Node $node, Scope $scope): array
59
66
}
60
67
61
68
$ extendsErrors = $ this ->genericAncestorsCheck ->check (
62
- $ node ->extends !== null ? [$ node ->extends ] : [],
69
+ $ originalNode ->extends !== null ? [$ originalNode ->extends ] : [],
63
70
array_map (static function (ExtendsTag $ tag ): Type {
64
71
return $ tag ->getType ();
65
72
}, $ extendsTags ),
@@ -76,7 +83,7 @@ public function processNode(Node $node, Scope $scope): array
76
83
);
77
84
78
85
$ implementsErrors = $ this ->genericAncestorsCheck ->check (
79
- $ node ->implements ,
86
+ $ originalNode ->implements ,
80
87
array_map (static function (ImplementsTag $ tag ): Type {
81
88
return $ tag ->getType ();
82
89
}, $ implementsTags ),
0 commit comments