Skip to content

Commit

Permalink
Refactor Reflection to TypeResolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Aug 11, 2024
1 parent 7cea755 commit f6946d1
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 115 deletions.
15 changes: 9 additions & 6 deletions src/Reflection/Internal/Data/TypeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Typhoon\Reflection\Internal\Data;

use Typhoon\Reflection\Internal\Inheritance\TypeResolver;
use Typhoon\Reflection\TypeKind;
use Typhoon\Type\Type;
use Typhoon\Type\types;
use Typhoon\Type\TypeVisitor;

/**
* @internal
Expand Down Expand Up @@ -50,13 +50,16 @@ public function withTentative(?Type $tentative): self
return $data;
}

public function inherit(TypeResolver $typeResolver): self
/**
* @param TypeVisitor<Type> $typeResolver
*/
public function inherit(TypeVisitor $typeResolver): self
{
return new self(
native: $typeResolver->resolveNativeType($this->native),
annotated: $typeResolver->resolveType($this->annotated),
tentative: $typeResolver->resolveNativeType($this->tentative),
inferred: $typeResolver->resolveNativeType($this->inferred),
native: $this->native?->accept($typeResolver),
annotated: $this->annotated?->accept($typeResolver),
tentative: $this->tentative?->accept($typeResolver),
inferred: $this->inferred?->accept($typeResolver),
);
}

Expand Down
41 changes: 39 additions & 2 deletions src/Reflection/Internal/Inheritance/ClassInheritance.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use Typhoon\Reflection\Internal\Data\ClassKind;
use Typhoon\Reflection\TyphoonReflector;
use Typhoon\Type\Type;
use Typhoon\Type\TypeVisitor;
use Typhoon\Type\Visitor\RelativeClassTypeResolver;
use Typhoon\Type\Visitor\TemplateTypeResolver;
use Typhoon\Type\Visitor\TypeResolvers;
use Typhoon\TypedMap\TypedMap;

/**
Expand Down Expand Up @@ -107,7 +111,7 @@ private function applyOneUsed(string $traitName, array $typeArguments): void
$traitData = $this->recompileTraitExpressions($this->reflector->reflect($traitId)->data);

$this->changeDetectors[] = $traitData[Data::ChangeDetector];
$typeResolver = TypeResolver::from($this->id, $this->data, $traitId, $traitData, $typeArguments);
$typeResolver = $this->createTypeResolvers($traitId, $traitData, $typeArguments);

foreach ($traitData[Data::Constants] as $constantName => $constant) {
$this->constant($constantName)->applyUsed($constant, $typeResolver);
Expand Down Expand Up @@ -178,7 +182,7 @@ private function addInherited(string $className, array $typeArguments): void
$this->parents = [$className => $typeArguments, ...$classData[Data::Parents]];
}

$typeResolver = TypeResolver::from($this->id, $this->data, $classId, $classData, $typeArguments);
$typeResolver = $this->createTypeResolvers($classId, $classData, $typeArguments);

foreach ($classData[Data::Constants] as $constantName => $constant) {
$this->constant($constantName)->applyInherited($constant, $typeResolver);
Expand Down Expand Up @@ -278,4 +282,37 @@ private function recompileTraitExpressions(TypedMap $data): TypedMap
$data[Data::Methods],
));
}

/**
* @param list<Type> $typeArguments
* @return TypeVisitor<Type>
*/
private function createTypeResolvers(NamedClassId $inheritedId, TypedMap $inheritedData, array $typeArguments): TypeVisitor
{
$typeResolvers = [];

if ($this->data[Data::ClassKind] !== ClassKind::Trait) {
$parent = $this->data[Data::UnresolvedParent];
$typeResolvers[] = new RelativeClassTypeResolver(
self: $this->id,
parent: $parent === null ? null : Id::namedClass($parent[0]),
);
}

$templates = $inheritedData[Data::Templates];

if ($templates !== []) {
$typeResolvers[] = new TemplateTypeResolver(array_map(
static fn(int $index, string $name, TypedMap $template): array => [
Id::template($inheritedId, $name),
$typeArguments[$index] ?? $template[Data::Constraint],
],
range(0, \count($templates) - 1),
array_keys($templates),
$templates,
));
}

return TypeResolvers::from($typeResolvers);
}
}
12 changes: 10 additions & 2 deletions src/Reflection/Internal/Inheritance/MethodInheritance.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Typhoon\Reflection\Internal\Data;
use Typhoon\Reflection\Internal\Data\TypeData;
use Typhoon\Reflection\Internal\Data\Visibility;
use Typhoon\Type\Type;
use Typhoon\Type\TypeVisitor;
use Typhoon\TypedMap\TypedMap;

