Skip to content

Commit 1443c5e

Browse files
committed
InterfaceAncestorsRule - refactoring to use virtual InClassNode
1 parent fb23cb8 commit 1443c5e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/Rules/Generics/InterfaceAncestorsRule.php

Lines changed: 13 additions & 7 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\Interface_>
15+
* @implements \PHPStan\Rules\Rule<InClassNode>
1516
*/
1617
class 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

Comments
 (0)