Debian Codegraph @

vendor/symfony/type-info/TypeResolver/ReflectionPropertyTypeResolver.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\UnsupportedException;
15 use Symfony\Component\TypeInfo\Type;
16 use Symfony\Component\TypeInfo\TypeContext\TypeContext;
17 use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory;
18
19 /**
20 * Resolves type for a given property reflection.
21 *
22 * @author Mathias Arlaud <[email protected]>
23 * @author Baptiste Leduc <[email protected]>
24 */
25 final class ReflectionPropertyTypeResolver implements TypeResolverInterface
26 {
27 public function __construct(
28 private readonly ReflectionTypeResolver $reflectionTypeResolver,
29 private readonly TypeContextFactory $typeContextFactory,
30 ) {
31 }
32
33 public function resolve(mixed $subject, ?TypeContext $typeContext = null): Type
34 {
35 if (!$subject instanceof \ReflectionProperty) {
36 throw new UnsupportedException(\sprintf('Expected subject to be a "ReflectionProperty", "%s" given.', get_debug_type($subject)), $subject);
37 }
38
39 $typeContext ??= $this->typeContextFactory->createFromReflection($subject);
40
41 try {
42 return $this->reflectionTypeResolver->resolve($subject->getType(), $typeContext);
43 } catch (UnsupportedException $e) {
44 $path = \sprintf('%s::$%s', $subject->getDeclaringClass()->getName(), $subject->getName());
45
46 throw new UnsupportedException(\sprintf('Cannot resolve type for "%s".', $path), $subject, previous: $e);
47 }
48 }
49 }