Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use Doctrine\Persistence\ManagerRegistry;
use App\Entity\SomeEntity;

/**
* @extends \Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository<\App\Entity\SomeEntity>
* @extends \Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepository<\App\Entity\SomeEntity>
*/
final class SomeServiceDocumentRepository extends ServiceDocumentRepository
{
Expand Down
22 changes: 13 additions & 9 deletions rules/Bundle230/Rector/Class_/AddAnnotationToRepositoryRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isRepositoryClass($node)) {
$repositoryClass = $this->matchServiceRepositoryClass($node);
if ($repositoryClass === null) {
return null;
}

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

$this->addAnnotationToNode($node, $entityClass);
$this->addAnnotationToNode($node, $entityClass, $repositoryClass);

return $node;
}

private function isRepositoryClass(Class_ $class): bool
private function matchServiceRepositoryClass(Class_ $class): ?string
{
if (! $class->extends instanceof Name) {
return false;
return null;
}

if ($this->isName($class->extends, DoctrineClass::SERVICE_ENTITY_REPOSITORY)) {
return true;
if (! $this->isNames(
$class->extends,
[DoctrineClass::SERVICE_ENTITY_REPOSITORY, DoctrineClass::SERVICE_DOCUMENT_REPOSITORY]
)) {
return null;
}

return $this->isName($class->extends, DoctrineClass::SERVICE_DOCUMENT_REPOSITORY);
return $this->getName($class->extends);
}

private function getEntityClassFromConstructor(Class_ $class): ?string
Expand Down Expand Up @@ -149,11 +153,11 @@ private function getEntityClassFromConstructor(Class_ $class): ?string
return null;
}

private function addAnnotationToNode(Class_ $class, string $entityClass): void
private function addAnnotationToNode(Class_ $class, string $entityClass, string $repositoryClass): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);

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

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($class);
Expand Down