forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumAncestorsRule.php
More file actions
89 lines (77 loc) · 3.01 KB
/
EnumAncestorsRule.php
File metadata and controls
89 lines (77 loc) · 3.01 KB
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
87
88
89
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Generics;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Node\InClassNode;
use PHPStan\PhpDoc\Tag\ExtendsTag;
use PHPStan\PhpDoc\Tag\ImplementsTag;
use PHPStan\Rules\Rule;
use PHPStan\Type\Type;
use function array_map;
use function array_merge;
use function sprintf;
/**
* @implements Rule<InClassNode>
*/
#[RegisteredRule(level: 2)]
final class EnumAncestorsRule implements Rule
{
public function __construct(
private GenericAncestorsCheck $genericAncestorsCheck,
private CrossCheckInterfacesHelper $crossCheckInterfacesHelper,
)
{
}
public function getNodeType(): string
{
return InClassNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
$originalNode = $node->getOriginalNode();
if (!$originalNode instanceof Node\Stmt\Enum_) {
return [];
}
$classReflection = $node->getClassReflection();
$enumName = $classReflection->getName();
$escapedEnumName = SprintfHelper::escapeFormatString($enumName);
$extendsErrors = $this->genericAncestorsCheck->check(
[],
array_map(static fn (ExtendsTag $tag): Type => $tag->getType(), $classReflection->getExtendsTags()),
sprintf('Enum %s @extends tag contains incompatible type %%s.', $escapedEnumName),
sprintf('Enum %s @extends tag contains unresolvable type.', $enumName),
sprintf('Enum %s has @extends tag, but cannot extend anything.', $escapedEnumName),
'',
'',
'',
'',
'',
'',
'',
'',
'',
);
$implementsErrors = $this->genericAncestorsCheck->check(
$originalNode->implements,
array_map(static fn (ImplementsTag $tag): Type => $tag->getType(), $classReflection->getImplementsTags()),
sprintf('Enum %s @implements tag contains incompatible type %%s.', $escapedEnumName),
sprintf('Enum %s @implements tag contains unresolvable type.', $enumName),
sprintf('Enum %s has @implements tag, but does not implement any interface.', $escapedEnumName),
sprintf('The @implements tag of enum %s describes %%s but the enum implements: %%s', $escapedEnumName),
'PHPDoc tag @implements contains generic type %s but %s %s is not generic.',
'Generic type %s in PHPDoc tag @implements does not specify all template types of %s %s: %s',
'Generic type %s in PHPDoc tag @implements specifies %d template types, but %s %s supports only %d: %s',
'Type %s in generic type %s in PHPDoc tag @implements is not subtype of template type %s of %s %s.',
'Call-site variance annotation of %s in generic type %s in PHPDoc tag @implements is not allowed.',
'PHPDoc tag @implements has invalid type %s.',
sprintf('Enum %s implements generic interface %%s but does not specify its types: %%s', $escapedEnumName),
sprintf('in implemented type %%s of enum %s', $escapedEnumName),
);
foreach ($this->crossCheckInterfacesHelper->check($classReflection) as $error) {
$implementsErrors[] = $error;
}
return array_merge($extendsErrors, $implementsErrors);
}
}