Skip to content

Commit

Permalink
[code-quality] Add GetRepositoryServiceLocatorToRepositoryServiceInje…
Browse files Browse the repository at this point in the history
…ctionRector (#368)

* [code-quality] Add GetRepositoryServiceLocatorToRepositoryServiceInjectionRector

* skip callable
  • Loading branch information
TomasVotruba authored Feb 26, 2025
1 parent 33f8d9b commit f058b83
Show file tree
Hide file tree
Showing 18 changed files with 531 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Fixture;

use Doctrine\ODM\MongoDB\DocumentManager;
use Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source\Entity\SomeEntityUsingService;

final class GetODMRepositoryOnService
{
public function run(DocumentManager $entityManager)
{
$someRepository = $entityManager->getRepository(SomeEntityUsingService::class);

return $someRepository->find(1);
}
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Fixture;

use Doctrine\ODM\MongoDB\DocumentManager;
use Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source\Entity\SomeEntityUsingService;

final class GetODMRepositoryOnService
{
public function __construct(private readonly \Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source\SomeServiceRepository $someEntityUsingServiceRepository)
{
}
public function run(DocumentManager $entityManager)
{
$someRepository = $this->someEntityUsingServiceRepository;

return $someRepository->find(1);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Fixture;

use Doctrine\ORM\EntityManagerInterface;
use Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source\Entity\SomeEntityUsingORMService;

final class GetRepositoryOnService
{
public function run(EntityManagerInterface $entityManager)
{
$someRepository = $entityManager->getRepository(SomeEntityUsingORMService::class);

return $someRepository->find(1);
}
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Fixture;

use Doctrine\ORM\EntityManagerInterface;
use Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source\Entity\SomeEntityUsingORMService;

final class GetRepositoryOnService
{
public function __construct(private readonly \Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source\SomeORMServiceRepository $someEntityUsingORMServiceRepository)
{
}
public function run(EntityManagerInterface $entityManager)
{
$someRepository = $this->someEntityUsingORMServiceRepository;

return $someRepository->find(1);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Fixture;

use Doctrine\ORM\EntityManagerInterface;

final class SkipCallable
{
public function run(EntityManagerInterface $entityManager)
{
$someRepository = $entityManager->getRepository(...);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Fixture;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source\Entity\SomeEntityUsingService;

final class SkipInsideRepository extends EntityRepository
{
public function run(EntityManagerInterface $entityManager)
{
$someRepository = $entityManager->getRepository(SomeEntityUsingService::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class GetRepositoryServiceLocatorToRepositoryServiceInjectionRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source\Entity;

/**
* @ORM\Entity(repositoryClass="Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source\SomeORMServiceRepository")
*/
class SomeEntityUsingORMService
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source\Entity;

use Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source\SomeServiceRepository;

/**
* @ORM\Entity(repositoryClass=SomeServiceRepository::class)
*/
class SomeEntityUsingService
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;

final class SomeORMServiceRepository extends ServiceEntityRepository
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector\Source;

use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepository;

final class SomeServiceRepository extends ServiceDocumentRepository
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\CodeQuality\Rector\Class_\GetRepositoryServiceLocatorToRepositoryServiceInjectionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(GetRepositoryServiceLocatorToRepositoryServiceInjectionRector::class);
};
20 changes: 20 additions & 0 deletions rules/CodeQuality/Enum/DoctrineClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,24 @@
* @var string
*/
public const COLLECTION = 'Doctrine\Common\Collections\Collection';

/**
* @var string
*/
public const SERVICE_DOCUMENT_REPOSITORY = 'Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepository';

/**
* @var string
*/
public const SERVICE_ENTITY_REPOSITORY = 'Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository';

/**
* @var string
*/
public const ENTITY_REPOSITORY = 'Doctrine\ORM\EntityRepository';

/**
* @var string
*/
public const OBJECT_REPOSITORY = 'Doctrine\Persistence\ObjectRepository';
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function getRuleDefinition(): RuleDefinition
new CodeSample(
<<<'CODE_SAMPLE'
use App\Entity\Training;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
Expand All @@ -63,6 +64,8 @@ public function getTrainings()
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
Expand Down
Loading

0 comments on commit f058b83

Please sign in to comment.