-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
UnionTypeMapper.php
395 lines (324 loc) · 13.2 KB
/
UnionTypeMapper.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Identifier;
use PhpParser\Node\IntersectionType as PHPParserNodeIntersectionType;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PhpParser\NodeAbstract;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use PHPStan\Type\VoidType;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\BoolUnionTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeCommonTypeNarrower;
use Rector\PHPStanStaticTypeMapper\ValueObject\UnionTypeAnalysis;
use Symfony\Contracts\Service\Attribute\Required;
/**
* @implements TypeMapperInterface<UnionType>
*/
final class UnionTypeMapper implements TypeMapperInterface
{
private PHPStanStaticTypeMapper $phpStanStaticTypeMapper;
public function __construct(
private readonly DoctrineTypeAnalyzer $doctrineTypeAnalyzer,
private readonly PhpVersionProvider $phpVersionProvider,
private readonly UnionTypeAnalyzer $unionTypeAnalyzer,
private readonly BoolUnionTypeAnalyzer $boolUnionTypeAnalyzer,
private readonly UnionTypeCommonTypeNarrower $unionTypeCommonTypeNarrower,
private readonly NodeNameResolver $nodeNameResolver
) {
}
#[Required]
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}
/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return UnionType::class;
}
/**
* @param UnionType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNode
{
$unionTypesNodes = [];
$skipIterable = $this->shouldSkipIterable($type);
foreach ($type->getTypes() as $unionedType) {
if ($unionedType instanceof IterableType && $skipIterable) {
continue;
}
$unionTypesNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType, $typeKind);
}
$unionTypesNodes = array_unique($unionTypesNodes);
return new BracketsAwareUnionTypeNode($unionTypesNodes);
}
/**
* @param UnionType $type
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
$arrayNode = $this->matchArrayTypes($type);
if ($arrayNode !== null) {
return $arrayNode;
}
if ($this->boolUnionTypeAnalyzer->isNullableBoolUnionType(
$type
) && ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
return new NullableType(new Name('bool'));
}
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES) && $this->isFalseBoolUnion(
$type
)) {
// return new Bool
return new Name('bool');
}
// special case for nullable
$nullabledType = $this->matchTypeForNullableUnionType($type);
if (! $nullabledType instanceof Type) {
// use first unioned type in case of unioned object types
return $this->matchTypeForUnionedObjectTypes($type, $typeKind);
}
// void cannot be nullable
if ($nullabledType instanceof VoidType) {
return null;
}
$nullabledTypeNode = $this->phpStanStaticTypeMapper->mapToPhpParserNode($nullabledType, $typeKind);
if (! $nullabledTypeNode instanceof Node) {
return null;
}
if (in_array($nullabledTypeNode::class, [NullableType::class, ComplexType::class], true)) {
return $nullabledTypeNode;
}
/** @var Name $nullabledTypeNode */
if (! $this->nodeNameResolver->isName($nullabledTypeNode, 'false')) {
return new NullableType($nullabledTypeNode);
}
return null;
}
private function shouldSkipIterable(UnionType $unionType): bool
{
$unionTypeAnalysis = $this->unionTypeAnalyzer->analyseForNullableAndIterable($unionType);
if (! $unionTypeAnalysis instanceof UnionTypeAnalysis) {
return false;
}
if (! $unionTypeAnalysis->hasIterable()) {
return false;
}
return $unionTypeAnalysis->hasArray();
}
private function matchArrayTypes(UnionType $unionType): Name | NullableType | null
{
$unionTypeAnalysis = $this->unionTypeAnalyzer->analyseForNullableAndIterable($unionType);
if (! $unionTypeAnalysis instanceof UnionTypeAnalysis) {
return null;
}
$type = $unionTypeAnalysis->hasIterable() ? 'iterable' : 'array';
if ($unionTypeAnalysis->isNullableType()) {
return new NullableType($type);
}
return new Name($type);
}
private function matchTypeForNullableUnionType(UnionType $unionType): ?Type
{
if (count($unionType->getTypes()) !== 2) {
return null;
}
$firstType = $unionType->getTypes()[0];
$secondType = $unionType->getTypes()[1];
if ($firstType instanceof NullType) {
return $secondType;
}
if ($secondType instanceof NullType) {
return $firstType;
}
return null;
}
private function hasObjectAndStaticType(PhpParserUnionType $phpParserUnionType): bool
{
$typeNames = $this->nodeNameResolver->getNames($phpParserUnionType->types);
$diff = array_diff(['object', ObjectReference::STATIC], $typeNames);
return $diff === [];
}
/**
* @param TypeKind::* $typeKind
* @return Name|FullyQualified|PhpParserUnionType|NullableType|null
*/
private function matchTypeForUnionedObjectTypes(UnionType $unionType, string $typeKind): ?Node
{
$phpParserUnionType = $this->matchPhpParserUnionType($unionType, $typeKind);
if ($phpParserUnionType !== null) {
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
// maybe all one type?
if ($this->boolUnionTypeAnalyzer->isBoolUnionType($unionType)) {
return new Name('bool');
}
return null;
}
if ($this->hasObjectAndStaticType($phpParserUnionType)) {
return null;
}
return $phpParserUnionType;
}
if ($this->boolUnionTypeAnalyzer->isBoolUnionType($unionType)) {
return new Name('bool');
}
$compatibleObjectTypeNode = $this->processResolveCompatibleObjectCandidates($unionType);
if ($compatibleObjectTypeNode instanceof NullableType || $compatibleObjectTypeNode instanceof FullyQualified) {
return $compatibleObjectTypeNode;
}
return $this->processResolveCompatibleStringCandidates($unionType);
}
private function processResolveCompatibleStringCandidates(UnionType $unionType): ?Name
{
foreach ($unionType->getTypes() as $type) {
if (! in_array($type::class, [ClassStringType::class, GenericClassStringType::class], true)) {
return null;
}
}
return new Name('string');
}
private function processResolveCompatibleObjectCandidates(UnionType $unionType): ?Node
{
// the type should be compatible with all other types, e.g. A extends B, B
$compatibleObjectType = $this->resolveCompatibleObjectCandidate($unionType);
if ($compatibleObjectType instanceof UnionType) {
$type = $this->matchTypeForNullableUnionType($compatibleObjectType);
if ($type instanceof ObjectType) {
return new NullableType(new FullyQualified($type->getClassName()));
}
}
if (! $compatibleObjectType instanceof ObjectType) {
return null;
}
return new FullyQualified($compatibleObjectType->getClassName());
}
/**
* @param TypeKind::* $typeKind
*/
private function matchPhpParserUnionType(UnionType $unionType, string $typeKind): ?PhpParserUnionType
{
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
return null;
}
$phpParserUnionedTypes = [];
foreach ($unionType->getTypes() as $unionedType) {
// void type and mixed type are not allowed in union
if (in_array($unionedType::class, [MixedType::class, VoidType::class], true)) {
return null;
}
/**
* NullType inside UnionType is allowed
* make it on TypeKind property as changing other type, eg: return type may conflict with parent child implementation
*
* @var Identifier|Name|null|PHPParserNodeIntersectionType $phpParserNode
*/
$phpParserNode = $unionedType instanceof NullType && $typeKind === TypeKind::PROPERTY
? new Name('null')
: $this->phpStanStaticTypeMapper->mapToPhpParserNode($unionedType, $typeKind);
if ($phpParserNode === null) {
return null;
}
if ($phpParserNode instanceof PHPParserNodeIntersectionType && $unionedType instanceof IntersectionType) {
return null;
}
$phpParserUnionedTypes[] = $phpParserNode;
}
/** @var Identifier[]|Name[] $phpParserUnionedTypes */
$phpParserUnionedTypes = array_unique($phpParserUnionedTypes);
if (count($phpParserUnionedTypes) < 2) {
return null;
}
return new PhpParserUnionType($phpParserUnionedTypes);
}
private function resolveCompatibleObjectCandidate(UnionType $unionType): UnionType|TypeWithClassName|null
{
if ($this->doctrineTypeAnalyzer->isDoctrineCollectionWithIterableUnionType($unionType)) {
$objectType = new ObjectType('Doctrine\Common\Collections\Collection');
return $this->unionTypeAnalyzer->isNullable($unionType)
? new UnionType([new NullType(), $objectType])
: $objectType;
}
$typesWithClassNames = $this->unionTypeAnalyzer->matchExclusiveTypesWithClassNames($unionType);
if ($typesWithClassNames === []) {
return null;
}
$sharedTypeWithClassName = $this->matchTwoObjectTypes($typesWithClassNames);
if ($sharedTypeWithClassName instanceof TypeWithClassName) {
return $this->correctObjectType($sharedTypeWithClassName);
}
// find least common denominator
return $this->unionTypeCommonTypeNarrower->narrowToSharedObjectType($unionType);
}
/**
* @param TypeWithClassName[] $typesWithClassNames
*/
private function matchTwoObjectTypes(array $typesWithClassNames): ?TypeWithClassName
{
foreach ($typesWithClassNames as $typeWithClassName) {
foreach ($typesWithClassNames as $nestedTypeWithClassName) {
if (! $this->areTypeWithClassNamesRelated($typeWithClassName, $nestedTypeWithClassName)) {
continue 2;
}
}
return $typeWithClassName;
}
return null;
}
private function areTypeWithClassNamesRelated(TypeWithClassName $firstType, TypeWithClassName $secondType): bool
{
return $firstType->accepts($secondType, false)
->yes();
}
private function correctObjectType(TypeWithClassName $typeWithClassName): TypeWithClassName
{
if ($typeWithClassName->getClassName() === NodeAbstract::class) {
return new ObjectType('PhpParser\Node');
}
if ($typeWithClassName->getClassName() === AbstractRector::class) {
return new ObjectType('Rector\Core\Contract\Rector\RectorInterface');
}
return $typeWithClassName;
}
private function isFalseBoolUnion(UnionType $unionType): bool
{
if (count($unionType->getTypes()) !== 2) {
return false;
}
foreach ($unionType->getTypes() as $unionedType) {
if ($unionedType instanceof ConstantBooleanType) {
continue;
}
return false;
}
return true;
}
}