From 81b102632ef29451fbe87db596bc482b6b47ab04 Mon Sep 17 00:00:00 2001 From: Junaid-PK Date: Thu, 3 Sep 2026 23:05:19 +0500 Subject: [PATCH 1/2] Handle root namespace classes in SymfonyFileLocator --- src/Mapping/Driver/SymfonyFileLocator.php | 8 +++----- tests/Mapping/SymfonyFileLocatorTest.php | 9 +++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Mapping/Driver/SymfonyFileLocator.php b/src/Mapping/Driver/SymfonyFileLocator.php index 09339fae..553c5150 100644 --- a/src/Mapping/Driver/SymfonyFileLocator.php +++ b/src/Mapping/Driver/SymfonyFileLocator.php @@ -11,10 +11,8 @@ use RuntimeException; use function array_keys; -use function assert; use function is_dir; use function is_file; -use function is_int; use function realpath; use function sprintf; use function str_replace; @@ -222,12 +220,12 @@ public function findMappingFile(string $className): string } } - $pos = strrpos($className, '\\'); - assert(is_int($pos)); + $pos = strrpos($className, '\\'); + $shortClassName = $pos === false ? $className : substr($className, $pos + 1); throw MappingException::mappingFileNotFound( $className, - substr($className, $pos + 1) . $this->fileExtension, + $shortClassName . $this->fileExtension, ); } diff --git a/tests/Mapping/SymfonyFileLocatorTest.php b/tests/Mapping/SymfonyFileLocatorTest.php index ff7767d8..0b58d5b4 100644 --- a/tests/Mapping/SymfonyFileLocatorTest.php +++ b/tests/Mapping/SymfonyFileLocatorTest.php @@ -188,6 +188,15 @@ public function testFindMappingFileNotFound(): void $locator->findMappingFile('Foo\\stdClass2'); } + public function testFindMappingFileNotFoundForClassInRootNamespace(): void + { + $locator = new SymfonyFileLocator([], '.yml'); + + $this->expectException(MappingException::class); + $this->expectExceptionMessage("No mapping file found named 'DateTime.yml' for class 'DateTime'."); + $locator->findMappingFile('DateTime'); + } + public function testFindMappingFileLeastSpecificNamespaceFirst(): void { // Low -> High From fb04176eb5f8928b0e30fbec6fde5caee3a27f04 Mon Sep 17 00:00:00 2001 From: Junaid-PK Date: Fri, 11 Sep 2026 13:36:21 +0500 Subject: [PATCH 2/2] Simplify short class name extraction --- src/Mapping/Driver/SymfonyFileLocator.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Mapping/Driver/SymfonyFileLocator.php b/src/Mapping/Driver/SymfonyFileLocator.php index 553c5150..5d9b1ceb 100644 --- a/src/Mapping/Driver/SymfonyFileLocator.php +++ b/src/Mapping/Driver/SymfonyFileLocator.php @@ -11,6 +11,7 @@ use RuntimeException; use function array_keys; +use function basename; use function is_dir; use function is_file; use function realpath; @@ -18,7 +19,6 @@ use function str_replace; use function str_starts_with; use function strlen; -use function strrpos; use function strtr; use function substr; @@ -220,12 +220,9 @@ public function findMappingFile(string $className): string } } - $pos = strrpos($className, '\\'); - $shortClassName = $pos === false ? $className : substr($className, $pos + 1); - throw MappingException::mappingFileNotFound( $className, - $shortClassName . $this->fileExtension, + basename(str_replace('\\', '/', $className)) . $this->fileExtension, ); }