-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathTypehintHelper.php
240 lines (216 loc) · 6.9 KB
/
TypehintHelper.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
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
233
234
235
236
237
238
239
240
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use ReflectionIntersectionType;
use ReflectionNamedType;
use ReflectionType;
use ReflectionUnionType;
use function array_map;
use function count;
use function get_class;
use function is_string;
use function sprintf;
use function str_ends_with;
use function strtolower;
final class TypehintHelper
{
private static function getTypeObjectFromTypehint(string $typeString, ClassReflection|string|null $selfClass): Type
{
switch (strtolower($typeString)) {
case 'int':
return new IntegerType();
case 'bool':
return new BooleanType();
case 'false':
return new ConstantBooleanType(false);
case 'true':
return new ConstantBooleanType(true);
case 'string':
return new StringType();
case 'float':
return new FloatType();
case 'array':
return new ArrayType(new MixedType(), new MixedType());
case 'iterable':
return new IterableType(new MixedType(), new MixedType());
case 'callable':
return new CallableType();
case 'void':
return new VoidType();
case 'object':
return new ObjectWithoutClassType();
case 'mixed':
return new MixedType(true);
case 'self':
if ($selfClass instanceof ClassReflection) {
$selfClass = $selfClass->getName();
}
return $selfClass !== null ? new ObjectType($selfClass) : new ErrorType();
case 'parent':
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
if (is_string($selfClass)) {
if ($reflectionProvider->hasClass($selfClass)) {
$selfClass = $reflectionProvider->getClass($selfClass);
} else {
$selfClass = null;
}
}
if ($selfClass !== null) {
if ($selfClass->getParentClass() !== null) {
return new ObjectType($selfClass->getParentClass()->getName());
}
}
return new NonexistentParentClassType();
case 'static':
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
if (is_string($selfClass)) {
if ($reflectionProvider->hasClass($selfClass)) {
$selfClass = $reflectionProvider->getClass($selfClass);
} else {
$selfClass = null;
}
}
if ($selfClass !== null) {
return new StaticType($selfClass);
}
return new ErrorType();
case 'null':
return new NullType();
case 'never':
return new NonAcceptingNeverType();
default:
return new ObjectType($typeString);
}
}
/** @api */
public static function decideTypeFromReflection(
?ReflectionType $reflectionType,
?Type $phpDocType = null,
ClassReflection|string|null $selfClass = null,
bool $isVariadic = false,
): Type
{
if ($reflectionType === null) {
if ($isVariadic && $phpDocType instanceof ArrayType) {
$phpDocType = $phpDocType->getItemType();
}
return $phpDocType ?? new MixedType();
}
if ($reflectionType instanceof ReflectionUnionType) {
$type = TypeCombinator::union(...array_map(static fn (ReflectionType $type): Type => self::decideTypeFromReflection($type, null, $selfClass, false), $reflectionType->getTypes()));
return self::decideType($type, $phpDocType);
}
if ($reflectionType instanceof ReflectionIntersectionType) {
$types = [];
foreach ($reflectionType->getTypes() as $innerReflectionType) {
$innerType = self::decideTypeFromReflection($innerReflectionType, null, $selfClass, false);
if (!$innerType->isObject()->yes()) {
return new NeverType();
}
$types[] = $innerType;
}
return self::decideType(TypeCombinator::intersect(...$types), $phpDocType);
}
if (!$reflectionType instanceof ReflectionNamedType) {
throw new ShouldNotHappenException(sprintf('Unexpected type: %s', get_class($reflectionType)));
}
$reflectionTypeString = $reflectionType->getName();
$loweredReflectionTypeString = strtolower($reflectionTypeString);
if (str_ends_with($loweredReflectionTypeString, '\\object')) {
$reflectionTypeString = 'object';
} elseif (str_ends_with($loweredReflectionTypeString, '\\mixed')) {
$reflectionTypeString = 'mixed';
} elseif (str_ends_with($loweredReflectionTypeString, '\\true')) {
$reflectionTypeString = 'true';
} elseif (str_ends_with($loweredReflectionTypeString, '\\false')) {
$reflectionTypeString = 'false';
} elseif (str_ends_with($loweredReflectionTypeString, '\\null')) {
$reflectionTypeString = 'null';
} elseif (str_ends_with($loweredReflectionTypeString, '\\never')) {
$reflectionTypeString = 'never';
}
$type = self::getTypeObjectFromTypehint($reflectionTypeString, $selfClass);
if ($reflectionType->allowsNull()) {
$type = TypeCombinator::addNull($type);
} elseif ($phpDocType !== null) {
$phpDocType = TypeCombinator::removeNull($phpDocType);
}
return self::decideType($type, $phpDocType);
}
public static function decideType(
Type $type,
?Type $phpDocType = null,
): Type
{
if ($type instanceof BenevolentUnionType) {
return $type;
}
if ($phpDocType !== null && !$phpDocType instanceof ErrorType) {
if ($phpDocType instanceof NeverType && $phpDocType->isExplicit()) {
return $phpDocType;
}
if (
$type instanceof MixedType
&& !$type->isExplicitMixed()
&& $phpDocType->isVoid()->yes()
) {
return $phpDocType;
}
if (TypeCombinator::removeNull($type) instanceof IterableType) {
if ($phpDocType instanceof UnionType) {
$innerTypes = [];
foreach ($phpDocType->getTypes() as $innerType) {
if ($innerType instanceof ArrayType) {
$innerTypes[] = new IterableType(
$innerType->getIterableKeyType(),
$innerType->getItemType(),
);
} else {
$innerTypes[] = $innerType;
}
}
$phpDocType = new UnionType($innerTypes);
} elseif ($phpDocType instanceof ArrayType) {
$phpDocType = new IterableType(
$phpDocType->getKeyType(),
$phpDocType->getItemType(),
);
}
}
if (
($type->isCallable()->yes() && $phpDocType->isCallable()->yes())
|| (
(!$phpDocType instanceof NeverType || ($type instanceof MixedType && !$type->isExplicitMixed()))
&& $type->isSuperTypeOf(TemplateTypeHelper::resolveToBounds($phpDocType))->yes()
)
) {
$resultType = $phpDocType;
} else {
$resultType = $type;
}
if ($type instanceof UnionType) {
$addToUnionTypes = [];
foreach ($type->getTypes() as $innerType) {
if (!$innerType->isSuperTypeOf($resultType)->no()) {
continue;
}
$addToUnionTypes[] = $innerType;
}
if (count($addToUnionTypes) > 0) {
$type = TypeCombinator::union($resultType, ...$addToUnionTypes);
} else {
$type = $resultType;
}
} elseif (TypeCombinator::containsNull($type)) {
$type = TypeCombinator::addNull($resultType);
} else {
$type = $resultType;
}
}
return $type;
}
}