Skip to content

Commit

Permalink
Add CustomTypeResolvers composite, accept an iterable of CustomTypeRe…
Browse files Browse the repository at this point in the history
…solver in TyphoonReflector::build()
  • Loading branch information
vudaltsov committed Aug 1, 2024
1 parent 23948b1 commit 9ca06bc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
33 changes: 33 additions & 0 deletions Annotated/CustomTypeResolvers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Typhoon\Reflection\Annotated;

use Typhoon\Type\Type;

/**
* @api
*/
final class CustomTypeResolvers implements CustomTypeResolver
{
/**
* @param iterable<CustomTypeResolver> $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;
}
}
7 changes: 4 additions & 3 deletions TyphoonReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,14 +70,15 @@ final class TyphoonReflector

/**
* @param ?iterable<ConstantLocator|NamedFunctionLocator|NamedClassLocator|AnonymousLocator> $locators
* @param iterable<CustomTypeResolver> $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(
Expand Down

0 comments on commit 9ca06bc

Please sign in to comment.