From fd2e6ddc5b8a14a9d5db4c3fd495a001dce073b5 Mon Sep 17 00:00:00 2001 From: Valentin Udaltsov Date: Sun, 3 Mar 2024 06:10:33 +0300 Subject: [PATCH] Added static factories to NamedTypeReflection --- TypeReflection/NamedTypeReflection.php | 80 +++++++++++++++++++++++++- TypeReflection/TypeConverter.php | 36 ++++++------ 2 files changed, 97 insertions(+), 19 deletions(-) diff --git a/TypeReflection/NamedTypeReflection.php b/TypeReflection/NamedTypeReflection.php index b6c5a51..e07f5f0 100644 --- a/TypeReflection/NamedTypeReflection.php +++ b/TypeReflection/NamedTypeReflection.php @@ -18,7 +18,7 @@ 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, @@ -26,6 +26,84 @@ public function __construct( $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); diff --git a/TypeReflection/TypeConverter.php b/TypeReflection/TypeConverter.php index ff46c5d..a84e8b9 100644 --- a/TypeReflection/TypeConverter.php +++ b/TypeReflection/TypeConverter.php @@ -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 @@ -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); @@ -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); @@ -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