Debian Codegraph @

vendor/symfony/type-info/TypeResolver/ReflectionParameterTypeResolver.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 parameter reflection.
21 *
22 * @author Mathias Arlaud <[email protected]>
23 * @author Baptiste Leduc <[email protected]>
24 */
25 final class ReflectionParameterTypeResolver 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 \ReflectionParameter) {
36 throw new UnsupportedException(\sprintf('Expected subject to be a "ReflectionParameter", "%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 = null !== $typeContext
45 ? \sprintf('%s::%s($%s)', $typeContext->calledClassName, $subject->getDeclaringFunction()->getName(), $subject->getName())
46 : \sprintf('%s($%s)', $subject->getDeclaringFunction()->getName(), $subject->getName());
47
48 throw new UnsupportedException(\sprintf('Cannot resolve type for "%s".', $path), $subject, previous: $e);
49 }
50 }
51 }