Repository navigation
Expand file tree
/
Copy pathTemplateTypeHelper.php
More file actions
232 lines (195 loc) · 6.77 KB
/
Copy pathTemplateTypeHelper.php
File metadata and controls
232 lines (195 loc) · 6.77 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php declare(strict_types = 1);
namespace PHPStan\Type\Generic;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Type\ErrorType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\NeverType;
use PHPStan\Type\NonAcceptingNeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\VerbosityLevel;
use function array_values;
final class TemplateTypeHelper
{
/**
* Replaces template types with standin types
*/
public static function resolveTemplateTypes(
Type $type,
TemplateTypeMap $standins,
TemplateTypeVarianceMap $callSiteVariances,
TemplateTypeVariance $positionVariance,
bool $keepErrorTypes = false,
): Type
{
if (!$type->hasTemplateOrLateResolvableType()) {
return $type;
}
$references = $type->getReferencedTemplateTypes($positionVariance);
return TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($standins, $references, $callSiteVariances, $keepErrorTypes): Type {
if ($type instanceof TemplateType && !$type->isArgument()) {
$newType = $standins->getType($type->getName());
$variance = TemplateTypeVariance::createInvariant();
foreach ($references as $reference) {
// this uses identity to distinguish between different occurrences of the same template type
// see https://github.com/phpstan/phpstan-src/pull/2485#discussion_r1328555397 for details
if ($reference->getType() === $type) {
$variance = $reference->getPositionVariance();
break;
}
}
if ($newType === null) {
return $traverse($type);
}
if ($newType instanceof ErrorType && !$keepErrorTypes) {
return $traverse($type->getDefault() ?? $type->getBound());
}
$callSiteVariance = $callSiteVariances->getVariance($type->getName());
if ($callSiteVariance === null || $callSiteVariance->invariant()) {
return $newType;
}
if (!$callSiteVariance->covariant() && $variance->covariant()) {
return $traverse($type->getBound());
}
if (!$callSiteVariance->contravariant() && $variance->contravariant()) {
return new NonAcceptingNeverType();
}
return $newType;
}
return $traverse($type);
});
}
public static function resolveToDefaults(Type $type): Type
{
return TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
if ($type instanceof TemplateType) {
return $traverse($type->getDefault() ?? $type->getBound());
}
return $traverse($type);
});
}
public static function resolveToBounds(Type $type): Type
{
return TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
if ($type instanceof TemplateType) {
return $traverse($type->getBound());
}
return $traverse($type);
});
}
/**
* Widens `never` type arguments of generic objects to their template bounds.
*
* An object constructed empty - like `new ArrayObject()` - gets `never` type
* arguments inferred. Once the object might have been mutated, `never` no longer
* describes what it can contain, but a wider type inferred from the call site
* would be unsound, so the bounds are the safest thing to fall back to.
*/
public static function widenNeverTypeArguments(Type $type): Type
{
return TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
if ($type instanceof GenericObjectType) {
$widenedTypes = self::widenNeverTypesToBounds($type->getTypes(), $type->getClassReflection());
if ($widenedTypes !== null) {
return $traverse(new GenericObjectType(
$type->getClassName(),
$widenedTypes,
$type->getSubtractedType(),
variances: $type->getVariances(),
));
}
} elseif ($type instanceof GenericStaticType) {
$widenedTypes = self::widenNeverTypesToBounds($type->getTypes(), $type->getClassReflection());
if ($widenedTypes !== null) {
return $traverse(new GenericStaticType(
$type->getClassReflection(),
$widenedTypes,
$type->getSubtractedType(),
$type->getVariances(),
));
}
}
return $traverse($type);
});
}
/**
* @param array<int, Type> $typeArguments
* @return array<int, Type>|null null when nothing was widened
*/
private static function widenNeverTypesToBounds(array $typeArguments, ?ClassReflection $classReflection): ?array
{
if ($classReflection === null) {
return null;
}
$templateTypes = array_values($classReflection->getTemplateTypeMap()->getTypes());
$widened = false;
foreach ($typeArguments as $i => $typeArgument) {
if (!$typeArgument instanceof NeverType) {
continue;
}
if (!isset($templateTypes[$i])) {
continue;
}
$templateType = $templateTypes[$i];
if (!$templateType instanceof TemplateType) {
continue;
}
$typeArguments[$i] = $templateType->getBound();
$widened = true;
}
return $widened ? $typeArguments : null;
}
/**
* @template T of Type
* @param T $type
* @return T
*/
public static function toArgument(Type $type): Type
{
$ownedTemplates = [];
/** @var T */
return TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$ownedTemplates): Type {
if ($type instanceof ParametersAcceptor) {
$templateTypeMap = $type->getTemplateTypeMap();
foreach ($type->getParameters() as $parameter) {
$parameterType = $parameter->getType();
if (!($parameterType instanceof TemplateType) || !$templateTypeMap->hasType($parameterType->getName())) {
continue;
}
$ownedTemplates[] = $parameterType;
}
$returnType = $type->getReturnType();
if ($returnType instanceof TemplateType && $templateTypeMap->hasType($returnType->getName())) {
$ownedTemplates[] = $returnType;
}
}
foreach ($ownedTemplates as $ownedTemplate) {
if ($ownedTemplate === $type) {
return $traverse($type);
}
}
if ($type instanceof TemplateType) {
// templates declared by a callable<T>(...)/Closure<T>(...) type in the signature
// belong to the callable value, not to the entered function
if ($type->getScope()->equals(TemplateTypeScope::createWithAnonymousFunction())) {
return $traverse($type);
}
return $traverse($type->toArgument());
}
return $traverse($type);
});
}
public static function generalizeInferredTemplateType(TemplateType $templateType, Type $type): Type
{
if (!$templateType->getVariance()->covariant()) {
$isArrayKey = $templateType->getBound()->describe(VerbosityLevel::precise()) === '(int|string)';
if ($type->isScalar()->yes() && $isArrayKey) {
$type = $type->generalize(GeneralizePrecision::templateArgument());
} elseif ($type->isConstantValue()->yes() && (!$templateType->getBound()->isScalar()->yes() || $isArrayKey)) {
$type = $type->generalize(GeneralizePrecision::templateArgument());
}
}
return $type;
}
}