mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-05 10:50:48 +00:00
055cd31f66
2ff0f08bd7
[PHP 8.0] Add always class to AnnotationToAttribute to include string to ::class reference conversion (#5619)
57 lines
2.7 KiB
PHP
57 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Doctrine\CodeQuality\AnnotationTransformer\ClassAnnotationTransformer;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
|
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
|
|
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
|
|
use Rector\BetterPhpDocParser\PhpDoc\StringNode;
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
|
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
|
|
use Rector\Doctrine\CodeQuality\Contract\ClassAnnotationTransformerInterface;
|
|
use Rector\Doctrine\CodeQuality\DocTagNodeFactory;
|
|
use Rector\Doctrine\CodeQuality\ValueObject\EntityMapping;
|
|
final class TableClassAnnotationTransformer implements ClassAnnotationTransformerInterface
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
private const TABLE_KEY = 'table';
|
|
/**
|
|
* @var string
|
|
*/
|
|
private const UNIQUE_CONSTRAINTS_CLASS = 'Doctrine\\ORM\\Mapping\\UniqueConstraint';
|
|
public function transform(EntityMapping $entityMapping, PhpDocInfo $classPhpDocInfo) : void
|
|
{
|
|
$classMapping = $entityMapping->getClassMapping();
|
|
$table = $classMapping[self::TABLE_KEY] ?? null;
|
|
if (isset($classMapping['type']) && $classMapping['type'] !== 'entity') {
|
|
return;
|
|
}
|
|
$arrayItemNodes = [];
|
|
if (\is_string($table)) {
|
|
$arrayItemNodes[] = new ArrayItemNode(new StringNode($table), 'name');
|
|
}
|
|
$uniqueConstraints = $classMapping['uniqueConstraints'] ?? null;
|
|
if ($uniqueConstraints) {
|
|
$uniqueConstraintDoctrineAnnotationTagValueNodes = [];
|
|
foreach ($uniqueConstraints as $name => $uniqueConstraint) {
|
|
$columnArrayItems = [];
|
|
foreach ($uniqueConstraint['columns'] as $column) {
|
|
$columnArrayItems[] = new ArrayItemNode(new StringNode($column));
|
|
}
|
|
$columnCurlList = new CurlyListNode($columnArrayItems);
|
|
$uniqueConstraintDoctrineAnnotationTagValueNodes[] = new ArrayItemNode(new DoctrineAnnotationTagValueNode(new IdentifierTypeNode('@\\' . self::UNIQUE_CONSTRAINTS_CLASS), null, [new ArrayItemNode(new StringNode($name), 'name'), new ArrayItemNode($columnCurlList, 'columns')]));
|
|
}
|
|
$arrayItemNodes[] = new ArrayItemNode(new CurlyListNode($uniqueConstraintDoctrineAnnotationTagValueNodes), 'uniqueConstraints');
|
|
}
|
|
$spacelessPhpDocTagNode = DocTagNodeFactory::createSpacelessPhpDocTagNode($arrayItemNodes, $this->getClassName());
|
|
$classPhpDocInfo->addPhpDocTagNode($spacelessPhpDocTagNode);
|
|
}
|
|
public function getClassName() : string
|
|
{
|
|
return 'Doctrine\\ORM\\Mapping\\Table';
|
|
}
|
|
}
|