/**
Expand Down Expand Up @@ -44,7 +46,10 @@ public function applyOwn(TypedMap $data): void
$this->throwsType->applyOwn(new TypeData(annotated: $data[Data::ThrowsType]));
}

public function applyUsed(TypedMap $data, TypeResolver $typeResolver): void
/**
* @param TypeVisitor<Type> $typeResolver
*/
public function applyUsed(TypedMap $data, TypeVisitor $typeResolver): void
{
if ($this->data !== null) {
$usedParameters = array_values($data[Data::Parameters]);
Expand All @@ -70,7 +75,10 @@ public function applyUsed(TypedMap $data, TypeResolver $typeResolver): void
$this->throwsType->applyInherited(new TypeData(annotated: $data[Data::ThrowsType]), $typeResolver);
}

public function applyInherited(TypedMap $data, TypeResolver $typeResolver): void
/**
* @param TypeVisitor<Type> $typeResolver
*/
public function applyInherited(TypedMap $data, TypeVisitor $typeResolver): void
{
if ($data[Data::Visibility] === Visibility::Private) {
return;
Expand Down
12 changes: 10 additions & 2 deletions src/Reflection/Internal/Inheritance/PropertyInheritance.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Typhoon\Reflection\Internal\Data;
use Typhoon\Reflection\Internal\Data\Visibility;
use Typhoon\Type\Type;
use Typhoon\Type\TypeVisitor;
use Typhoon\TypedMap\TypedMap;

/**
Expand All @@ -31,13 +33,19 @@ public function applyOwn(TypedMap $data): void
$this->type->applyOwn($data[Data::Type]);
}

public function applyUsed(TypedMap $data, TypeResolver $typeResolver): void
/**
* @param TypeVisitor<Type> $typeResolver
*/
public function applyUsed(TypedMap $data, TypeVisitor $typeResolver): void
{
$this->data ??= $data;
$this->type->applyInherited($data[Data::Type], $typeResolver);
}

public function applyInherited(TypedMap $data, TypeResolver $typeResolver): void
/**
* @param TypeVisitor<Type> $typeResolver
*/
public function applyInherited(TypedMap $data, TypeVisitor $typeResolver): void
{
if ($data[Data::Visibility] === Visibility::Private) {
return;
Expand Down
8 changes: 6 additions & 2 deletions src/Reflection/Internal/Inheritance/TypeInheritance.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Typhoon\Reflection\Internal\Data\TypeData;
use Typhoon\Type\Type;
use Typhoon\Type\TypeVisitor;

/**
* @internal
Expand All @@ -16,7 +17,7 @@ final class TypeInheritance
private ?TypeData $own = null;

/**
* @var list<array{TypeData, TypeResolver}>
* @var list<array{TypeData, TypeVisitor<Type>}>
*/
private array $inherited = [];

Expand All @@ -33,7 +34,10 @@ public function applyOwn(TypeData $data): void
$this->own = $data;
}

public function applyInherited(TypeData $data, TypeResolver $typeResolver): void
/**
* @param TypeVisitor<Type> $typeResolver
*/
public function applyInherited(TypeData $data, TypeVisitor $typeResolver): void
{
$this->inherited[] = [$data, $typeResolver];
}
Expand Down
101 changes: 0 additions & 101 deletions src/Reflection/Internal/Inheritance/TypeResolver.php

This file was deleted.

0 comments on commit f6946d1

Please sign in to comment.