rector/vendor/rector/rector-doctrine/rules/CodeQuality/Rector/Property/TypedPropertyFromDoctrineCollectionRector.php
Tomas Votruba 11b9220a05 Updated Rector to commit b4eb883e9110f50607ce2eed629c237b5e7e7356
b4eb883e91 [DeadCode] Skip isset() from property fetch from docblock on RemoveAlwaysTrueIfConditionRector (#5754)
2024-03-22 00:10:36 +00:00

117 lines
3.7 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Doctrine\CodeQuality\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Doctrine\TypeAnalyzer\DoctrineCollectionTypeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\ValueObject\PhpVersion;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Doctrine\Tests\CodeQuality\Rector\Property\TypedPropertyFromDoctrineCollectionRector\TypedPropertyFromDoctrineCollectionRectorTest
* @deprecated Use more complete \Rector\Doctrine\CodeQuality\Rector\Class_\ExplicitRelationCollectionRector instead
*/
final class TypedPropertyFromDoctrineCollectionRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\Doctrine\TypeAnalyzer\DoctrineCollectionTypeAnalyzer
*/
private $doctrineCollectionTypeAnalyzer;
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
*/
private $phpDocInfoFactory;
/**
* @readonly
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
public function __construct(DoctrineCollectionTypeAnalyzer $doctrineCollectionTypeAnalyzer, PhpDocInfoFactory $phpDocInfoFactory, StaticTypeMapper $staticTypeMapper)
{
$this->doctrineCollectionTypeAnalyzer = $doctrineCollectionTypeAnalyzer;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->staticTypeMapper = $staticTypeMapper;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Add typed property based on Doctrine collection', [new CodeSample(<<<'CODE_SAMPLE'
use Doctrine\ORM\Mapping as ORM;
use App\Entity\TrainingTerm;
/**
* @ORM\Entity
*/
class DoctrineCollection
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\TrainingTerm", mappedBy="training")
* @var TrainingTerm[]|Collection
*/
private $trainingTerms;
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
use Doctrine\ORM\Mapping as ORM;
use App\Entity\TrainingTerm;
/**
* @ORM\Entity
*/
class DoctrineCollection
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\TrainingTerm", mappedBy="training")
* @var TrainingTerm[]|Collection
*/
private \Doctrine\Common\Collections\Collection $trainingTerms;
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Property::class];
}
/**
* @param Property $node
*/
public function refactor(Node $node) : ?Node
{
if ($node->type !== null) {
return null;
}
$propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (!$propertyPhpDocInfo instanceof PhpDocInfo) {
return null;
}
$varTagValueNode = $propertyPhpDocInfo->getVarTagValueNode();
if (!$varTagValueNode instanceof VarTagValueNode) {
return null;
}
$varTagType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $node);
if (!$this->doctrineCollectionTypeAnalyzer->detect($varTagType)) {
return null;
}
$node->type = new FullyQualified('Doctrine\\Common\\Collections\\Collection');
return $node;
}
public function provideMinPhpVersion() : int
{
return PhpVersion::PHP_74;
}
}