-
Notifications
You must be signed in to change notification settings - Fork 560
Expand file tree
/
Copy pathTemplateTypeCheck.php
More file actions
139 lines (128 loc) · 4.91 KB
/
TemplateTypeCheck.php
File metadata and controls
139 lines (128 loc) · 4.91 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Generics;
use PhpParser\Node;
use PHPStan\Internal\SprintfHelper;
use PHPStan\PhpDoc\Tag\TemplateTag;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassNameNodePair;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FloatType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeScope;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\KeyOfType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;
use PHPStan\Type\TypeAliasResolver;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function array_map;
use function array_merge;
use function get_class;
use function sprintf;
class TemplateTypeCheck
{
public function __construct(
private ReflectionProvider $reflectionProvider,
private ClassCaseSensitivityCheck $classCaseSensitivityCheck,
private GenericObjectTypeCheck $genericObjectTypeCheck,
private TypeAliasResolver $typeAliasResolver,
private bool $checkClassCaseSensitivity,
)
{
}
/**
* @param array<string, TemplateTag> $templateTags
* @return RuleError[]
*/
public function check(
Node $node,
TemplateTypeScope $templateTypeScope,
array $templateTags,
string $sameTemplateTypeNameAsClassMessage,
string $sameTemplateTypeNameAsTypeMessage,
string $invalidBoundTypeMessage,
string $notSupportedBoundMessage,
): array
{
$messages = [];
foreach ($templateTags as $templateTag) {
$templateTagName = $templateTag->getName();
if ($this->reflectionProvider->hasClass($templateTagName)) {
$messages[] = RuleErrorBuilder::message(sprintf(
$sameTemplateTypeNameAsClassMessage,
$templateTagName,
))->build();
}
if ($this->typeAliasResolver->hasTypeAlias($templateTagName, $templateTypeScope->getClassName())) {
$messages[] = RuleErrorBuilder::message(sprintf(
$sameTemplateTypeNameAsTypeMessage,
$templateTagName,
))->build();
}
$boundType = $templateTag->getBound();
foreach ($boundType->getReferencedClasses() as $referencedClass) {
if (
$this->reflectionProvider->hasClass($referencedClass)
&& !$this->reflectionProvider->getClass($referencedClass)->isTrait()
) {
continue;
}
$messages[] = RuleErrorBuilder::message(sprintf(
$invalidBoundTypeMessage,
$templateTagName,
$referencedClass,
))->build();
}
if ($this->checkClassCaseSensitivity) {
$classNameNodePairs = array_map(static fn (string $referencedClass): ClassNameNodePair => new ClassNameNodePair($referencedClass, $node), $boundType->getReferencedClasses());
$messages = array_merge($messages, $this->classCaseSensitivityCheck->checkClassNames($classNameNodePairs));
}
$boundType = $templateTag->getBound();
$boundTypeClass = get_class($boundType);
if (
$boundTypeClass !== MixedType::class
&& $boundTypeClass !== ConstantArrayType::class
&& $boundTypeClass !== ArrayType::class
&& $boundTypeClass !== ConstantStringType::class
&& $boundTypeClass !== StringType::class
&& $boundTypeClass !== ConstantIntegerType::class
&& $boundTypeClass !== IntegerType::class
&& $boundTypeClass !== FloatType::class
&& $boundTypeClass !== BooleanType::class
&& $boundTypeClass !== ObjectWithoutClassType::class
&& $boundTypeClass !== ObjectType::class
&& $boundTypeClass !== GenericObjectType::class
&& $boundTypeClass !== KeyOfType::class
&& !$boundType instanceof UnionType
&& !$boundType instanceof IntersectionType
&& !$boundType instanceof TemplateType
) {
$messages[] = RuleErrorBuilder::message(sprintf($notSupportedBoundMessage, $templateTagName, $boundType->describe(VerbosityLevel::typeOnly())))->build();
}
$escapedTemplateTagName = SprintfHelper::escapeFormatString($templateTagName);
$genericObjectErrors = $this->genericObjectTypeCheck->check(
$boundType,
sprintf('PHPDoc tag @template %s bound contains generic type %%s but %%s %%s is not generic.', $escapedTemplateTagName),
sprintf('PHPDoc tag @template %s bound has type %%s which does not specify all template types of %%s %%s: %%s', $escapedTemplateTagName),
sprintf('PHPDoc tag @template %s bound has type %%s which specifies %%d template types, but %%s %%s supports only %%d: %%s', $escapedTemplateTagName),
sprintf('Type %%s in generic type %%s in PHPDoc tag @template %s is not subtype of template type %%s of %%s %%s.', $escapedTemplateTagName),
);
foreach ($genericObjectErrors as $genericObjectError) {
$messages[] = $genericObjectError;
}
}
return $messages;
}
}