-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathRequireExtendsRule.php
86 lines (72 loc) · 1.98 KB
/
RequireExtendsRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Classes;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function sprintf;
/**
* @implements Rule<InClassNode>
*/
final class RequireExtendsRule implements Rule
{
public function getNodeType(): string
{
return InClassNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
$classReflection = $node->getClassReflection();
if ($classReflection->isInterface()) {
return [];
}
$errors = [];
foreach ($classReflection->getInterfaces() as $interface) {
$extendsTags = $interface->getRequireExtendsTags();
foreach ($extendsTags as $extendsTag) {
$type = $extendsTag->getType();
foreach ($type->getObjectClassNames() as $className) {
if ($classReflection->is($className)) {
continue;
}
$errors[] = RuleErrorBuilder::message(
sprintf(
'Interface %s requires implementing class to extend %s, but %s does not.',
$interface->getDisplayName(),
$type->describe(VerbosityLevel::typeOnly()),
$classReflection->getDisplayName(),
),
)
->identifier('class.missingExtends')
->build();
break;
}
}
}
foreach ($classReflection->getTraits(true) as $trait) {
$extendsTags = $trait->getRequireExtendsTags();
foreach ($extendsTags as $extendsTag) {
$type = $extendsTag->getType();
foreach ($type->getObjectClassNames() as $className) {
if ($classReflection->is($className)) {
continue;
}
$errors[] = RuleErrorBuilder::message(
sprintf(
'Trait %s requires using class to extend %s, but %s does not.',
$trait->getDisplayName(),
$type->describe(VerbosityLevel::typeOnly()),
$classReflection->getDisplayName(),
),
)
->identifier('class.missingExtends')
->build();
break;
}
}
}
return $errors;
}
}