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\Interface_ >
15
+ * @implements \PHPStan\Rules\Rule<InClassNode >
15
16
*/
16
17
class InterfaceAncestorsRule implements Rule
17
18
{
@@ -31,19 +32,24 @@ public function __construct(
31
32
32
33
public function getNodeType (): string
33
34
{
34
- return Node \ Stmt \Interface_ ::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
- throw new \PHPStan \ShouldNotHappenException ();
40
+ $ originalNode = $ node ->getOriginalNode ();
41
+ if (!$ originalNode instanceof Node \Stmt \Interface_) {
42
+ return [];
41
43
}
44
+ if (!$ scope ->isInClass ()) {
45
+ return [];
46
+ }
47
+ $ classReflection = $ scope ->getClassReflection ();
42
48
43
- $ interfaceName = ( string ) $ node -> namespacedName ;
49
+ $ interfaceName = $ classReflection -> getName () ;
44
50
$ extendsTags = [];
45
51
$ implementsTags = [];
46
- $ docComment = $ node ->getDocComment ();
52
+ $ docComment = $ originalNode ->getDocComment ();
47
53
if ($ docComment !== null ) {
48
54
$ resolvedPhpDoc = $ this ->fileTypeMapper ->getResolvedPhpDoc (
49
55
$ scope ->getFile (),
@@ -57,7 +63,7 @@ public function processNode(Node $node, Scope $scope): array
57
63
}
58
64
59
65
$ extendsErrors = $ this ->genericAncestorsCheck ->check (
60
- $ node ->extends ,
66
+ $ originalNode ->extends ,
61
67
array_map (static function (ExtendsTag $ tag ): Type {
62
68
return $ tag ->getType ();
63
69
}, $ extendsTags ),
0 commit comments