From 044072e2cbdd6b12a2b820f3805963a9f8f22860 Mon Sep 17 00:00:00 2001 From: Romain Canon Date: Tue, 8 Aug 2023 17:48:15 +0200 Subject: [PATCH] fix: handle class name collision while parsing types inside a class --- src/Type/Parser/Lexer/AliasLexer.php | 4 ++- .../ClassNameCollisionTestCollision.php | 33 +++++++++++++++++++ tests/Integration/Mapping/Fixture/Error.php | 13 ++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/Mapping/ClassNameCollisionTestCollision.php create mode 100644 tests/Integration/Mapping/Fixture/Error.php diff --git a/src/Type/Parser/Lexer/AliasLexer.php b/src/Type/Parser/Lexer/AliasLexer.php index 96c2ac46..d5320a30 100644 --- a/src/Type/Parser/Lexer/AliasLexer.php +++ b/src/Type/Parser/Lexer/AliasLexer.php @@ -31,7 +31,9 @@ public function tokenize(string $symbol): Token private function resolve(string $symbol): string { - if (Reflection::classOrInterfaceExists($symbol)) { + // Matches the case where a class extends a class with the same name but + // in a different namespace. + if ($symbol === $this->reflection->getShortName() && Reflection::classOrInterfaceExists($symbol)) { return $symbol; } diff --git a/tests/Integration/Mapping/ClassNameCollisionTestCollision.php b/tests/Integration/Mapping/ClassNameCollisionTestCollision.php new file mode 100644 index 00000000..5b7c1754 --- /dev/null +++ b/tests/Integration/Mapping/ClassNameCollisionTestCollision.php @@ -0,0 +1,33 @@ +mapper() + ->map(ObjectWithErrorsClassNameCollision::class, ['foo', 'bar']); + } catch (MappingError $error) { + $this->mappingFail($error); + } + + self::assertSame('foo', $result->errors[0]->message); + self::assertSame('bar', $result->errors[1]->message); + } +} + +final class ObjectWithErrorsClassNameCollision +{ + public function __construct( + /** @var list */ + public array $errors + ) {} +} diff --git a/tests/Integration/Mapping/Fixture/Error.php b/tests/Integration/Mapping/Fixture/Error.php new file mode 100644 index 00000000..68f33eb9 --- /dev/null +++ b/tests/Integration/Mapping/Fixture/Error.php @@ -0,0 +1,13 @@ +