Passed
Pull Request — main (#327)
by Chema
08:24 queued 04:46
created

SuffixExtendsRule::processNode()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 27
rs 8.8333
cc 7
nc 8
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\PHPStan\Rules;
6
7
use PhpParser\Node;
8
use PhpParser\Node\Stmt\Class_;
9
use PHPStan\Analyser\Scope;
10
use PHPStan\Reflection\ClassReflection;
11
use PHPStan\Rules\Rule;
12
13
use function strlen;
14
15
/**
16
 * @implements Rule<Class_>
17
 */
18
19
final class SuffixExtendsRule implements Rule
20
{
21
    public function __construct(
22
        private string $suffix,
23
        private string $expectedParent,
24
    ) {
25
    }
26
27
    public function getNodeType(): string
28
    {
29
        return Class_::class;
30
    }
31
32
    public function processNode(Node $node, Scope $scope): array
33
    {
34
        if ($node->isAnonymous()) {
0 ignored issues
show
Bug introduced by
The method isAnonymous() does not exist on PhpParser\Node. It seems like you code against a sub-type of PhpParser\Node such as PhpParser\Node\Stmt\Class_. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        if ($node->/** @scrutinizer ignore-call */ isAnonymous()) {
Loading history...
35
            return [];
36
        }
37
38
        $classReflection = $scope->getClassReflection();
39
        if (!$classReflection instanceof ClassReflection) {
40
            return [];
41
        }
42
43
        $className = $classReflection->getName();
44
        $pos = strrpos($className, '\\');
45
        $shortName = $pos === false ? $className : substr($className, $pos + 1);
46
47
        if (substr($shortName, -strlen($this->suffix)) !== $this->suffix) {
48
            return [];
49
        }
50
51
        if (
52
            $className !== $this->expectedParent &&
53
            !$classReflection->isSubclassOf($this->expectedParent)
54
        ) {
55
            return [sprintf('Class %s should extend %s', $className, $this->expectedParent)];
56
        }
57
58
        return [];
59
    }
60
}
61