rector/vendor/rector/rector-doctrine/rules/CodeQuality/AnnotationTransformer/PropertyAnnotationTransformer/ColumnAnnotationTransformer.php
Tomas Votruba 055cd31f66 Updated Rector to commit 2ff0f08bd7f7f6c848821bcabf0872f81a9c440b
2ff0f08bd7 [PHP 8.0] Add always class to AnnotationToAttribute to include string to ::class reference conversion (#5619)
2024-02-14 19:28:57 +00:00

45 lines
1.9 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;
use Rector\Doctrine\Enum\MappingClass;
final class ColumnAnnotationTransformer 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
{
$propertyMapping = $entityMapping->matchFieldPropertyMapping($property);
if ($propertyMapping === null) {
return;
}
// rename to "name"
if (isset($propertyMapping[EntityMappingKey::COLUMN])) {
$propertyMapping[EntityMappingKey::NAME] = $propertyMapping[EntityMappingKey::COLUMN];
unset($propertyMapping[EntityMappingKey::COLUMN]);
}
$arrayItemNodes = $this->arrayItemNodeFactory->create($propertyMapping, [EntityMappingKey::TYPE, EntityMappingKey::NAME]);
$spacelessPhpDocTagNode = DocTagNodeFactory::createSpacelessPhpDocTagNode($arrayItemNodes, $this->getClassName());
$propertyPhpDocInfo->addPhpDocTagNode($spacelessPhpDocTagNode);
}
public function getClassName() : string
{
return MappingClass::COLUMN;
}
}