From 9ca06bc0e0023fc715f3dd7d39e95c9ebeb758ea Mon Sep 17 00:00:00 2001 From: Valentin Udaltsov Date: Thu, 1 Aug 2024 22:45:27 +0300 Subject: [PATCH] Add CustomTypeResolvers composite, accept an iterable of CustomTypeResolver in TyphoonReflector::build() --- Annotated/CustomTypeResolvers.php | 33 +++++++++++++++++++++++++++++++ TyphoonReflector.php | 7 ++++--- 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 Annotated/CustomTypeResolvers.php diff --git a/Annotated/CustomTypeResolvers.php b/Annotated/CustomTypeResolvers.php new file mode 100644 index 0000000..92ec2b1 --- /dev/null +++ b/Annotated/CustomTypeResolvers.php @@ -0,0 +1,33 @@ + $resolvers + */ + public function __construct( + private readonly iterable $resolvers, + ) {} + + public function resolveCustomType(string $unresolvedName, array $typeArguments, TypeContext $context): ?Type + { + foreach ($this->resolvers as $resolver) { + $type = $resolver->resolveCustomType($unresolvedName, $typeArguments, $context); + + if ($type !== null) { + return $type; + } + } + + return null; + } +} diff --git a/TyphoonReflector.php b/TyphoonReflector.php index ce5bb1d..ce4d291 100644 --- a/TyphoonReflector.php +++ b/TyphoonReflector.php @@ -20,7 +20,7 @@ use Typhoon\DeclarationId\TemplateId; use Typhoon\PhpStormReflectionStubs\PhpStormStubsLocator; use Typhoon\Reflection\Annotated\CustomTypeResolver; -use Typhoon\Reflection\Annotated\NullCustomTypeResolver; +use Typhoon\Reflection\Annotated\CustomTypeResolvers; use Typhoon\Reflection\Exception\DeclarationNotFound; use Typhoon\Reflection\Internal\Cache\Cache; use Typhoon\Reflection\Internal\Cache\InMemoryPsr16Cache; @@ -70,14 +70,15 @@ final class TyphoonReflector /** * @param ?iterable $locators + * @param iterable $customTypeResolvers */ public static function build( ?iterable $locators = null, ?CacheInterface $cache = null, - CustomTypeResolver $customTypeResolver = new NullCustomTypeResolver(), + iterable $customTypeResolvers = [], ?Parser $phpParser = null, ): self { - $phpDocReflector = new PhpDocReflector($customTypeResolver); + $phpDocReflector = new PhpDocReflector(new CustomTypeResolvers($customTypeResolvers)); return new self( codeReflector: new CodeReflector(