-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTypeFactoryTrait.php
More file actions
463 lines (391 loc) · 13.3 KB
/
TypeFactoryTrait.php
File metadata and controls
463 lines (391 loc) · 13.3 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\TypeInfo;
use Symfony\Component\TypeInfo\Type\ArrayShapeType;
use Symfony\Component\TypeInfo\Type\BackedEnumType;
use Symfony\Component\TypeInfo\Type\BuiltinType;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\EnumType;
use Symfony\Component\TypeInfo\Type\GenericType;
use Symfony\Component\TypeInfo\Type\IntersectionType;
use Symfony\Component\TypeInfo\Type\NullableType;
use Symfony\Component\TypeInfo\Type\ObjectShapeType;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\Type\TemplateType;
use Symfony\Component\TypeInfo\Type\UnionType;
/**
* Helper trait to create any type easily.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
* @author Baptiste Leduc <baptiste.leduc@gmail.com>
*/
trait TypeFactoryTrait
{
/**
* @template T of TypeIdentifier
* @template U value-of<T>
*
* @param T|U $identifier
*
* @return BuiltinType<T>
*/
public static function builtin(TypeIdentifier|string $identifier): BuiltinType
{
/** @var T $identifier */
$identifier = \is_string($identifier) ? TypeIdentifier::from($identifier) : $identifier;
return new BuiltinType($identifier);
}
/**
* @return BuiltinType<TypeIdentifier::INT>
*/
public static function int(): BuiltinType
{
return self::builtin(TypeIdentifier::INT);
}
/**
* @return BuiltinType<TypeIdentifier::FLOAT>
*/
public static function float(): BuiltinType
{
return self::builtin(TypeIdentifier::FLOAT);
}
/**
* @return BuiltinType<TypeIdentifier::STRING>
*/
public static function string(): BuiltinType
{
return self::builtin(TypeIdentifier::STRING);
}
/**
* @return BuiltinType<TypeIdentifier::BOOL>
*/
public static function bool(): BuiltinType
{
return self::builtin(TypeIdentifier::BOOL);
}
/**
* @return BuiltinType<TypeIdentifier::RESOURCE>
*/
public static function resource(): BuiltinType
{
return self::builtin(TypeIdentifier::RESOURCE);
}
/**
* @return BuiltinType<TypeIdentifier::FALSE>
*/
public static function false(): BuiltinType
{
return self::builtin(TypeIdentifier::FALSE);
}
/**
* @return BuiltinType<TypeIdentifier::TRUE>
*/
public static function true(): BuiltinType
{
return self::builtin(TypeIdentifier::TRUE);
}
/**
* @return BuiltinType<TypeIdentifier::CALLABLE>
*/
public static function callable(): BuiltinType
{
return self::builtin(TypeIdentifier::CALLABLE);
}
/**
* @return BuiltinType<TypeIdentifier::MIXED>
*/
public static function mixed(): BuiltinType
{
return self::builtin(TypeIdentifier::MIXED);
}
/**
* @return BuiltinType<TypeIdentifier::NULL>
*/
public static function null(): BuiltinType
{
return self::builtin(TypeIdentifier::NULL);
}
/**
* @return BuiltinType<TypeIdentifier::VOID>
*/
public static function void(): BuiltinType
{
return self::builtin(TypeIdentifier::VOID);
}
/**
* @return BuiltinType<TypeIdentifier::NEVER>
*/
public static function never(): BuiltinType
{
return self::builtin(TypeIdentifier::NEVER);
}
/**
* @template T of BuiltinType<TypeIdentifier::ARRAY>|BuiltinType<TypeIdentifier::ITERABLE>|ObjectType|GenericType
*
* @param T $type
*
* @return CollectionType<T>
*/
public static function collection(BuiltinType|ObjectType|GenericType $type, ?Type $value = null, ?Type $key = null, bool $asList = false): CollectionType
{
if (!$type instanceof GenericType && (null !== $value || null !== $key)) {
$type = self::generic($type, $key ?? self::arrayKey(), $value ?? self::mixed());
}
return new CollectionType($type, $asList);
}
/**
* @return CollectionType<BuiltinType<TypeIdentifier::ARRAY>>
*/
public static function array(?Type $value = null, ?Type $key = null, bool $asList = false): CollectionType
{
return self::collection(self::builtin(TypeIdentifier::ARRAY), $value, $key, $asList);
}
/**
* @return CollectionType<BuiltinType<TypeIdentifier::ITERABLE>>
*/
public static function iterable(?Type $value = null, ?Type $key = null): CollectionType
{
return self::collection(self::builtin(TypeIdentifier::ITERABLE), $value, $key);
}
/**
* @return CollectionType<BuiltinType<TypeIdentifier::ARRAY>>
*/
public static function list(?Type $value = null): CollectionType
{
return self::array($value, self::int(), asList: true);
}
/**
* @return CollectionType<BuiltinType<TypeIdentifier::ARRAY>>
*/
public static function dict(?Type $value = null): CollectionType
{
return self::array($value, self::string());
}
/**
* @param array<array{type: Type, optional?: bool}|Type> $shape
*/
public static function arrayShape(array $shape, bool $sealed = true, ?Type $extraKeyType = null, ?Type $extraValueType = null): ArrayShapeType
{
$shape = array_map(static fn (array|Type $item): array => $item instanceof Type
? ['type' => $item, 'optional' => false]
: ['type' => $item['type'], 'optional' => $item['optional'] ?? false], $shape);
if ($extraKeyType || $extraValueType) {
$sealed = false;
}
$extraKeyType ??= !$sealed ? Type::arrayKey() : null;
$extraValueType ??= !$sealed ? Type::mixed() : null;
return new ArrayShapeType($shape, $extraKeyType, $extraValueType);
}
public static function arrayKey(): UnionType
{
return self::union(self::int(), self::string());
}
/**
* @template T of class-string
*
* @param T|null $className
*
* @return ($className is class-string ? ObjectType<T> : BuiltinType<TypeIdentifier::OBJECT>)
*/
public static function object(?string $className = null): BuiltinType|ObjectType
{
return null !== $className ? new ObjectType($className) : new BuiltinType(TypeIdentifier::OBJECT);
}
/**
* @param array<string, array{type: Type, optional?: bool}|Type> $shape
*/
public static function objectShape(array $shape): ObjectShapeType
{
$shape = array_map(
static fn (array|Type $item): array => $item instanceof Type
? ['type' => $item, 'optional' => false]
: ['type' => $item['type'], 'optional' => $item['optional'] ?? false],
$shape
);
return new ObjectShapeType($shape);
}
/**
* @template T of class-string<\UnitEnum>|class-string<\BackedEnum>
* @template U of BuiltinType<TypeIdentifier::INT>|BuiltinType<TypeIdentifier::STRING>
*
* @param T $className
* @param U|null $backingType
*
* @return ($className is class-string<\BackedEnum> ? ($backingType is U ? BackedEnumType<T, U> : BackedEnumType<T, BuiltinType<TypeIdentifier::INT>|BuiltinType<TypeIdentifier::STRING>>) : EnumType<T>))
*/
public static function enum(string $className, ?BuiltinType $backingType = null): EnumType
{
if (is_subclass_of($className, \BackedEnum::class)) {
if (null === $backingType) {
$reflectionBackingType = (new \ReflectionEnum($className))->getBackingType();
$typeIdentifier = TypeIdentifier::INT->value === (string) $reflectionBackingType ? TypeIdentifier::INT : TypeIdentifier::STRING;
$backingType = new BuiltinType($typeIdentifier);
}
return new BackedEnumType($className, $backingType);
}
return new EnumType($className);
}
/**
* @template T of BuiltinType<TypeIdentifier::ARRAY>|BuiltinType<TypeIdentifier::ITERABLE>|ObjectType
*
* @param T $mainType
*
* @return GenericType<T>
*/
public static function generic(BuiltinType|ObjectType $mainType, Type ...$variableTypes): GenericType
{
return new GenericType($mainType, ...$variableTypes);
}
/**
* @template T of Type
*
* @param T|null $bound
*
* @return ($bound is null ? TemplateType<BuiltinType<TypeIdentifier::MIXED>> : TemplateType<T>)
*/
public static function template(string $name, ?Type $bound = null): TemplateType
{
return new TemplateType($name, $bound ?? Type::mixed());
}
/**
* @template T of Type
*
* @param list<T> $types
*
* @return UnionType<T>|NullableType<T>
*/
public static function union(Type ...$types): UnionType
{
/** @var list<T> $unionTypes */
$unionTypes = [];
$nullableUnion = false;
$isNullable = static fn (Type $type): bool => $type instanceof BuiltinType && TypeIdentifier::NULL === $type->getTypeIdentifier();
foreach ($types as $type) {
if ($type instanceof NullableType) {
$nullableUnion = true;
$type = $type->getWrappedType();
}
if ($type instanceof UnionType) {
foreach ($type->getTypes() as $unionType) {
if ($isNullable($unionType)) {
$nullableUnion = true;
continue;
}
$unionTypes[] = $unionType;
}
continue;
}
if ($isNullable($type)) {
$nullableUnion = true;
continue;
}
$unionTypes[] = $type;
}
if (1 === \count($unionTypes)) {
return self::nullable($unionTypes[0]);
}
$unionType = new UnionType(...$unionTypes);
return $nullableUnion ? self::nullable($unionType) : $unionType;
}
/**
* @template T of ObjectType|GenericType<ObjectType>|CollectionType<GenericType<ObjectType>>
*
* @param list<T|IntersectionType<T>> $types
*
* @return IntersectionType<T>
*/
public static function intersection(Type ...$types): IntersectionType
{
/** @var list<T> $intersectionTypes */
$intersectionTypes = [];
foreach ($types as $type) {
if (!$type instanceof IntersectionType) {
$intersectionTypes[] = $type;
continue;
}
foreach ($type->getTypes() as $intersectionType) {
$intersectionTypes[] = $intersectionType;
}
}
return new IntersectionType(...$intersectionTypes);
}
/**
* @template T of Type
*
* @param T $type
*
* @return T|NullableType<T>
*/
public static function nullable(Type $type): Type
{
if ($type->isNullable()) {
return $type;
}
return new NullableType($type);
}
public static function fromValue(mixed $value): Type
{
$type = match ($value) {
null => self::null(),
true => self::true(),
false => self::false(),
default => null,
};
if (null !== $type) {
return $type;
}
if (\is_callable($value)) {
return Type::callable();
}
if (\is_resource($value)) {
return Type::resource();
}
$type = match (get_debug_type($value)) {
TypeIdentifier::INT->value => self::int(),
TypeIdentifier::FLOAT->value => self::float(),
TypeIdentifier::STRING->value => self::string(),
default => null,
};
if (null !== $type) {
return $type;
}
$type = match (true) {
$value instanceof \UnitEnum => Type::enum($value::class),
\is_object($value) => \stdClass::class === $value::class ? self::object() : self::object($value::class),
\is_array($value) => self::builtin(TypeIdentifier::ARRAY),
default => null,
};
if (null === $type) {
return Type::mixed();
}
if (is_iterable($value)) {
/** @var list<BuiltinType<TypeIdentifier::INT>|BuiltinType<TypeIdentifier::STRING>> $keyTypes */
$keyTypes = [];
/** @var list<Type> $valueTypes */
$valueTypes = [];
foreach ($value as $k => $v) {
$keyTypes[] = self::fromValue($k);
$valueTypes[] = self::fromValue($v);
}
if ($keyTypes) {
$keyTypes = array_values(array_unique($keyTypes));
$keyType = \count($keyTypes) > 1 ? self::union(...$keyTypes) : $keyTypes[0];
} else {
$keyType = Type::arrayKey();
}
$valueType = $valueTypes ? CollectionType::mergeCollectionValueTypes($valueTypes) : Type::mixed();
return self::collection($type, $valueType, $keyType, \is_array($value) && [] !== $value && array_is_list($value));
}
if ($value instanceof \ArrayAccess) {
return self::collection($type);
}
return $type;
}
}