Skip to content

Commit 4efe521

Browse files
authored
add document repository support (#461)
1 parent a57295c commit 4efe521

3 files changed

Lines changed: 55 additions & 9 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: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ public function __construct(
3737

3838
public function getRuleDefinition(): RuleDefinition
3939
{
40-
return new RuleDefinition('Add @extends ServiceEntityRepository<T> annotation to repository classes', [
41-
new CodeSample(
42-
<<<'CODE_SAMPLE'
40+
return new RuleDefinition(
41+
'Add @extends ServiceEntityRepository<T> annotation to repository classes that extends ServiceEntityRepository or ServiceDocumentRepository',
42+
[
43+
new CodeSample(
44+
<<<'CODE_SAMPLE'
4345
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
4446
4547
final class SomeRepository extends ServiceEntityRepository
@@ -50,8 +52,8 @@ public function __construct(ManagerRegistry $registry)
5052
}
5153
}
5254
CODE_SAMPLE
53-
,
54-
<<<'CODE_SAMPLE'
55+
,
56+
<<<'CODE_SAMPLE'
5557
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
5658
5759
/**
@@ -65,8 +67,10 @@ public function __construct(ManagerRegistry $registry)
6567
}
6668
}
6769
CODE_SAMPLE
68-
),
69-
]);
70+
),
71+
72+
]
73+
);
7074
}
7175

7276
/**
@@ -102,7 +106,11 @@ private function isRepositoryClass(Class_ $class): bool
102106
return false;
103107
}
104108

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

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

0 commit comments

Comments
 (0)