Debian Codegraph @

vendor/symfony/type-info/TypeResolver/ReflectionTypeResolver.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\TypeResolver;
13
14 use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
15 use Symfony\Component\TypeInfo\Exception\UnsupportedException;
16 use Symfony\Component\TypeInfo\Type;
17 use Symfony\Component\TypeInfo\TypeContext\TypeContext;
18 use Symfony\Component\TypeInfo\TypeIdentifier;
19
20 /**
21 * Resolves type for a given type reflection.
22 *
23 * @author Mathias Arlaud <[email protected]>
24 * @author Baptiste Leduc <[email protected]>
25 */
26 final class ReflectionTypeResolver implements TypeResolverInterface
27 {
28 public function resolve(mixed $subject, ?TypeContext $typeContext = null): Type
29 {
30 if ($subject instanceof \ReflectionUnionType) {
31 return Type::union(...array_map(fn (mixed $t): Type => $this->resolve($t, $typeContext), $subject->getTypes()));
32 }
33
34 if ($subject instanceof \ReflectionIntersectionType) {
35 return Type::intersection(...array_map(fn (mixed $t): Type => $this->resolve($t, $typeContext), $subject->getTypes()));
36 }
37
38 if (!$subject instanceof \ReflectionNamedType) {
39 throw new UnsupportedException(\sprintf('Expected subject to be a "ReflectionNamedType", a "ReflectionUnionType" or a "ReflectionIntersectionType", "%s" given.', get_debug_type($subject)), $subject);
40 }
41
42 $identifier = $subject->getName();
43 $nullable = $subject->allowsNull();
44
45 if (TypeIdentifier::ARRAY->value === $identifier) {
46 $type = Type::array();
47
48 return $nullable ? Type::nullable($type) : $type;
49 }
50
51 if (TypeIdentifier::ITERABLE->value === $identifier) {
52 $type = Type::iterable();
53
54 return $nullable ? Type::nullable($type) : $type;
55 }
56
57 if (TypeIdentifier::NULL->value === $identifier || TypeIdentifier::MIXED->value === $identifier) {
58 return Type::builtin($identifier);
59 }
60
61 if ($subject->isBuiltin()) {
62 $type = Type::builtin(TypeIdentifier::from($identifier));
63
64 return $nullable ? Type::nullable($type) : $type;
65 }
66
67 if (\in_array(strtolower($identifier), ['self', 'static', 'parent'], true) && !$typeContext) {
68 throw new InvalidArgumentException(\sprintf('A "%s" must be provided to resolve "%s".', TypeContext::class, strtolower($identifier)));
69 }
70
71 /** @var class-string $className */
72 $className = match (true) {
73 'self' === strtolower($identifier) => $typeContext->getDeclaringClass(),
74 'static' === strtolower($identifier) => $typeContext->getCalledClass(),
75 'parent' === strtolower($identifier) => $typeContext->getParentClass(),
76 default => $identifier,
77 };
78
79 if (is_subclass_of($className, \UnitEnum::class) && !interface_exists($className)) {
80 $type = Type::enum($className);
81 } else {
82 $type = Type::object($className);
83 }
84
85 return $nullable ? Type::nullable($type) : $type;
86 }
87 }