| 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 PHPStan\PhpDocParser\Parser\PhpDocParser; |
| 15 |
use Psr\Container\ContainerInterface; |
| 16 |
use Symfony\Component\TypeInfo\Exception\UnsupportedException; |
| 17 |
use Symfony\Component\TypeInfo\Type; |
| 18 |
use Symfony\Component\TypeInfo\TypeContext\TypeContext; |
| 19 |
use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory; |
| 20 |
|
| 21 |
/** |
| 22 |
* Resolves type for a given subject by delegating resolving to nested type resolvers. |
| 23 |
* |
| 24 |
* @author Mathias Arlaud <[email protected]> |
| 25 |
* @author Baptiste Leduc <[email protected]> |
| 26 |
*/ |
| 27 |
final class TypeResolver implements TypeResolverInterface |
| 28 |
{ |
| 29 |
/** |
| 30 |
* @param ContainerInterface $resolvers Locator of type resolvers, keyed by supported subject type |
| 31 |
*/ |
| 32 |
public function __construct( |
| 33 |
private readonly ContainerInterface $resolvers, |
| 34 |
) { |
| 35 |
} |
| 36 |
|
| 37 |
public function resolve(mixed $subject, ?TypeContext $typeContext = null): Type |
| 38 |
{ |
| 39 |
$subjectType = match (\is_object($subject)) { |
| 40 |
true => match (true) { |
| 41 |
is_subclass_of($subject::class, \ReflectionType::class) => \ReflectionType::class, |
| 42 |
is_subclass_of($subject::class, \ReflectionFunctionAbstract::class) => \ReflectionFunctionAbstract::class, |
| 43 |
default => $subject::class, |
| 44 |
}, |
| 45 |
false => get_debug_type($subject), |
| 46 |
}; |
| 47 |
|
| 48 |
if (!$this->resolvers->has($subjectType)) { |
| 49 |
if ('string' === $subjectType) { |
| 50 |
throw new UnsupportedException('Cannot find any resolver for "string" type. Try running "composer require phpstan/phpdoc-parser".', $subject); |
| 51 |
} |
| 52 |
|
| 53 |
throw new UnsupportedException(\sprintf('Cannot find any resolver for "%s" type.', $subjectType), $subject); |
| 54 |
} |
| 55 |
|
| 56 |
/** @param TypeResolverInterface $resolver */ |
| 57 |
$resolver = $this->resolvers->get($subjectType); |
| 58 |
|
| 59 |
return $resolver->resolve($subject, $typeContext); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @param array<string, TypeResolverInterface>|null $resolvers |
| 64 |
*/ |
| 65 |
public static function create(?array $resolvers = null): self |
| 66 |
{ |
| 67 |
if (null === $resolvers) { |
| 68 |
$stringTypeResolver = class_exists(PhpDocParser::class) ? new StringTypeResolver() : null; |
| 69 |
$typeContextFactory = new TypeContextFactory($stringTypeResolver); |
| 70 |
$reflectionTypeResolver = new ReflectionTypeResolver(); |
| 71 |
|
| 72 |
$resolvers = [ |
| 73 |
\ReflectionType::class => $reflectionTypeResolver, |
| 74 |
\ReflectionParameter::class => new ReflectionParameterTypeResolver($reflectionTypeResolver, $typeContextFactory), |
| 75 |
\ReflectionProperty::class => new ReflectionPropertyTypeResolver($reflectionTypeResolver, $typeContextFactory), |
| 76 |
\ReflectionFunctionAbstract::class => new ReflectionReturnTypeResolver($reflectionTypeResolver, $typeContextFactory), |
| 77 |
]; |
| 78 |
|
| 79 |
if (null !== $stringTypeResolver) { |
| 80 |
$resolvers['string'] = $stringTypeResolver; |
| 81 |
$resolvers[\ReflectionParameter::class] = new PhpDocAwareReflectionTypeResolver($resolvers[\ReflectionParameter::class], $stringTypeResolver, $typeContextFactory); |
| 82 |
$resolvers[\ReflectionProperty::class] = new PhpDocAwareReflectionTypeResolver($resolvers[\ReflectionProperty::class], $stringTypeResolver, $typeContextFactory); |
| 83 |
$resolvers[\ReflectionFunctionAbstract::class] = new PhpDocAwareReflectionTypeResolver($resolvers[\ReflectionFunctionAbstract::class], $stringTypeResolver, $typeContextFactory); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
$resolversContainer = new class($resolvers) implements ContainerInterface { |
| 88 |
public function __construct( |
| 89 |
private readonly array $resolvers, |
| 90 |
) { |
| 91 |
} |
| 92 |
|
| 93 |
public function has(string $id): bool |
| 94 |
{ |
| 95 |
return isset($this->resolvers[$id]); |
| 96 |
} |
| 97 |
|
| 98 |
public function get(string $id): TypeResolverInterface |
| 99 |
{ |
| 100 |
return $this->resolvers[$id]; |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
return new self($resolversContainer); |
| 105 |
} |
| 106 |
} |