From 878498570c57488e7f3b7e8db44fcb45f4c318fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Sat, 18 Aug 2018 15:04:40 +0200 Subject: [PATCH] Fix return type of entity manager dynamic return The methods `EntityManagerInterface#getReference()` and `EntityManagerInterface#getPartialReference()` have nullable return. That's properly documented for `getReference()` but had to be fixed for `getPartialReference()`. Ref: https://github.com/doctrine/doctrine2/pull/7360 --- .../EntityManagerFindDynamicReturnTypeExtension.php | 7 +------ .../ORM/data/entityManagerDynamicReturn.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Type/Doctrine/EntityManagerFindDynamicReturnTypeExtension.php b/src/Type/Doctrine/EntityManagerFindDynamicReturnTypeExtension.php index 2498cc8d..fa0884d6 100644 --- a/src/Type/Doctrine/EntityManagerFindDynamicReturnTypeExtension.php +++ b/src/Type/Doctrine/EntityManagerFindDynamicReturnTypeExtension.php @@ -43,12 +43,7 @@ public function getTypeFromMethodCall( return $mixedType; } - $type = new ObjectType($argType->getValue()); - if ($methodReflection->getName() === 'find') { - $type = TypeCombinator::addNull($type); - } - - return $type; + return TypeCombinator::addNull(new ObjectType($argType->getValue())); } } diff --git a/tests/DoctrineIntegration/ORM/data/entityManagerDynamicReturn.php b/tests/DoctrineIntegration/ORM/data/entityManagerDynamicReturn.php index 465f651f..745d5005 100644 --- a/tests/DoctrineIntegration/ORM/data/entityManagerDynamicReturn.php +++ b/tests/DoctrineIntegration/ORM/data/entityManagerDynamicReturn.php @@ -31,12 +31,22 @@ public function findDynamicType(): void public function getReferenceDynamicType(): void { $test = $this->entityManager->getReference(MyEntity::class, 1); + + if ($test === null) { + throw new RuntimeException('Sorry, but no...'); + } + $test->doSomething(); } public function getPartialReferenceDynamicType(): void { $test = $this->entityManager->getPartialReference(MyEntity::class, 1); + + if ($test === null) { + throw new RuntimeException('Sorry, but no...'); + } + $test->doSomething(); } }