From 0c177409ed477e978433597639335fb2a78bc331 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 21 Mar 2024 11:01:32 +0100 Subject: [PATCH] Add support for getOneOrNullResult() method in ChildDoctrineRepositoryClassTypeRector (#5747) --- .../Fixture/get_one_or_null.php.inc | 41 +++++++++++++++++++ ...ChildDoctrineRepositoryClassTypeRector.php | 22 ++++++---- 2 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector/Fixture/get_one_or_null.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector/Fixture/get_one_or_null.php.inc b/rules-tests/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector/Fixture/get_one_or_null.php.inc new file mode 100644 index 00000000000..338e608b650 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector/Fixture/get_one_or_null.php.inc @@ -0,0 +1,41 @@ + + */ +final class GetOneOrNull extends EntityRepository +{ + public function findSome() + { + return $this->createQueryBuilder() + ->getOneOrNullResult(); + } +} + +?> +----- + + */ +final class GetOneOrNull extends EntityRepository +{ + public function findSome(): ?SomeObject + { + return $this->createQueryBuilder() + ->getOneOrNullResult(); + } +} + +?> diff --git a/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php b/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php index 7a50297399e..bdd7732d48a 100644 --- a/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php +++ b/rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php @@ -39,9 +39,11 @@ public function __construct( public function getRuleDefinition(): RuleDefinition { - return new RuleDefinition('Add return type to classes that extend Doctrine\ORM\EntityRepository', [ - new CodeSample( - <<<'CODE_SAMPLE' + return new RuleDefinition( + 'Add return type to classes that extend Doctrine\ORM\EntityRepository based on return Doctrine method names', + [ + new CodeSample( + <<<'CODE_SAMPLE' use Doctrine\ORM\EntityRepository; /** @@ -57,8 +59,8 @@ public function getActiveItem() } } CODE_SAMPLE - , - <<<'CODE_SAMPLE' + , + <<<'CODE_SAMPLE' use Doctrine\ORM\EntityRepository; /** @@ -74,8 +76,10 @@ public function getActiveItem(): ?SomeType } } CODE_SAMPLE - ), - ]); + ), + + ] + ); } /** @@ -107,7 +111,9 @@ public function refactor(Node $node): ?Node continue; } - if ($this->containsMethodCallNamed($classMethod, 'findOneBy')) { + if ($this->containsMethodCallNamed($classMethod, 'getOneOrNullResult')) { + $classMethod->returnType = $this->createNullableType($entityClassName); + } elseif ($this->containsMethodCallNamed($classMethod, 'findOneBy')) { $classMethod->returnType = $this->createNullableType($entityClassName); }