rector/vendor/rector/rector-doctrine/rules/CodeQuality/AnnotationTransformer/PropertyAnnotationTransformer/OneToManyAnnotationTransformer.php
2024-02-12 13:17:09 +00:00

41 lines
1.7 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Doctrine\CodeQuality\AnnotationTransformer\PropertyAnnotationTransformer;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Doctrine\CodeQuality\Contract\PropertyAnnotationTransformerInterface;
use Rector\Doctrine\CodeQuality\DocTagNodeFactory;
use Rector\Doctrine\CodeQuality\Enum\EntityMappingKey;
use Rector\Doctrine\CodeQuality\NodeFactory\ArrayItemNodeFactory;
use Rector\Doctrine\CodeQuality\ValueObject\EntityMapping;
final class OneToManyAnnotationTransformer implements PropertyAnnotationTransformerInterface
{
/**
* @readonly
* @var \Rector\Doctrine\CodeQuality\NodeFactory\ArrayItemNodeFactory
*/
private $arrayItemNodeFactory;
public function __construct(ArrayItemNodeFactory $arrayItemNodeFactory)
{
$this->arrayItemNodeFactory = $arrayItemNodeFactory;
}
public function transform(EntityMapping $entityMapping, PhpDocInfo $propertyPhpDocInfo, Property $property) : void
{
$oneToManyMapping = $entityMapping->matchOneToManyPropertyMapping($property);
if (!\is_array($oneToManyMapping)) {
return;
}
// handled by OrderBy mapping rule as standalone entity class
unset($oneToManyMapping[EntityMappingKey::ORDER_BY]);
$arrayItemNodes = $this->arrayItemNodeFactory->create($oneToManyMapping, ['targetEntity', 'mappedBy']);
$spacelessPhpDocTagNode = DocTagNodeFactory::createSpacelessPhpDocTagNode($arrayItemNodes, $this->getClassName());
$propertyPhpDocInfo->addPhpDocTagNode($spacelessPhpDocTagNode);
}
public function getClassName() : string
{
return 'Doctrine\\ORM\\Mapping\\OneToMany';
}
}