Skip to content

Commit

Permalink
Move ClassReflection::withResolvedTemplates logic to ClassTemplateRes…
Browse files Browse the repository at this point in the history
…olver::create
  • Loading branch information
vudaltsov committed Sep 21, 2023
1 parent 4e459e7 commit f0473d5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
10 changes: 1 addition & 9 deletions src/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,16 +551,8 @@ public function withResolvedTemplates(array $templateArguments = []): self
return $this;
}

$resolvedTemplateArguments = [];

foreach ($this->templates as $template) {
$resolvedTemplateArguments[$template->name] = $templateArguments[$template->name]
?? $templateArguments[$template->getPosition()]
?? $template->getConstraint();
}

/** @var self<T> */
return $this->withResolvedTypes(new ClassTemplateResolver($this->name, $resolvedTemplateArguments));
return $this->withResolvedTypes(ClassTemplateResolver::create($this->name, $this->templates, $templateArguments));
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/TemplateReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @api
* @psalm-immutable
*/
final class TemplateReflection
{
Expand Down
25 changes: 22 additions & 3 deletions src/TypeResolver/ClassTemplateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Typhoon\Reflection\TypeResolver;

use Typhoon\Reflection\TemplateReflection;
use Typhoon\Type;
use Typhoon\Type\types;
use Typhoon\Type\TypeVisitor;

/**
* @internal
* @psalm-internal Typhoon\Reflection
* @api
* @psalm-immutable
* @implements TypeVisitor<Type\Type>
*/
Expand All @@ -20,11 +20,30 @@ final class ClassTemplateResolver implements TypeVisitor
* @param class-string $class
* @param non-empty-array<non-empty-string, Type\Type> $templateArguments
*/
public function __construct(
private function __construct(
private readonly string $class,
private readonly array $templateArguments,
) {}

/**
* @psalm-pure
* @param class-string $class
* @param non-empty-array<TemplateReflection> $templates
* @param array<Type\Type> $templateArguments
*/
public static function create(string $class, array $templates, array $templateArguments): self
{
$resolvedTemplateArguments = [];

foreach ($templates as $template) {
$resolvedTemplateArguments[$template->name] = $templateArguments[$template->name]
?? $templateArguments[$template->getPosition()]
?? $template->getConstraint();
}

return new self($class, $resolvedTemplateArguments);
}

public function visitNever(Type\NeverType $type): mixed
{
return $type;
Expand Down

0 comments on commit f0473d5

Please sign in to comment.