From 22d13bdc3b032812341c122b62620f8e7f0c0f3d Mon Sep 17 00:00:00 2001 From: Valentin Udaltsov Date: Thu, 22 Feb 2024 04:43:28 +0300 Subject: [PATCH] Added AliasType --- src/AliasType.php | 42 ++++++++++++++++++++++++++++++++++++++ src/TypeVisitor.php | 3 +++ src/types.php | 9 ++++++++ tests/PsalmTest.php | 1 + tests/psalm/AliasType.phpt | 9 ++++++++ 5 files changed, 64 insertions(+) create mode 100644 src/AliasType.php create mode 100644 tests/psalm/AliasType.phpt diff --git a/src/AliasType.php b/src/AliasType.php new file mode 100644 index 0000000..6ccd241 --- /dev/null +++ b/src/AliasType.php @@ -0,0 +1,42 @@ + + */ +final class AliasType implements Type +{ + /** + * @var non-empty-string + */ + public readonly string $class; + + /** + * @var non-empty-string + */ + public readonly string $name; + + /** + * @internal + * @psalm-internal Typhoon\Type + * @param non-empty-string $class + * @param non-empty-string $name + */ + public function __construct( + string $class, + string $name, + ) { + $this->class = $class; + $this->name = $name; + } + + public function accept(TypeVisitor $visitor): mixed + { + return $visitor->visitAlias($this); + } +} diff --git a/src/TypeVisitor.php b/src/TypeVisitor.php index 4a3f60e..eebcf61 100644 --- a/src/TypeVisitor.php +++ b/src/TypeVisitor.php @@ -118,6 +118,9 @@ public function visitTemplate(TemplateType $type): mixed; /** @return TReturn */ public function visitConditional(ConditionalType $type): mixed; + /** @return TReturn */ + public function visitAlias(AliasType $type): mixed; + /** @return TReturn */ public function visitIntersection(IntersectionType $type): mixed; diff --git a/src/types.php b/src/types.php index 9019816..73bda9d 100644 --- a/src/types.php +++ b/src/types.php @@ -358,6 +358,15 @@ public static function conditional(Argument|TemplateType $subject, Type $if, Typ return new ConditionalType($subject, $if, $then, $else); } + /** + * @param non-empty-string $class + * @param non-empty-string $name + */ + public static function alias(string $class, string $name): AliasType + { + return new AliasType($class, $name); + } + /** * @param non-empty-string $name */ diff --git a/tests/PsalmTest.php b/tests/PsalmTest.php index 5a52174..9cdbcc6 100644 --- a/tests/PsalmTest.php +++ b/tests/PsalmTest.php @@ -24,6 +24,7 @@ public static function extractType(Type $_type): mixed return null; } + #[TestWith([__DIR__ . '/psalm/AliasType.phpt'])] #[TestWith([__DIR__ . '/psalm/AnyLiteralIntType.phpt'])] #[TestWith([__DIR__ . '/psalm/AnyLiteralStringType.phpt'])] #[TestWith([__DIR__ . '/psalm/ArrayShapeType.phpt'])] diff --git a/tests/psalm/AliasType.phpt b/tests/psalm/AliasType.phpt new file mode 100644 index 0000000..dc58c51 --- /dev/null +++ b/tests/psalm/AliasType.phpt @@ -0,0 +1,9 @@ +--FILE-- +