From b73e83d835928a7710f4633411dc3bcfe8215bd1 Mon Sep 17 00:00:00 2001 From: Adrien Foulon <6115458+tofandel@users.noreply.github.com> Date: Thu, 7 Jul 2022 10:22:08 +0200 Subject: [PATCH] fix: Backport Normalize class names #300 (#301) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Grégoire Paris --- .../Mapping/AbstractClassMetadataFactory.php | 19 ++++++++++++++++++- .../AbstractClassMetadataFactoryTest.php | 17 +++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Persistence/Mapping/AbstractClassMetadataFactory.php b/src/Persistence/Mapping/AbstractClassMetadataFactory.php index 92fbaee4..ef60934b 100644 --- a/src/Persistence/Mapping/AbstractClassMetadataFactory.php +++ b/src/Persistence/Mapping/AbstractClassMetadataFactory.php @@ -21,6 +21,7 @@ use function class_exists; use function explode; use function is_array; +use function ltrim; use function str_replace; use function strpos; use function strrpos; @@ -214,6 +215,18 @@ abstract protected function initializeReflection(ClassMetadata $class, Reflectio */ abstract protected function isEntity(ClassMetadata $class); + /** + * Removes the prepended backslash of a class string to conform with how php outputs class names + * + * @param string $className + * + * @return string + */ + private function normalizeClassName($className) + { + return ltrim($className, '\\'); + } + /** * {@inheritDoc} * @@ -222,6 +235,8 @@ abstract protected function isEntity(ClassMetadata $class); */ public function getMetadataFor($className) { + $className = $this->normalizeClassName($className); + if (isset($this->loadedMetadata[$className])) { return $this->loadedMetadata[$className]; } @@ -307,6 +322,8 @@ public function getMetadataFor($className) */ public function hasMetadataFor($className) { + $className = $this->normalizeClassName($className); + return isset($this->loadedMetadata[$className]); } @@ -321,7 +338,7 @@ public function hasMetadataFor($className) */ public function setMetadataFor($className, $class) { - $this->loadedMetadata[$className] = $class; + $this->loadedMetadata[$this->normalizeClassName($className)] = $class; } /** diff --git a/tests/Persistence/Mapping/AbstractClassMetadataFactoryTest.php b/tests/Persistence/Mapping/AbstractClassMetadataFactoryTest.php index 409b2808..bb1de980 100644 --- a/tests/Persistence/Mapping/AbstractClassMetadataFactoryTest.php +++ b/tests/Persistence/Mapping/AbstractClassMetadataFactoryTest.php @@ -84,6 +84,19 @@ public function testAnonymousClassIsNotMistakenForShortAlias(): void self::assertFalse($cmf->isTransient(get_class(new class () { }))); } + + public function testItGetsTheSameMetadataForBackslashedClassName(): void + { + $cmf = $this->getMockForAbstractClass(AbstractClassMetadataFactory::class); + $cmf + ->method('newClassMetadataInstance') + ->with(SomeOtherEntity::class) + ->willReturn( + $this->createMock(ClassMetadata::class) + ); + + self::assertSame($cmf->getMetadataFor(SomeOtherEntity::class), $cmf->getMetadataFor('\\' . SomeOtherEntity::class)); + } } class SomeGrandParentEntity @@ -97,3 +110,7 @@ class SomeParentEntity extends SomeGrandParentEntity final class SomeEntity extends SomeParentEntity { } + +final class SomeOtherEntity +{ +}