Skip to content

Commit 787002f

Browse files
authored
fix document service repository class (#462)
1 parent 4efe521 commit 787002f

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

rules-tests/Bundle230/Rector/Class_/AddAnnotationToRepositoryRector/Fixture/some_service_document_repository.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use Doctrine\Persistence\ManagerRegistry;
2525
use App\Entity\SomeEntity;
2626

2727
/**
28-
* @extends \Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository<\App\Entity\SomeEntity>
28+
* @extends \Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepository<\App\Entity\SomeEntity>
2929
*/
3030
final class SomeServiceDocumentRepository extends ServiceDocumentRepository
3131
{

rules/Bundle230/Rector/Class_/AddAnnotationToRepositoryRector.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public function getNodeTypes(): array
8686
*/
8787
public function refactor(Node $node): ?Node
8888
{
89-
if (! $this->isRepositoryClass($node)) {
89+
$repositoryClass = $this->matchServiceRepositoryClass($node);
90+
if ($repositoryClass === null) {
9091
return null;
9192
}
9293

@@ -95,22 +96,25 @@ public function refactor(Node $node): ?Node
9596
return null;
9697
}
9798

98-
$this->addAnnotationToNode($node, $entityClass);
99+
$this->addAnnotationToNode($node, $entityClass, $repositoryClass);
99100

100101
return $node;
101102
}
102103

103-
private function isRepositoryClass(Class_ $class): bool
104+
private function matchServiceRepositoryClass(Class_ $class): ?string
104105
{
105106
if (! $class->extends instanceof Name) {
106-
return false;
107+
return null;
107108
}
108109

109-
if ($this->isName($class->extends, DoctrineClass::SERVICE_ENTITY_REPOSITORY)) {
110-
return true;
110+
if (! $this->isNames(
111+
$class->extends,
112+
[DoctrineClass::SERVICE_ENTITY_REPOSITORY, DoctrineClass::SERVICE_DOCUMENT_REPOSITORY]
113+
)) {
114+
return null;
111115
}
112116

113-
return $this->isName($class->extends, DoctrineClass::SERVICE_DOCUMENT_REPOSITORY);
117+
return $this->getName($class->extends);
114118
}
115119

116120
private function getEntityClassFromConstructor(Class_ $class): ?string
@@ -149,11 +153,11 @@ private function getEntityClassFromConstructor(Class_ $class): ?string
149153
return null;
150154
}
151155

152-
private function addAnnotationToNode(Class_ $class, string $entityClass): void
156+
private function addAnnotationToNode(Class_ $class, string $entityClass, string $repositoryClass): void
153157
{
154158
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);
155159

156-
$genericsAnnotation = sprintf('\%s<\%s>', DoctrineClass::SERVICE_ENTITY_REPOSITORY, $entityClass);
160+
$genericsAnnotation = sprintf('\%s<\%s>', $repositoryClass, $entityClass);
157161
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@extends', new GenericTagValueNode($genericsAnnotation)));
158162

159163
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($class);

0 commit comments

Comments
 (0)