Skip to content

Commit 7dc12bf

Browse files
committed
add document repository support
1 parent a57295c commit 7dc12bf

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

config/sets/doctrine-code-quality.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
TypedPropertyFromToOneRelationTypeRector::class,
2626
CompleteReturnDocblockFromToManyRector::class,
2727

28-
// annotations generics
28+
// @extends annotations service repository generics
2929
AddAnnotationToRepositoryRector::class,
3030
]);
3131

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Rector\Doctrine\Tests\Bundle230\Rector\Class_\AddAnnotationToRepositoryRector\Fixture;
4+
5+
use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepository;
6+
use Doctrine\Persistence\ManagerRegistry;
7+
use App\Entity\SomeEntity;
8+
9+
final class SomeServiceDocumentRepository extends ServiceDocumentRepository
10+
{
11+
public function __construct(ManagerRegistry $registry)
12+
{
13+
parent::__construct($registry, SomeEntity::class);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Doctrine\Tests\Bundle230\Rector\Class_\AddAnnotationToRepositoryRector\Fixture;
22+
23+
use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepository;
24+
use Doctrine\Persistence\ManagerRegistry;
25+
use App\Entity\SomeEntity;
26+
27+
/**
28+
* @extends \Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository<\App\Entity\SomeEntity>
29+
*/
30+
final class SomeServiceDocumentRepository extends ServiceDocumentRepository
31+
{
32+
public function __construct(ManagerRegistry $registry)
33+
{
34+
parent::__construct($registry, SomeEntity::class);
35+
}
36+
}
37+
38+
?>

rules/Bundle230/Rector/Class_/AddAnnotationToRepositoryRector.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public function __construct(
3737

3838
public function getRuleDefinition(): RuleDefinition
3939
{
40-
return new RuleDefinition('Add @extends ServiceEntityRepository<T> annotation to repository classes', [
40+
return new RuleDefinition(
41+
'Add @extends ServiceEntityRepository<T> annotation to repository classes that extends ServiceEntityRepository or ServiceDocumentRepository',
42+
[
4143
new CodeSample(
4244
<<<'CODE_SAMPLE'
4345
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
@@ -66,6 +68,7 @@ public function __construct(ManagerRegistry $registry)
6668
}
6769
CODE_SAMPLE
6870
),
71+
6972
]);
7073
}
7174

@@ -102,7 +105,11 @@ private function isRepositoryClass(Class_ $class): bool
102105
return false;
103106
}
104107

105-
return $this->isName($class->extends, DoctrineClass::SERVICE_ENTITY_REPOSITORY);
108+
if ($this->isName($class->extends, DoctrineClass::SERVICE_ENTITY_REPOSITORY)) {
109+
return true;
110+
}
111+
112+
return $this->isName($class->extends, DoctrineClass::SERVICE_DOCUMENT_REPOSITORY);
106113
}
107114

108115
private function getEntityClassFromConstructor(Class_ $class): ?string

0 commit comments

Comments
 (0)