Debian Codegraph @

vendor/symfony/type-info/TypeFactoryTrait.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\TypeInfo;
13
14 use Symfony\Component\TypeInfo\Type\ArrayShapeType;
15 use Symfony\Component\TypeInfo\Type\BackedEnumType;
16 use Symfony\Component\TypeInfo\Type\BuiltinType;
17 use Symfony\Component\TypeInfo\Type\CollectionType;
18 use Symfony\Component\TypeInfo\Type\EnumType;
19 use Symfony\Component\TypeInfo\Type\GenericType;
20 use Symfony\Component\TypeInfo\Type\IntersectionType;
21 use Symfony\Component\TypeInfo\Type\NullableType;
22 use Symfony\Component\TypeInfo\Type\ObjectType;
23 use Symfony\Component\TypeInfo\Type\TemplateType;
24 use Symfony\Component\TypeInfo\Type\UnionType;
25
26 /**
27 * Helper trait to create any type easily.
28 *
29 * @author Mathias Arlaud <[email protected]>
30 * @author Baptiste Leduc <[email protected]>
31 */
32 trait TypeFactoryTrait
33 {
34 /**
35 * @template T of TypeIdentifier
36 * @template U value-of<T>
37 *
38 * @param T|U $identifier
39 *
40 * @return BuiltinType<T>
41 */
42 public static function builtin(TypeIdentifier|string $identifier): BuiltinType
43 {
44 /** @var T $identifier */
45 $identifier = \is_string($identifier) ? TypeIdentifier::from($identifier) : $identifier;
46
47 return new BuiltinType($identifier);
48 }
49
50 /**
51 * @return BuiltinType<TypeIdentifier::INT>
52 */
53 public static function int(): BuiltinType
54 {
55 return self::builtin(TypeIdentifier::INT);
56 }
57
58 /**
59 * @return BuiltinType<TypeIdentifier::FLOAT>
60 */
61 public static function float(): BuiltinType
62 {
63 return self::builtin(TypeIdentifier::FLOAT);
64 }
65
66 /**
67 * @return BuiltinType<TypeIdentifier::STRING>
68 */
69 public static function string(): BuiltinType
70 {
71 return self::builtin(TypeIdentifier::STRING);
72 }
73
74 /**
75 * @return BuiltinType<TypeIdentifier::BOOL>
76 */
77 public static function bool(): BuiltinType
78 {
79 return self::builtin(TypeIdentifier::BOOL);
80 }
81
82 /**
83 * @return BuiltinType<TypeIdentifier::RESOURCE>
84 */
85 public static function resource(): BuiltinType
86 {
87 return self::builtin(TypeIdentifier::RESOURCE);
88 }
89
90 /**
91 * @return BuiltinType<TypeIdentifier::FALSE>
92 */
93 public static function false(): BuiltinType
94 {
95 return self::builtin(TypeIdentifier::FALSE);
96 }
97
98 /**
99 * @return BuiltinType<TypeIdentifier::TRUE>
100 */
101 public static function true(): BuiltinType
102 {
103 return self::builtin(TypeIdentifier::TRUE);
104 }
105
106 /**
107 * @return BuiltinType<TypeIdentifier::CALLABLE>
108 */
109 public static function callable(): BuiltinType
110 {
111 return self::builtin(TypeIdentifier::CALLABLE);
112 }
113
114 /**
115 * @return BuiltinType<TypeIdentifier::MIXED>
116 */
117 public static function mixed(): BuiltinType
118 {
119 return self::builtin(TypeIdentifier::MIXED);
120 }
121
122 /**
123 * @return BuiltinType<TypeIdentifier::NULL>
124 */
125 public static function null(): BuiltinType
126 {
127 return self::builtin(TypeIdentifier::NULL);
128 }
129
130 /**
131 * @return BuiltinType<TypeIdentifier::VOID>
132 */
133 public static function void(): BuiltinType
134 {
135 return self::builtin(TypeIdentifier::VOID);
136 }
137
138 /**
139 * @return BuiltinType<TypeIdentifier::NEVER>
140 */
141 public static function never(): BuiltinType
142 {
143 return self::builtin(TypeIdentifier::NEVER);
144 }
145
146 /**
147 * @template T of BuiltinType<TypeIdentifier::ARRAY>|BuiltinType<TypeIdentifier::ITERABLE>|ObjectType|GenericType
148 *
149 * @param T $type
150 *
151 * @return CollectionType<T>
152 */
153 public static function collection(BuiltinType|ObjectType|GenericType $type, ?Type $value = null, ?Type $key = null, bool $asList = false): CollectionType
154 {
155 if (!$type instanceof GenericType && (null !== $value || null !== $key)) {
156 $type = self::generic($type, $key ?? self::arrayKey(), $value ?? self::mixed());
157 }
158
159 return new CollectionType($type, $asList);
160 }
161
162 /**
163 * @return CollectionType<BuiltinType<TypeIdentifier::ARRAY>>
164 */
165 public static function array(?Type $value = null, ?Type $key = null, bool $asList = false): CollectionType
166 {
167 return self::collection(self::builtin(TypeIdentifier::ARRAY), $value, $key, $asList);
168 }
169
170 /**
171 * @return CollectionType<BuiltinType<TypeIdentifier::ITERABLE>>
172 */
173 public static function iterable(?Type $value = null, ?Type $key = null, bool $asList = false): CollectionType
174 {
175 if ($asList) {
176 trigger_deprecation('symfony/type-info', '7.3', 'The third argument of "%s()" is deprecated. Use the "%s::list()" method to create a list instead.', __METHOD__, self::class);
177 }
178
179 return self::collection(self::builtin(TypeIdentifier::ITERABLE), $value, $key, $asList);
180 }
181
182 /**
183 * @return CollectionType<BuiltinType<TypeIdentifier::ARRAY>>
184 */
185 public static function list(?Type $value = null): CollectionType
186 {
187 return self::array($value, self::int(), asList: true);
188 }
189
190 /**
191 * @return CollectionType<BuiltinType<TypeIdentifier::ARRAY>>
192 */
193 public static function dict(?Type $value = null): CollectionType
194 {
195 return self::array($value, self::string());
196 }
197
198 /**
199 * @param array<array{type: Type, optional?: bool}|Type> $shape
200 */
201 public static function arrayShape(array $shape, bool $sealed = true, ?Type $extraKeyType = null, ?Type $extraValueType = null): ArrayShapeType
202 {
203 $shape = array_map(static fn (array|Type $item): array => $item instanceof Type
204 ? ['type' => $item, 'optional' => false]
205 : ['type' => $item['type'], 'optional' => $item['optional'] ?? false], $shape);
206
207 if ($extraKeyType || $extraValueType) {
208 $sealed = false;
209 }
210
211 $extraKeyType ??= !$sealed ? Type::arrayKey() : null;
212 $extraValueType ??= !$sealed ? Type::mixed() : null;
213
214 return new ArrayShapeType($shape, $extraKeyType, $extraValueType);
215 }
216
217 public static function arrayKey(): UnionType
218 {
219 return self::union(self::int(), self::string());
220 }
221
222 /**
223 * @template T of class-string
224 *
225 * @param T|null $className
226 *
227 * @return ($className is class-string ? ObjectType<T> : BuiltinType<TypeIdentifier::OBJECT>)
228 */
229 public static function object(?string $className = null): BuiltinType|ObjectType
230 {
231 return null !== $className ? new ObjectType($className) : new BuiltinType(TypeIdentifier::OBJECT);
232 }
233
234 /**
235 * @template T of class-string<\UnitEnum>|class-string<\BackedEnum>
236 * @template U of BuiltinType<TypeIdentifier::INT>|BuiltinType<TypeIdentifier::STRING>
237 *
238 * @param T $className
239 * @param U|null $backingType
240 *
241 * @return ($className is class-string<\BackedEnum> ? ($backingType is U ? BackedEnumType<T, U> : BackedEnumType<T, BuiltinType<TypeIdentifier::INT>|BuiltinType<TypeIdentifier::STRING>>) : EnumType<T>))
242 */
243 public static function enum(string $className, ?BuiltinType $backingType = null): EnumType
244 {
245 if (is_subclass_of($className, \BackedEnum::class)) {
246 if (null === $backingType) {
247 $reflectionBackingType = (new \ReflectionEnum($className))->getBackingType();
248 $typeIdentifier = TypeIdentifier::INT->value === (string) $reflectionBackingType ? TypeIdentifier::INT : TypeIdentifier::STRING;
249 $backingType = new BuiltinType($typeIdentifier);
250 }
251
252 return new BackedEnumType($className, $backingType);
253 }
254
255 return new EnumType($className);
256 }
257
258 /**
259 * @template T of BuiltinType<TypeIdentifier::ARRAY>|BuiltinType<TypeIdentifier::ITERABLE>|ObjectType
260 *
261 * @param T $mainType
262 *
263 * @return GenericType<T>
264 */
265 public static function generic(BuiltinType|ObjectType $mainType, Type ...$variableTypes): GenericType
266 {
267 return new GenericType($mainType, ...$variableTypes);
268 }
269
270 /**
271 * @template T of Type
272 *
273 * @param T|null $bound
274 *
275 * @return ($bound is null ? TemplateType<BuiltinType<TypeIdentifier::MIXED>> : TemplateType<T>)
276 */
277 public static function template(string $name, ?Type $bound = null): TemplateType
278 {
279 return new TemplateType($name, $bound ?? Type::mixed());
280 }
281
282 /**
283 * @template T of Type
284 *
285 * @param list<T> $types
286 *
287 * @return UnionType<T>|NullableType<T>
288 */
289 public static function union(Type ...$types): UnionType
290 {
291 /** @var list<T> $unionTypes */
292 $unionTypes = [];
293
294 $nullableUnion = false;
295 $isNullable = static fn (Type $type): bool => $type instanceof BuiltinType && TypeIdentifier::NULL === $type->getTypeIdentifier();
296
297 foreach ($types as $type) {
298 if ($type instanceof NullableType) {
299 $nullableUnion = true;
300 $type = $type->getWrappedType();
301 }
302
303 if ($type instanceof UnionType) {
304 foreach ($type->getTypes() as $unionType) {
305 if ($isNullable($unionType)) {
306 $nullableUnion = true;
307
308 continue;
309 }
310
311 $unionTypes[] = $unionType;
312 }
313
314 continue;
315 }
316
317 if ($isNullable($type)) {
318 $nullableUnion = true;
319
320 continue;
321 }
322
323 $unionTypes[] = $type;
324 }
325
326 if (1 === \count($unionTypes)) {
327 return self::nullable($unionTypes[0]);
328 }
329
330 $unionType = new UnionType(...$unionTypes);
331
332 return $nullableUnion ? self::nullable($unionType) : $unionType;
333 }
334
335 /**
336 * @template T of ObjectType|GenericType<ObjectType>|CollectionType<GenericType<ObjectType>>
337 *
338 * @param list<T|IntersectionType<T>> $types
339 *
340 * @return IntersectionType<T>
341 */
342 public static function intersection(Type ...$types): IntersectionType
343 {
344 /** @var list<T> $intersectionTypes */
345 $intersectionTypes = [];
346
347 foreach ($types as $type) {
348 if (!$type instanceof IntersectionType) {
349 $intersectionTypes[] = $type;
350
351 continue;
352 }
353
354 foreach ($type->getTypes() as $intersectionType) {
355 $intersectionTypes[] = $intersectionType;
356 }
357 }
358
359 return new IntersectionType(...$intersectionTypes);
360 }
361
362 /**
363 * @template T of Type
364 *
365 * @param T $type
366 *
367 * @return T|NullableType<T>
368 */
369 public static function nullable(Type $type): Type
370 {
371 if ($type->isNullable()) {
372 return $type;
373 }
374
375 return new NullableType($type);
376 }
377
378 public static function fromValue(mixed $value): Type
379 {
380 $type = match ($value) {
381 null => self::null(),
382 true => self::true(),
383 false => self::false(),
384 default => null,
385 };
386
387 if (null !== $type) {
388 return $type;
389 }
390
391 if (\is_callable($value)) {
392 return Type::callable();
393 }
394
395 if (\is_resource($value)) {
396 return Type::resource();
397 }
398
399 $type = match (get_debug_type($value)) {
400 TypeIdentifier::INT->value => self::int(),
401 TypeIdentifier::FLOAT->value => self::float(),
402 TypeIdentifier::STRING->value => self::string(),
403 default => null,
404 };
405
406 if (null !== $type) {
407 return $type;
408 }
409
410 $type = match (true) {
411 $value instanceof \UnitEnum => Type::enum($value::class),
412 \is_object($value) => \stdClass::class === $value::class ? self::object() : self::object($value::class),
413 \is_array($value) => self::builtin(TypeIdentifier::ARRAY),
414 default => null,
415 };
416
417 if (null === $type) {
418 return Type::mixed();
419 }
420
421 if (is_iterable($value)) {
422 /** @var list<BuiltinType<TypeIdentifier::INT>|BuiltinType<TypeIdentifier::STRING>> $keyTypes */
423 $keyTypes = [];
424
425 /** @var list<Type> $valueTypes */
426 $valueTypes = [];
427
428 foreach ($value as $k => $v) {
429 $keyTypes[] = self::fromValue($k);
430 $valueTypes[] = self::fromValue($v);
431 }
432
433 if ($keyTypes) {
434 $keyTypes = array_values(array_unique($keyTypes));
435 $keyType = \count($keyTypes) > 1 ? self::union(...$keyTypes) : $keyTypes[0];
436 } else {
437 $keyType = Type::arrayKey();
438 }
439
440 $valueType = $valueTypes ? CollectionType::mergeCollectionValueTypes($valueTypes) : Type::mixed();
441
442 return self::collection($type, $valueType, $keyType, \is_array($value) && [] !== $value && array_is_list($value));
443 }
444
445 if ($value instanceof \ArrayAccess) {
446 return self::collection($type);
447 }
448
449 return $type;
450 }
451 }