forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VarianceCheck.php
168 lines (140 loc) · 4.61 KB
/
VarianceCheck.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Generics;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Type;
use function sprintf;
class VarianceCheck
{
/** @return RuleError[] */
public function checkParametersAcceptor(
ParametersAcceptor $parametersAcceptor,
string $parameterTypeMessage,
string $returnTypeMessage,
string $generalMessage,
bool $isConstructor,
bool $isStatic,
bool $isPrivate,
): array
{
$errors = [];
foreach ($parametersAcceptor->getTemplateTypeMap()->getTypes() as $templateType) {
if (!$templateType instanceof TemplateType) {
continue;
}
if ($templateType->getScope()->getClassName() !== null
&& $templateType->getScope()->getFunctionName() === '__construct'
) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Constructor is not allowed to define type parameters, but template type %s is defined %s.',
$templateType->getName(),
$generalMessage,
))->build();
continue;
}
if ($templateType->getScope()->getFunctionName() === null
|| $templateType->getVariance()->invariant()
) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
'Variance annotation is only allowed for type parameters of classes and interfaces, but occurs in template type %s %s.',
$templateType->getName(),
$generalMessage,
))->build();
}
if ($isConstructor) {
return $errors;
}
foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
$variance = TemplateTypeVariance::createContravariant();
$type = $parameterReflection->getType();
$message = sprintf($parameterTypeMessage, $parameterReflection->getName());
foreach ($this->check($variance, $type, $message, $isStatic, $isPrivate) as $error) {
$errors[] = $error;
}
}
$variance = TemplateTypeVariance::createCovariant();
$type = $parametersAcceptor->getReturnType();
foreach ($this->check($variance, $type, $returnTypeMessage, $isStatic, $isPrivate) as $error) {
$errors[] = $error;
}
return $errors;
}
/** @return RuleError[] */
public function checkProperty(
PropertyReflection $propertyReflection,
string $message,
bool $isStatic,
bool $isPrivate,
bool $isReadOnly,
): array
{
$readableType = $propertyReflection->getReadableType();
$writableType = $propertyReflection->getWritableType();
if ($readableType->equals($writableType)) {
$variance = $isReadOnly
? TemplateTypeVariance::createCovariant()
: TemplateTypeVariance::createInvariant();
return $this->check($variance, $readableType, $message, $isStatic, $isPrivate);
}
$errors = [];
if ($propertyReflection->isReadable()) {
foreach ($this->check(TemplateTypeVariance::createCovariant(), $readableType, $message, $isStatic, $isPrivate) as $error) {
$errors[] = $error;
}
}
if ($propertyReflection->isWritable()) {
$checkStatic = $isStatic && !$propertyReflection->isReadable();
foreach ($this->check(TemplateTypeVariance::createContravariant(), $writableType, $message, $checkStatic, $isPrivate) as $error) {
$errors[] = $error;
}
}
return $errors;
}
/** @return RuleError[] */
public function check(
TemplateTypeVariance $positionVariance,
Type $type,
string $messageContext,
bool $isStatic,
bool $isPrivate,
): array
{
$errors = [];
foreach ($type->getReferencedTemplateTypes($positionVariance) as $reference) {
$referredType = $reference->getType();
if ($isStatic && $referredType->getScope()->getFunctionName() === null) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Class template type %s cannot be referenced in a static member %s.',
$referredType->getName(),
$messageContext,
))->build();
continue;
}
if ($isPrivate) {
continue;
}
if (($referredType->getScope()->getFunctionName() !== null && !$referredType->getVariance()->invariant())
|| $this->isTemplateTypeVarianceValid($reference->getPositionVariance(), $referredType)) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
'Template type %s is declared as %s, but occurs in %s position %s.',
$referredType->getName(),
$referredType->getVariance()->describe(),
$reference->getPositionVariance()->describe(),
$messageContext,
))->build();
}
return $errors;
}
private function isTemplateTypeVarianceValid(TemplateTypeVariance $positionVariance, TemplateType $type): bool
{
return $positionVariance->validPosition($type->getVariance());
}
}