[Doctrine] Skip ServiceEntityRepositoryParentCallToDIRector on non __construct method (#5333)

Co-authored-by: Dominik Firla <dominik.firla@carvago.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-01-27 20:13:27 +07:00 committed by GitHub
parent 6f17c9c114
commit 453b8ba0b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View File

@ -11,6 +11,7 @@ use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Doctrine\NodeFactory\RepositoryNodeFactory;
use Rector\Doctrine\Type\RepositoryTypeFactory;
use Rector\NodeTypeResolver\Node\AttributeKey;
@ -142,6 +143,10 @@ CODE_SAMPLE
private function shouldSkipClassMethod(ClassMethod $classMethod): bool
{
if (! $this->isName($classMethod, MethodName::CONSTRUCT)) {
return true;
}
/** @var string|null $parentClassName */
$parentClassName = $classMethod->getAttribute(AttributeKey::PARENT_CLASS_NAME);
if ($parentClassName === null) {

View File

@ -0,0 +1,49 @@
<?php
namespace Rector\Doctrine\Tests\Rector\ClassMethod\ServiceEntityRepositoryParentCallToDIRector\Fixture;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Ramsey\Uuid\UuidInterface;
use Rector\Doctrine\Tests\Rector\ClassMethod\ServiceEntityRepositoryParentCallToDIRector\Source\Project;
final class DoNotChangeOtherMethods extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Project::class);
}
public function findBy(UuidInterface $id): void
{
}
}
?>
-----
<?php
namespace Rector\Doctrine\Tests\Rector\ClassMethod\ServiceEntityRepositoryParentCallToDIRector\Fixture;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Ramsey\Uuid\UuidInterface;
use Rector\Doctrine\Tests\Rector\ClassMethod\ServiceEntityRepositoryParentCallToDIRector\Source\Project;
final class DoNotChangeOtherMethods extends ServiceEntityRepository
{
/**
* @var \Doctrine\ORM\EntityRepository<\Rector\Doctrine\Tests\Rector\ClassMethod\ServiceEntityRepositoryParentCallToDIRector\Source\Project>
*/
private \Doctrine\ORM\EntityRepository $repository;
public function __construct(private \Doctrine\ORM\EntityManagerInterface $entityManager)
{
$this->repository = $entityManager->getRepository(Project::class);
}
public function findBy(UuidInterface $id): void
{
}
}
?>