|
3 | 3 | namespace PHPStan\Rule\Nette;
|
4 | 4 |
|
5 | 5 | use PhpParser\Node;
|
6 |
| -use PhpParser\Node\Stmt\Class_; |
7 | 6 | use PHPStan\Analyser\Scope;
|
8 |
| -use PHPStan\Broker\Broker; |
| 7 | +use PHPStan\Node\InClassNode; |
9 | 8 | use PHPStan\Rules\Rule;
|
| 9 | +use PHPStan\Rules\RuleErrorBuilder; |
10 | 10 | use function in_array;
|
11 | 11 | use function sprintf;
|
12 | 12 |
|
13 | 13 | /**
|
14 |
| - * @implements Rule<Class_> |
| 14 | + * @implements Rule<InClassNode> |
15 | 15 | */
|
16 | 16 | class DoNotExtendNetteObjectRule implements Rule
|
17 | 17 | {
|
18 | 18 |
|
19 |
| - /** @var Broker */ |
20 |
| - private $broker; |
21 |
| - |
22 |
| - public function __construct(Broker $broker) |
23 |
| - { |
24 |
| - $this->broker = $broker; |
25 |
| - } |
26 |
| - |
27 | 19 | public function getNodeType(): string
|
28 | 20 | {
|
29 |
| - return Class_::class; |
| 21 | + return InClassNode::class; |
30 | 22 | }
|
31 | 23 |
|
32 | 24 | public function processNode(Node $node, Scope $scope): array
|
33 | 25 | {
|
34 |
| - if (!isset($node->namespacedName)) { |
35 |
| - // anonymous class - will be possible to inspect |
36 |
| - // with node visitor and special ClassBody node |
37 |
| - // because $scope will contain the anonymous class reflection |
38 |
| - return []; |
39 |
| - } |
40 |
| - |
41 |
| - $className = (string) $node->namespacedName; |
42 |
| - if (!$this->broker->hasClass($className)) { |
43 |
| - return []; |
44 |
| - } |
45 |
| - |
46 |
| - $classReflection = $this->broker->getClass($className); |
| 26 | + $classReflection = $node->getClassReflection(); |
47 | 27 | $parentClass = $classReflection->getNativeReflection()->getParentClass();
|
48 | 28 | if ($parentClass !== false && in_array($parentClass->getName(), [ // @phpstan-ignore-line
|
49 | 29 | 'Nette\Object',
|
50 | 30 | 'Nette\LegacyObject',
|
51 | 31 | ], true)) {
|
52 | 32 | return [
|
53 |
| - sprintf( |
| 33 | + RuleErrorBuilder::message(sprintf( |
54 | 34 | "Class %s extends %s - it's better to use %s trait.",
|
55 |
| - $className, |
| 35 | + $classReflection->getDisplayName(), |
56 | 36 | 'Nette\Object',
|
57 | 37 | 'Nette\SmartObject'
|
58 |
| - ), |
| 38 | + ))->build(), |
59 | 39 | ];
|
60 | 40 | }
|
61 | 41 |
|
|
0 commit comments