Skip to content

Commit

Permalink
Add odm support to AddReturnDocBlockToCollectionPropertyGetterByToMan…
Browse files Browse the repository at this point in the history
…yAnnotationRector (#330)

* add failing fixture

* add odm support to AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector
  • Loading branch information
TomasVotruba authored Jul 25, 2024
1 parent f43ecb2 commit 8bcc255
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector\Fixture\Odm;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Rector\Doctrine\Tests\CodeQuality\Rector\Class_\AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector\Source\Training;

/**
* @\Doctrine\ODM\MongoDB\Mapping\Annotations\Document
*/
final class OdmMany
{
/**
* @\Doctrine\ODM\MongoDB\Mapping\Annotations\ReferenceMany(targetDocument="\Rector\Doctrine\Tests\CodeQuality\Rector\Class_\AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector\Source\Training")
*/
private $items = [];

public function getItems()
{
return $this->items;
}
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Class_\AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector\Fixture\Odm;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Rector\Doctrine\Tests\CodeQuality\Rector\Class_\AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector\Source\Training;

/**
* @\Doctrine\ODM\MongoDB\Mapping\Annotations\Document
*/
final class OdmMany
{
/**
* @\Doctrine\ODM\MongoDB\Mapping\Annotations\ReferenceMany(targetDocument="\Rector\Doctrine\Tests\CodeQuality\Rector\Class_\AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector\Source\Training")
*/
private $items = [];

/**
* @return \Doctrine\Common\Collections\Collection<int, \\Rector\Doctrine\Tests\CodeQuality\Rector\Class_\AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector\Source\Training>
*/
public function getItems()
{
return $this->items;
}
}

?>
11 changes: 9 additions & 2 deletions rules/CodeQuality/Enum/ToManyMappings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

namespace Rector\Doctrine\CodeQuality\Enum;

use Rector\Doctrine\Enum\MappingClass;
use Rector\Doctrine\Enum\OdmMappingClass;

class ToManyMappings
{
/**
* @var class-string[]
* @var string[]
*/
final public const TO_MANY_CLASSES = ['Doctrine\\ORM\\Mapping\\OneToMany', 'Doctrine\\ORM\\Mapping\\ManyToMany'];
final public const TO_MANY_CLASSES = [
MappingClass::ONE_TO_MANY,
MappingClass::MANY_TO_MANY,
OdmMappingClass::REFERENCE_MANY,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,11 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
}

$property = $this->methodUniqueReturnedPropertyResolver->resolve($node, $classMethod);

if (! $property instanceof Property) {
continue;
}

$collectionObjectType = $this->collectionTypeResolver->resolveFromToManyProperties($property);

if (! $collectionObjectType instanceof FullyQualifiedObjectType) {
continue;
}
Expand Down
7 changes: 6 additions & 1 deletion src/PhpDocParser/DoctrineDocBlockResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use PhpParser\Node\Stmt\Class_;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Doctrine\Enum\MappingClass;
use Rector\Doctrine\Enum\OdmMappingClass;

final readonly class DoctrineDocBlockResolver
{
Expand All @@ -17,6 +19,9 @@ public function __construct(
public function isDoctrineEntityClass(Class_ $class): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);
return $phpDocInfo->hasByAnnotationClasses(['Doctrine\ORM\Mapping\Entity', 'Doctrine\ORM\Mapping\Embeddable']);

return $phpDocInfo->hasByAnnotationClasses(
[MappingClass::ENTITY, MappingClass::EMBEDDABLE, OdmMappingClass::DOCUMENT]
);
}
}
8 changes: 6 additions & 2 deletions src/TypeAnalyzer/CollectionTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ public function resolveFromToManyProperties(Property $property): ?FullyQualified
}

$targetEntityArrayItemNode = $doctrineAnnotationTagValueNode->getValue('targetEntity');
if (! $targetEntityArrayItemNode instanceof ArrayItemNode) {
// in case of odm
$targetDocumentArrayItemNode = $doctrineAnnotationTagValueNode->getValue('targetDocument');

$targetArrayItemNode = $targetEntityArrayItemNode ?: $targetDocumentArrayItemNode;
if (! $targetArrayItemNode instanceof ArrayItemNode) {
return null;
}

$targetEntityClass = $targetEntityArrayItemNode->value;
$targetEntityClass = $targetArrayItemNode->value;

if ($targetEntityClass instanceof StringNode) {
$targetEntityClass = $targetEntityClass->value;
Expand Down

0 comments on commit 8bcc255

Please sign in to comment.