44
55use PhpParser \Node ;
66use PHPStan \Analyser \Scope ;
7+ use PHPStan \Node \InClassNode ;
78use PHPStan \PhpDoc \Tag \ExtendsTag ;
89use PHPStan \PhpDoc \Tag \ImplementsTag ;
910use PHPStan \Rules \Rule ;
1011use PHPStan \Type \FileTypeMapper ;
1112use PHPStan \Type \Type ;
1213
1314/**
14- * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\Interface_ >
15+ * @implements \PHPStan\Rules\Rule<InClassNode >
1516 */
1617class InterfaceAncestorsRule implements Rule
1718{
@@ -31,19 +32,24 @@ public function __construct(
3132
3233 public function getNodeType (): string
3334 {
34- return Node \ Stmt \Interface_ ::class;
35+ return InClassNode ::class;
3536 }
3637
3738 public function processNode (Node $ node , Scope $ scope ): array
3839 {
39- if (!isset ($ node ->namespacedName )) {
40- throw new \PHPStan \ShouldNotHappenException ();
40+ $ originalNode = $ node ->getOriginalNode ();
41+ if (!$ originalNode instanceof Node \Stmt \Interface_) {
42+ return [];
4143 }
44+ if (!$ scope ->isInClass ()) {
45+ return [];
46+ }
47+ $ classReflection = $ scope ->getClassReflection ();
4248
43- $ interfaceName = ( string ) $ node -> namespacedName ;
49+ $ interfaceName = $ classReflection -> getName () ;
4450 $ extendsTags = [];
4551 $ implementsTags = [];
46- $ docComment = $ node ->getDocComment ();
52+ $ docComment = $ originalNode ->getDocComment ();
4753 if ($ docComment !== null ) {
4854 $ resolvedPhpDoc = $ this ->fileTypeMapper ->getResolvedPhpDoc (
4955 $ scope ->getFile (),
@@ -57,7 +63,7 @@ public function processNode(Node $node, Scope $scope): array
5763 }
5864
5965 $ extendsErrors = $ this ->genericAncestorsCheck ->check (
60- $ node ->extends ,
66+ $ originalNode ->extends ,
6167 array_map (static function (ExtendsTag $ tag ): Type {
6268 return $ tag ->getType ();
6369 }, $ extendsTags ),
0 commit comments