From cfb125a2e188746c765ecb35e2c54b1cf29b2391 Mon Sep 17 00:00:00 2001 From: Romain Canon Date: Wed, 24 Apr 2024 14:08:24 +0200 Subject: [PATCH] fix: handle interface generics --- src/Type/Parser/Lexer/Token/ClassNameToken.php | 8 ++++---- tests/Functional/Type/Parser/LexingParserTest.php | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Type/Parser/Lexer/Token/ClassNameToken.php b/src/Type/Parser/Lexer/Token/ClassNameToken.php index 54a47ac7..5e277c43 100644 --- a/src/Type/Parser/Lexer/Token/ClassNameToken.php +++ b/src/Type/Parser/Lexer/Token/ClassNameToken.php @@ -49,15 +49,15 @@ public function traverse(TokenStream $stream): Type return $constant; } - if ($this->reflection->isInterface()) { - return new InterfaceType($this->reflection->name); - } - $templates = (new ClassTemplatesResolver())->resolveTemplateNamesFrom($this->reflection->name); $generics = $this->generics($stream, $this->reflection->name, $templates); $generics = $this->assignGenerics($this->reflection->name, $templates, $generics); + if ($this->reflection->isInterface()) { + return new InterfaceType($this->reflection->name, $generics); + } + return new NativeClassType($this->reflection->name, $generics); } diff --git a/tests/Functional/Type/Parser/LexingParserTest.php b/tests/Functional/Type/Parser/LexingParserTest.php index c648a535..db84ab9e 100644 --- a/tests/Functional/Type/Parser/LexingParserTest.php +++ b/tests/Functional/Type/Parser/LexingParserTest.php @@ -837,6 +837,12 @@ public static function parse_valid_types_returns_valid_result_data_provider(): i 'type' => InterfaceType::class, ]; + yield 'Interface name with one template' => [ + 'raw' => SomeInterfaceWithOneTemplate::class . '', + 'transformed' => SomeInterfaceWithOneTemplate::class . '', + 'type' => InterfaceType::class, + ]; + yield 'Class name with generic with one template' => [ 'raw' => SomeClassWithOneTemplate::class . '', 'transformed' => SomeClassWithOneTemplate::class . '', @@ -1621,3 +1627,8 @@ final class SomeClassWithTemplateOfArrayKey {} * @template TemplateB of object */ final class SomeClassWithFirstTemplateWithoutTypeAndSecondTemplateWithType {} + +/** + * @template TemplateA + */ +interface SomeInterfaceWithOneTemplate {}