Skip to content

Commit

Permalink
Added static factories to NamedTypeReflection
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Mar 3, 2024
1 parent 70e134e commit fd2e6dd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 19 deletions.
80 changes: 79 additions & 1 deletion TypeReflection/NamedTypeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,92 @@ final class NamedTypeReflection extends \ReflectionNamedType
/**
* @param non-empty-string $name
*/
public function __construct(
private function __construct(
string $name,
private readonly bool $builtIn = true,
private readonly bool $nullable = false,
) {
$this->_name = $name;
}

public static function null(): self
{
return new self('null', nullable: true);
}

public static function true(): self
{
return new self('true');
}

public static function false(): self
{
return new self('false');
}

public static function bool(): self
{
return new self('bool');
}

public static function int(): self
{
return new self('int');
}

public static function float(): self
{
return new self('float');
}

public static function string(): self
{
return new self('string');
}

public static function array(): self
{
return new self('array');
}

public static function object(): self
{
return new self('object');
}

public static function iterable(): self
{
return new self('iterable');
}

public static function callable(): self
{
return new self('callable');
}

public static function mixed(): self
{
return new self('mixed', nullable: true);
}

public static function void(): self
{
return new self('void');
}

public static function never(): self
{
return new self('never');
}

/**
* @param non-empty-string $name
*/
public static function namedObject(string $name): self
{
return new self($name, builtIn: false);
}

public function toNullable(): self
{
return new self($this->_name, $this->builtIn, true);
Expand Down
36 changes: 18 additions & 18 deletions TypeReflection/TypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,76 +19,76 @@ final class TypeConverter extends DefaultTypeVisitor
{
public function string(Type $self): mixed
{
return new NamedTypeReflection('string');
return NamedTypeReflection::string();
}

public function int(Type $self): mixed
{
return new NamedTypeReflection('int');
return NamedTypeReflection::int();
}

public function float(Type $self): mixed
{
return new NamedTypeReflection('float');
return NamedTypeReflection::float();
}

public function array(Type $self, Type $key, Type $value, array $elements): mixed
{
return new NamedTypeReflection('array');
return NamedTypeReflection::array();
}

public function never(Type $self): mixed
{
return new NamedTypeReflection('never');
return NamedTypeReflection::never();
}

public function void(Type $self): mixed
{
return new NamedTypeReflection('void');
return NamedTypeReflection::void();
}

public function null(Type $self): mixed
{
return new NamedTypeReflection('null', nullable: true);
return NamedTypeReflection::null();
}

public function bool(Type $self): mixed
{
return new NamedTypeReflection('bool');
return NamedTypeReflection::bool();
}

public function namedObject(Type $self, string $class, array $arguments): mixed
{
return new NamedTypeReflection($class, builtIn: false);
return NamedTypeReflection::namedObject($class);
}

public function literalValue(Type $self, float|bool|int|string $value): mixed
{
return match ($value) {
true => new NamedTypeReflection('true'),
false => new NamedTypeReflection('false'),
true => NamedTypeReflection::true(),
false => NamedTypeReflection::false(),
default => throw new NonConvertableType($self),
};
}

public function callable(Type $self, array $parameters, Type $return): mixed
{
return new NamedTypeReflection('callable');
return NamedTypeReflection::callable();
}

public function object(Type $self): mixed
{
return new NamedTypeReflection('object');
return NamedTypeReflection::object();
}

public function iterable(Type $self, Type $key, Type $value): mixed
{
return new NamedTypeReflection('iterable');
return NamedTypeReflection::iterable();
}

public function closure(Type $self, array $parameters, Type $return): mixed
{
return new NamedTypeReflection(\Closure::class, builtIn: false);
return NamedTypeReflection::namedObject(\Closure::class);
}

public function union(Type $self, array $types): mixed
Expand Down Expand Up @@ -117,7 +117,7 @@ public function union(Type $self, array $types): mixed
return $convertedTypes[0]->toNullable();
}

$convertedTypes[] = new NamedTypeReflection('null', nullable: true);
$convertedTypes[] = NamedTypeReflection::null();
}

\assert(\count($convertedTypes) > 1);
Expand All @@ -128,7 +128,7 @@ public function union(Type $self, array $types): mixed
public function template(Type $self, string $name, AtClass|AtFunction|AtMethod $declaredAt, array $arguments): mixed
{
if ($name === 'self' || $name === 'parent' || $name === 'static') {
return new NamedTypeReflection($name, builtIn: false);
return NamedTypeReflection::namedObject($name);
}

throw new NonConvertableType($self);
Expand All @@ -152,7 +152,7 @@ function (Type $type) use ($self): \ReflectionNamedType {

public function mixed(Type $self): mixed
{
return new NamedTypeReflection('mixed', nullable: true);
return NamedTypeReflection::mixed();
}

protected function default(Type $self): mixed
Expand Down

0 comments on commit fd2e6dd

Please sign in to comment.