Skip to content

Commit

Permalink
Switch to typhoon/type v0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Jan 19, 2024
1 parent 3e633e1 commit d0e9305
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 119 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"nikic/php-parser": "^4.15",
"phpstan/phpdoc-parser": "^1.0",
"psr/simple-cache": "^3.0",
"typhoon/type": "^0.2@dev",
"typhoon/type-stringifier": "^0.2@dev"
"typhoon/type": "^0.3@dev",
"typhoon/type-stringifier": "^0.3@dev"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.41.1",
Expand Down
38 changes: 19 additions & 19 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Typhoon\Reflection\Reflector\ContextAwareReflection;
use Typhoon\Reflection\Reflector\RootReflection;
use Typhoon\Reflection\TypeResolver\ClassTemplateResolver;
use Typhoon\Reflection\TypeResolver\StaticResolver;
use Typhoon\Reflection\TypeResolver\TemplateResolver;
use Typhoon\Type;

/**
Expand Down Expand Up @@ -571,7 +571,7 @@ public function resolveTemplates(array $templateArguments = []): self
}

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

/**
Expand Down Expand Up @@ -668,7 +668,7 @@ protected function setContext(ReflectionContext $reflectionContext): void
}
}

private function resolvedTypes(ClassTemplateResolver|StaticResolver $typeResolver): self
private function resolvedTypes(TemplateResolver|StaticResolver $typeResolver): self
{
$class = clone $this;
$class->propertiesIndexedByName = array_map(
Expand Down
4 changes: 2 additions & 2 deletions src/MethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Typhoon\Reflection;

use Typhoon\Reflection\Reflector\ContextAwareReflection;
use Typhoon\Reflection\TypeResolver\ClassTemplateResolver;
use Typhoon\Reflection\TypeResolver\StaticResolver;
use Typhoon\Reflection\TypeResolver\TemplateResolver;

/**
* @api
Expand Down Expand Up @@ -411,7 +411,7 @@ public function getNativeReflection(): \ReflectionMethod
return $this->nativeReflection ??= new \ReflectionMethod($this->class, $this->name);
}

public function resolveTypes(ClassTemplateResolver|StaticResolver $typeResolver): self
public function resolveTypes(TemplateResolver|StaticResolver $typeResolver): self
{
$method = clone $this;
$method->parameters = array_map(
Expand Down
7 changes: 1 addition & 6 deletions src/NameResolution/NameAsClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ public function constant(string $name): mixed
throw new ReflectionException(sprintf('Name "%s" cannot be resolved as class.', $name));
}

public function classTemplate(string $class, string $name): mixed
{
throw new ReflectionException(sprintf('Name "%s" cannot be resolved as class.', $class));
}

public function methodTemplate(string $class, string $method, string $name): mixed
public function template(string $name): mixed
{
throw new ReflectionException(sprintf('Name "%s" cannot be resolved as class.', $name));
}
Expand Down
11 changes: 3 additions & 8 deletions src/NameResolution/NameAsTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,17 @@ public function class(string $name): mixed

public function static(string $self): mixed
{
return types::static($self, ...$this->templateArguments);
return types::static(...$this->templateArguments);
}

public function constant(string $name): mixed
{
return types::constant($name);
}

public function classTemplate(string $class, string $name): mixed
public function template(string $name): mixed
{
return types::classTemplate($class, $name);
}

public function methodTemplate(string $class, string $method, string $name): mixed
{
return types::methodTemplate($class, $method, $name);
return types::template($name);
}

public function classOrConstants(string $classCandidate, array $constantCandidates): mixed
Expand Down
12 changes: 4 additions & 8 deletions src/NameResolution/NameContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ final class NameContext
*/
private array $classTemplateNamesMap = [];

private ?UnqualifiedName $method = null;

