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 15, 2024
1 parent cec46f7 commit aacc447
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 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 @@ -6,5 +6,4 @@
use Rector\Doctrine\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector;

return RectorConfig::configure()
->withRules([ImproveDoctrineCollectionDocTypeInEntityRector::class])
->withPhpVersion(\Rector\ValueObject\PhpVersionFeature::ATTRIBUTES);
->withRules([ImproveDoctrineCollectionDocTypeInEntityRector::class]);
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Doctrine\CodeQuality\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -126,6 +127,7 @@ private function refactorProperty(Property $property): ?Property
CollectionMapping::TO_MANY_CLASSES,
'targetEntity'
);

if (! $targetEntityExpr instanceof Expr) {
return null;
}
Expand Down Expand Up @@ -240,14 +242,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 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 aacc447

Please sign in to comment.