Skip to content

Commit

Permalink
Add support for getOneOrNullResult() method in ChildDoctrineRepositor…
Browse files Browse the repository at this point in the history
…yClassTypeRector (#5747)
  • Loading branch information
TomasVotruba committed Mar 21, 2024
1 parent f4070bb commit 0c17740
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Fixture;

use Doctrine\ORM\EntityRepository;
use Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Source\SomeObject;

/**
* @extends EntityRepository<SomeObject>
*/
final class GetOneOrNull extends EntityRepository
{
public function findSome()
{
return $this->createQueryBuilder()
->getOneOrNullResult();
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Fixture;

use Doctrine\ORM\EntityRepository;
use Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Source\SomeObject;

/**
* @extends EntityRepository<SomeObject>
*/
final class GetOneOrNull extends EntityRepository
{
public function findSome(): ?SomeObject
{
return $this->createQueryBuilder()
->getOneOrNullResult();
}
}

?>
Expand Up @@ -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;
/**
Expand All @@ -57,8 +59,8 @@ public function getActiveItem()
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
,
<<<'CODE_SAMPLE'
use Doctrine\ORM\EntityRepository;
/**
Expand All @@ -74,8 +76,10 @@ public function getActiveItem(): ?SomeType
}
}
CODE_SAMPLE
),
]);
),

]
);
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 0c17740

Please sign in to comment.