/**
* @var array<non-empty-string, true>
*/
Expand Down Expand Up @@ -107,11 +105,10 @@ public function enterClass(string $name, ?string $parent = null, array $template
/**
* @param array<string> $templateNames
*/
public function enterMethod(string $name, array $templateNames = []): void
public function enterMethod(array $templateNames = []): void
{
$this->leaveMethod();

$this->method = new UnqualifiedName($name);
$this->methodTemplateNamesMap = array_fill_keys(
array_map(
/** @return non-empty-string */
Expand All @@ -124,7 +121,6 @@ public function enterMethod(string $name, array $templateNames = []): void

public function leaveMethod(): void
{
$this->method = null;
$this->methodTemplateNamesMap = [];
}

Expand Down Expand Up @@ -223,12 +219,12 @@ public function resolveName(string|Name $name, NameResolver $resolver): mixed
return $resolver->static($this->resolveNameAsClass($this->self));
}

if ($this->method !== null && isset($this->methodTemplateNamesMap[$nameAsString])) {
return $resolver->methodTemplate($this->resolveNameAsClass($this->self), $this->method->toString(), $nameAsString);
if (isset($this->methodTemplateNamesMap[$nameAsString])) {
return $resolver->template($nameAsString);
}

if (isset($this->classTemplateNamesMap[$nameAsString])) {
return $resolver->classTemplate($this->resolveNameAsClass($this->self), $nameAsString);
return $resolver->template($nameAsString);
}
}

Expand Down
11 changes: 1 addition & 10 deletions src/NameResolution/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,10 @@ public function static(string $self): mixed;
public function constant(string $name): mixed;

/**
* @param class-string $class
* @param non-empty-string $name
* @return T
*/
public function classTemplate(string $class, string $name): mixed;

/**
* @param class-string $class
* @param non-empty-string $method
* @param non-empty-string $name
* @return T
*/
public function methodTemplate(string $class, string $method, string $name): mixed;
public function template(string $name): mixed;

/**
* @param non-empty-string $classCandidate
Expand Down
4 changes: 2 additions & 2 deletions src/ParameterReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Typhoon\Reflection;

use Typhoon\Reflection\Reflector\ContextAwareReflection;
use Typhoon\Reflection\TypeResolver\ClassTemplateResolver;
use Typhoon\Reflection\TypeResolver\StaticResolver;
use Typhoon\Reflection\TypeResolver\TemplateResolver;

/**
* @api
Expand Down Expand Up @@ -176,7 +176,7 @@ public function getNativeReflection(): \ReflectionParameter
);
}

public function resolveTypes(ClassTemplateResolver|StaticResolver $typeResolver): self
public function resolveTypes(TemplateResolver|StaticResolver $typeResolver): self
{
$parameter = clone $this;
$parameter->type = $this->type->resolve($typeResolver);
Expand Down
4 changes: 2 additions & 2 deletions src/PropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Typhoon\Reflection;

use Typhoon\Reflection\Reflector\ContextAwareReflection;
use Typhoon\Reflection\TypeResolver\ClassTemplateResolver;
use Typhoon\Reflection\TypeResolver\StaticResolver;
use Typhoon\Reflection\TypeResolver\TemplateResolver;

/**
* @api
Expand Down Expand Up @@ -203,7 +203,7 @@ public function getNativeReflection(): \ReflectionProperty
return $this->nativeReflection ??= new \ReflectionProperty($this->class, $this->name);
}

public function resolveTypes(ClassTemplateResolver|StaticResolver $typeResolver): self
public function resolveTypes(TemplateResolver|StaticResolver $typeResolver): self
{
$property = clone $this;
$property->type = $this->type->resolve($typeResolver);
Expand Down
5 changes: 1 addition & 4 deletions src/Reflector/NameContextVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ public function enterNode(Node $node): ?int
}

if ($node instanceof Stmt\ClassMethod) {
$this->nameContext->enterMethod(
name: $node->name->name,
templateNames: PhpDocParsingVisitor::fromNode($node)->templateNames(),
);
$this->nameContext->enterMethod(PhpDocParsingVisitor::fromNode($node)->templateNames());

return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Reflector/NativeReflectionReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ private function reflectNativeType(?\ReflectionType $reflectionType, ?string $cl
throw new \LogicException('Cannot use type "static" outside of class scope.');
}

return types::static($class);
return types::static();
}

$type = match ($name) {
Expand Down
6 changes: 3 additions & 3 deletions src/Reflector/PhpDocTypeReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,12 @@ private function reflectIterable(array $templateArguments): Type\Type
};
}

private function reflectArrayShape(ArrayShapeNode $node): Type\ShapeType
private function reflectArrayShape(ArrayShapeNode $node): Type\ArrayShapeType
{
$elements = [];

foreach ($node->items as $item) {
$type = types::element($this->doReflect($item->valueType), $item->optional);
$type = types::arrayElement($this->doReflect($item->valueType), $item->optional);

if ($item->keyName === null) {
$elements[] = $type;
Expand All @@ -299,7 +299,7 @@ private function reflectArrayShape(ArrayShapeNode $node): Type\ShapeType
$elements[$key] = $type;
}

return types::shape($elements, $node->sealed);
return types::arrayShape($elements, $node->sealed);
}

private function reflectConstExpr(ConstTypeNode $node): Type\Type
Expand Down
8 changes: 3 additions & 5 deletions src/Reflector/PhpParserReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ private function reflectOwnMethods(string $class, Stmt\ClassLike $classNode): ar
$phpDoc = PhpDocParsingVisitor::fromNode($node);

try {
$this->nameContext->enterMethod($name, $phpDoc->templateNames());
$this->nameContext->enterMethod($phpDoc->templateNames());

$methods[] = new MethodReflection(
class: $class,
Expand Down Expand Up @@ -534,13 +534,11 @@ private function reflectNativeType(?Node $node): ?Type\Type
}

if ($node instanceof Name) {
$resolvedName = $this->nameContext->resolveNameAsClass($node->toCodeString());

if ($node->toString() === 'static') {
return types::static($resolvedName);
return types::static();
}

return types::object($resolvedName);
return types::object($this->nameContext->resolveNameAsClass($node->toCodeString()));
}

throw new ReflectionException(sprintf('%s is not supported.', $node::class));
Expand Down
4 changes: 2 additions & 2 deletions src/TypeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Typhoon\Reflection;

use Typhoon\Reflection\TypeResolver\ClassTemplateResolver;
use Typhoon\Reflection\TypeResolver\StaticResolver;
use Typhoon\Reflection\TypeResolver\TemplateResolver;
use Typhoon\Type\Type;
use Typhoon\Type\types;

Expand Down Expand Up @@ -69,7 +69,7 @@ public function getResolved(): Type
return $this->resolved;
}

public function resolve(ClassTemplateResolver|StaticResolver $typeResolver): self
public function resolve(TemplateResolver|StaticResolver $typeResolver): self
{
return new self(
native: $this->native,
Expand Down
Loading

0 comments on commit d0e9305

Please sign in to comment.