diff --git a/src/Type/Parser/Factory/Specifications/AliasSpecification.php b/src/Type/Parser/Factory/Specifications/AliasSpecification.php index be71ec10..236a1586 100644 --- a/src/Type/Parser/Factory/Specifications/AliasSpecification.php +++ b/src/Type/Parser/Factory/Specifications/AliasSpecification.php @@ -12,6 +12,10 @@ use ReflectionFunction; use Reflector; +use function array_shift; +use function explode; +use function strtolower; + /** @internal */ final class AliasSpecification implements TypeParserSpecification { @@ -49,32 +53,27 @@ public function manipulateToken(TraversingToken $token): TraversingToken private function resolveAlias(string $symbol): string { - $alias = $symbol; + $aliases = PhpParser::parseUseStatements($this->reflection); $namespaceParts = explode('\\', $symbol); - $lastPart = array_shift($namespaceParts); - if ($lastPart) { - $alias = strtolower($lastPart); + $lastPart = strtolower(end($namespaceParts)); + + if (isset($aliases[$lastPart])) { + return $aliases[$lastPart]; } - $aliases = PhpParser::parseUseStatements($this->reflection); + $alias = strtolower(array_shift($namespaceParts)); if (! isset($aliases[$alias])) { return $symbol; } - if ($aliases[$alias] === $symbol) { - return $symbol; - } - - $full = $aliases[$alias]; - - if (! empty($namespaceParts)) { - $full .= '\\' . implode('\\', $namespaceParts); + if ($namespaceParts === []) { + return $aliases[$alias]; } - return $full; + return $aliases[$alias] . '\\' . implode('\\', $namespaceParts); } private function resolveNamespaced(string $symbol): string diff --git a/tests/Integration/Mapping/Namespace/SameClassNameAsNamespaceGroupMappingTest.php b/tests/Integration/Mapping/Namespace/SameClassNameAsNamespaceGroupMappingTest.php new file mode 100644 index 00000000..c8ebe69f --- /dev/null +++ b/tests/Integration/Mapping/Namespace/SameClassNameAsNamespaceGroupMappingTest.php @@ -0,0 +1,37 @@ +mapperBuilder()->mapper()->map(ClassContainingNamespacedClasses::class, [ + 'objectA' => ['stringValue' => 'foo'], + 'objectB' => ['integerValue' => 42], + ]); + } catch (MappingError $error) { + $this->mappingFail($error); + } + + self::assertSame('foo', $result->objectA->stringValue); + self::assertSame(42, $result->objectB->integerValue); + } +} + +final class ClassContainingNamespacedClasses +{ + public SomeNamespace $objectA; + public SomeClass $objectB; +} diff --git a/tests/Integration/Mapping/Namespace/class-name-with-same-name-as-namespace-group.php b/tests/Integration/Mapping/Namespace/class-name-with-same-name-as-namespace-group.php new file mode 100644 index 00000000..948931a3 --- /dev/null +++ b/tests/Integration/Mapping/Namespace/class-name-with-same-name-as-namespace-group.php @@ -0,0 +1,13 @@ +