Skip to content

Commit

Permalink
Fix attribute support in ImproveDoctrineCollectionDocTypeInEntityRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 14, 2024
1 parent cec46f7 commit 26697a7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ use Rector\Doctrine\Tests\CodeQuality\Rector\Property\ImproveDoctrineCollectionD
class VarAndAttribute
{
/**
* @var Collection<int, Training>
* @var \Doctrine\Common\Collections\Collection<int, \Rector\Doctrine\Tests\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector\Source\Training>
*/
#[ORM\OneToMany(targetEntity:Training::class, mappedBy:"trainer")]
private $trainings = [];

/**
* @param Collection|\Training[] $trainings
*/
public function setTrainings($trainings)
{
$this->trainings = $trainings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ private function refactorProperty(Property $property): ?Property
CollectionMapping::TO_MANY_CLASSES,
'targetEntity'
);

if (! $targetEntityExpr instanceof Expr) {
return null;
}
Expand Down Expand Up @@ -240,14 +241,24 @@ private function refactorPropertyPhpDocInfo(Property $property, PhpDocInfo $phpD

private function refactorAttribute(Expr $expr, PhpDocInfo $phpDocInfo, Property $property): ?Property
{
$phpDocVarTagValueNode = $phpDocInfo->getVarTagValueNode();
$phpDocCollectionVarTagValueNode = $this->collectionVarTagValueNodeResolver->resolve($property);

if ($phpDocVarTagValueNode instanceof VarTagValueNode && ! $phpDocCollectionVarTagValueNode instanceof VarTagValueNode) {
return null;
$toManyAttribute = $this->attributeFinder->findAttributeByClasses(
$property,
CollectionMapping::TO_MANY_CLASSES
);
if ($toManyAttribute instanceof Node\Attribute) {
$targetEntityClassName = $this->targetEntityResolver->resolveFromAttribute($toManyAttribute);
} else {
$phpDocVarTagValueNode = $phpDocInfo->getVarTagValueNode();
$phpDocCollectionVarTagValueNode = $this->collectionVarTagValueNodeResolver->resolve($property);

if ($phpDocVarTagValueNode instanceof VarTagValueNode && ! $phpDocCollectionVarTagValueNode instanceof VarTagValueNode) {
return null;
}

$targetEntityClassName = $this->targetEntityResolver->resolveFromExpr($expr);
}

$targetEntityClassName = $this->targetEntityResolver->resolveFromExpr($expr);
if ($targetEntityClassName === null) {
return null;
}
Expand Down
19 changes: 19 additions & 0 deletions src/NodeAnalyzer/TargetEntityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Rector\Doctrine\NodeAnalyzer;

use PhpParser\Node\Attribute;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Exception\NotImplementedYetException;
Expand All @@ -19,6 +21,23 @@ public function __construct(
) {
}

public function resolveFromAttribute(Attribute $attribute): ?string
{
foreach ($attribute->args as $arg) {
if (! $arg->name instanceof Identifier) {
continue;
}

if ($arg->name->toString() !== 'targetEntity') {
continue;
}

return $this->resolveFromExpr($arg->value);
}

return null;
}

public function resolveFromExpr(Expr $targetEntityExpr): string|null
{
if ($targetEntityExpr instanceof ClassConstFetch) {
Expand Down

0 comments on commit 26697a7

Please sign in to comment.