Add uuid only to entities with id (#1921)

Add uuid only to entities with id
This commit is contained in:
Tomáš Votruba 2019-08-29 18:05:01 +02:00 committed by GitHub
commit 7ab5511705
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 492 additions and 93 deletions

View File

@ -21,6 +21,7 @@ use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\ManyToManyTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\ManyToOneTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\OneToManyTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\OneToOneTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\TableTagValueNode;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineRelationTagValueNodeInterface;
final class PhpDocInfo
@ -181,6 +182,11 @@ final class PhpDocInfo
return $this->matchChildValueNodeOfType(IdTagValueNode::class);
}
public function getDoctrineTableTagValueNode(): ?TableTagValueNode
{
return $this->matchChildValueNodeOfType(TableTagValueNode::class);
}
public function getDoctrineManyToManyTagValueNode(): ?ManyToManyTagValueNode
{
return $this->matchChildValueNodeOfType(ManyToManyTagValueNode::class);

View File

@ -5,6 +5,7 @@ namespace Rector\Doctrine\AbstarctRector;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\Doctrine\PhpDocParser\DoctrineDocBlockResolver;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineRelationTagValueNodeInterface;
trait DoctrineTrait
{
@ -26,8 +27,32 @@ trait DoctrineTrait
return $this->doctrineDocBlockResolver->isDoctrineEntityClass($class);
}
protected function isDoctrineEntityClassWithIdProperty(Class_ $class): bool
{
if (! $this->doctrineDocBlockResolver->isDoctrineEntityClass($class)) {
return false;
}
foreach ($class->stmts as $classStmt) {
if (! $classStmt instanceof Property) {
continue;
}
if ($this->doctrineDocBlockResolver->hasPropertyDoctrineIdTag($classStmt)) {
return true;
}
}
return false;
}
protected function getTargetEntity(Property $property): ?string
{
return $this->doctrineDocBlockResolver->getTargetEntity($property);
}
protected function getDoctrineRelationTagValueNode(Property $property): ?DoctrineRelationTagValueNodeInterface
{
return $this->doctrineDocBlockResolver->getDoctrineRelationTagValueNode($property);
}
}

View File

@ -1,24 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\Doctrine\Collector;
final class EntitiesWithAddedUuidPropertyCollector
{
/**
* @var string[]
*/
private $classes = [];
public function addClass(string $class): void
{
$this->classes[] = $class;
}
/**
* @return string[]
*/
public function getClasses(): array
{
return $this->classes;
}
}

View File

@ -0,0 +1,37 @@
<?php declare(strict_types=1);
namespace Rector\Doctrine\Collector;
final class UuidMigrationDataCollector
{
/**
* @var mixed[]
*/
private $propertiesByClass = [];
public function addClassAndProperty(string $class, string $property): void
{
$this->propertiesByClass[$class]['properties'][] = $property;
}
public function addClassToManyRelationProperty(string $class, string $property, string $tableName): void
{
$this->propertiesByClass[$class]['to_many_relations'][] = [
'property' => $property,
'table_name' => $tableName,
];
}
public function addClassToOneRelationProperty(string $class, string $property): void
{
$this->propertiesByClass[$class]['to_one_relations'][] = $property;
}
/**
* @return string[][][]
*/
public function getPropertiesByClass(): array
{
return $this->propertiesByClass;
}
}

View File

@ -0,0 +1,46 @@
<?php declare(strict_types=1);
namespace Rector\Doctrine\Extension;
use Nette\Utils\Json;
use Rector\Contract\Extension\FinishingExtensionInterface;
use Rector\Doctrine\Collector\UuidMigrationDataCollector;
use Symfony\Component\Console\Style\SymfonyStyle;
final class ReportEntitiesWithAddedPropertiesFinishExtension implements FinishingExtensionInterface
{
/**
* @var UuidMigrationDataCollector
*/
private $uuidMigrationDataCollector;
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
public function __construct(
UuidMigrationDataCollector $uuidMigrationDataCollector,
SymfonyStyle $symfonyStyle
) {
$this->uuidMigrationDataCollector = $uuidMigrationDataCollector;
$this->symfonyStyle = $symfonyStyle;
}
public function run(): void
{
$propertiesByClass = $this->uuidMigrationDataCollector->getPropertiesByClass();
if ($propertiesByClass === []) {
return;
}
$data = [
'title' => 'Entities with new properties',
'added_properties_by_class' => $propertiesByClass,
];
$jsonContent = Json::encode($data, Json::PRETTY);
$this->symfonyStyle->writeln($jsonContent);
}
}

View File

@ -1,40 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\Doctrine\Extension;
use Rector\Contract\Extension\FinishingExtensionInterface;
use Rector\Doctrine\Collector\EntitiesWithAddedUuidPropertyCollector;
use Symfony\Component\Console\Style\SymfonyStyle;
final class ReportEntitiesWithAddedUuidFinishExtension implements FinishingExtensionInterface
{
/**
* @var EntitiesWithAddedUuidPropertyCollector
*/
private $entitiesWithAddedUuidPropertyCollector;
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
public function __construct(
EntitiesWithAddedUuidPropertyCollector $entitiesWithAddedUuidPropertyCollector,
SymfonyStyle $symfonyStyle
) {
$this->entitiesWithAddedUuidPropertyCollector = $entitiesWithAddedUuidPropertyCollector;
$this->symfonyStyle = $symfonyStyle;
}
public function run(): void
{
$classes = $this->entitiesWithAddedUuidPropertyCollector->getClasses();
if ($classes === []) {
return;
}
$this->symfonyStyle->section('Entities with new nullable $uuid property');
$this->symfonyStyle->listing($classes);
}
}

View File

@ -6,6 +6,7 @@ use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\TableTagValueNode;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineRelationTagValueNodeInterface;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
@ -41,6 +42,16 @@ final class DoctrineDocBlockResolver
return $doctrineRelationTagValueNode->getTargetEntity();
}
public function hasPropertyDoctrineIdTag(Property $property): bool
{
$propertyPhpDocInfo = $this->getPhpDocInfo($property);
if ($propertyPhpDocInfo === null) {
return false;
}
return (bool) $propertyPhpDocInfo->getDoctrineIdTagValueNode();
}
public function getDoctrineRelationTagValueNode(Property $property): ?DoctrineRelationTagValueNodeInterface
{
$propertyPhpDocInfo = $this->getPhpDocInfo($property);
@ -51,6 +62,16 @@ final class DoctrineDocBlockResolver
return $propertyPhpDocInfo->getDoctrineRelationTagValueNode();
}
public function getDoctrineTableTagValueNode(Class_ $class): ?TableTagValueNode
{
$classPhpDocInfo = $this->getPhpDocInfo($class);
if ($classPhpDocInfo === null) {
return null;
}
return $classPhpDocInfo->getDoctrineTableTagValueNode();
}
private function getPhpDocInfo(Node $node): ?PhpDocInfo
{
if ($node->getDocComment() === null) {

View File

@ -10,10 +10,13 @@ use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\Node\VarLikeIdentifier;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Doctrine\Collector\UuidMigrationDataCollector;
use Rector\Doctrine\PhpDocParser\Ast\PhpDoc\PhpDocTagNodeFactory;
use Rector\Doctrine\Uuid\UuidTableNameResolver;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineRelationTagValueNodeInterface;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\ToManyTagNodeInterface;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\ToOneTagNodeInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\RectorDefinition;
@ -33,12 +36,26 @@ final class AddUuidMirrorForRelationPropertyRector extends AbstractRector
*/
private $phpDocTagNodeFactory;
/**
* @var UuidMigrationDataCollector
*/
private $uuidMigrationDataCollector;
/**
* @var UuidTableNameResolver
*/
private $uuidTableNameResolver;
public function __construct(
DocBlockManipulator $docBlockManipulator,
PhpDocTagNodeFactory $phpDocTagNodeFactory
PhpDocTagNodeFactory $phpDocTagNodeFactory,
UuidMigrationDataCollector $uuidMigrationDataCollector,
UuidTableNameResolver $uuidTableNameResolver
) {
$this->docBlockManipulator = $docBlockManipulator;
$this->phpDocTagNodeFactory = $phpDocTagNodeFactory;
$this->uuidMigrationDataCollector = $uuidMigrationDataCollector;
$this->uuidTableNameResolver = $uuidTableNameResolver;
}
public function getDefinition(): RectorDefinition
@ -99,18 +116,18 @@ final class AddUuidMirrorForRelationPropertyRector extends AbstractRector
$newPropertyProperty = new PropertyProperty(new VarLikeIdentifier($uuidPropertyName));
$propertyWithUuid->props = [$newPropertyProperty];
$this->addNewPropertyToCollector($property, $uuidPropertyName);
return $propertyWithUuid;
}
private function updateDocComment(Property $property): void
{
/** @var PhpDocInfo $propertyPhpDocInfo */
$propertyPhpDocInfo = $this->getPhpDocInfo($property);
if ($propertyPhpDocInfo === null) {
return;
}
/** @var DoctrineRelationTagValueNodeInterface $doctrineRelationTagValueNode */
$doctrineRelationTagValueNode = $propertyPhpDocInfo->getDoctrineRelationTagValueNode();
$doctrineRelationTagValueNode = $this->getDoctrineRelationTagValueNode($property);
if ($doctrineRelationTagValueNode instanceof ToManyTagNodeInterface) {
$this->refactorToManyPropertyPhpDocInfo($propertyPhpDocInfo, $property);
@ -130,9 +147,8 @@ final class AddUuidMirrorForRelationPropertyRector extends AbstractRector
$propertyPhpDocInfo->removeTagValueNodeFromNode($doctrineJoinColumnTagValueNode);
}
$propertyPhpDocInfo->getPhpDocNode()->children[] = $this->phpDocTagNodeFactory->createJoinTableTagNode(
$property
);
$joinTableTagNode = $this->phpDocTagNodeFactory->createJoinTableTagNode($property);
$propertyPhpDocInfo->getPhpDocNode()->children[] = $joinTableTagNode;
}
private function refactorToOnePropertyPhpDocInfo(PhpDocInfo $propertyPhpDocInfo): void
@ -189,4 +205,25 @@ final class AddUuidMirrorForRelationPropertyRector extends AbstractRector
return false;
}
private function addNewPropertyToCollector(Property $property, string $propertyName): void
{
/** @var string $className */
$className = $property->getAttribute(AttributeKey::CLASS_NAME);
/** @var DoctrineRelationTagValueNodeInterface $doctrineRelationTagValueNode */
$doctrineRelationTagValueNode = $this->getDoctrineRelationTagValueNode($property);
$joinTableName = $this->uuidTableNameResolver->resolveManyToManyTableNameForProperty($property);
if ($doctrineRelationTagValueNode instanceof ToManyTagNodeInterface) {
$this->uuidMigrationDataCollector->addClassToManyRelationProperty(
$className,
$propertyName,
$joinTableName
);
} elseif ($doctrineRelationTagValueNode instanceof ToOneTagNodeInterface) {
$this->uuidMigrationDataCollector->addClassToOneRelationProperty($className, $propertyName);
}
}
}

View File

@ -8,7 +8,7 @@ use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\Doctrine\Collector\EntitiesWithAddedUuidPropertyCollector;
use Rector\Doctrine\Collector\UuidMigrationDataCollector;
use Rector\Doctrine\NodeFactory\EntityUuidNodeFactory;
use Rector\PhpParser\Node\Manipulator\ClassManipulator;
use Rector\Rector\AbstractRector;
@ -30,18 +30,18 @@ final class AddUuidToEntityWhereMissingRector extends AbstractRector
private $classManipulator;
/**
* @var EntitiesWithAddedUuidPropertyCollector
* @var UuidMigrationDataCollector
*/
private $entitiesWithAddedUuidPropertyCollector;
private $uuidMigrationDataCollector;
public function __construct(
EntityUuidNodeFactory $entityUuidNodeFactory,
ClassManipulator $classManipulator,
EntitiesWithAddedUuidPropertyCollector $entitiesWithAddedUuidPropertyCollector
UuidMigrationDataCollector $uuidMigrationDataCollector
) {
$this->entityUuidNodeFactory = $entityUuidNodeFactory;
$this->classManipulator = $classManipulator;
$this->entitiesWithAddedUuidPropertyCollector = $entitiesWithAddedUuidPropertyCollector;
$this->uuidMigrationDataCollector = $uuidMigrationDataCollector;
}
public function getDefinition(): RectorDefinition
@ -66,7 +66,7 @@ final class AddUuidToEntityWhereMissingRector extends AbstractRector
*/
public function refactor(Node $node): ?Node
{
if (! $this->isDoctrineEntityClass($node)) {
if (! $this->isDoctrineEntityClassWithIdProperty($node)) {
return null;
}
@ -95,7 +95,7 @@ final class AddUuidToEntityWhereMissingRector extends AbstractRector
/** @var string $class */
$class = $this->getName($node);
$this->entitiesWithAddedUuidPropertyCollector->addClass($class);
$this->uuidMigrationDataCollector->addClassAndProperty($class, 'uuid');
return $node;
}

View File

@ -3,9 +3,11 @@
namespace Rector\Doctrine\Uuid;
use Nette\Utils\Strings;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\Doctrine\PhpDocParser\DoctrineDocBlockResolver;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class UuidTableNameResolver
@ -15,9 +17,17 @@ final class UuidTableNameResolver
*/
private $doctrineDocBlockResolver;
public function __construct(DoctrineDocBlockResolver $doctrineDocBlockResolver)
{
/**
* @var ParsedNodesByType
*/
private $parsedNodesByType;
public function __construct(
DoctrineDocBlockResolver $doctrineDocBlockResolver,
ParsedNodesByType $parsedNodesByType
) {
$this->doctrineDocBlockResolver = $doctrineDocBlockResolver;
$this->parsedNodesByType = $parsedNodesByType;
}
/**
@ -25,18 +35,24 @@ final class UuidTableNameResolver
*/
public function resolveManyToManyTableNameForProperty(Property $property): string
{
/** @var string $currentClass */
$currentClass = $property->getAttribute(AttributeKey::CLASS_NAME);
$shortCurrentClass = $this->resolveShortClassName($currentClass);
/** @var Class_ $currentClass */
$currentClass = $property->getAttribute(AttributeKey::CLASS_NODE);
$currentTableName = $this->resolveTableNameFromClass($currentClass);
$targetEntity = $this->doctrineDocBlockResolver->getTargetEntity($property);
if ($targetEntity === null) {
throw new ShouldNotHappenException(__METHOD__);
}
$shortTargetEntity = $this->resolveShortClassName($targetEntity);
$targetEntityClass = $this->parsedNodesByType->findClass($targetEntity);
if ($targetEntityClass === null) {
// dummy fallback
$targetTableName = $this->resolveShortClassName($targetEntity);
} else {
$targetTableName = $this->resolveTableNameFromClass($targetEntityClass);
}
return strtolower($shortCurrentClass . '_uuid_' . $shortTargetEntity . '_uuid');
return strtolower($currentTableName . '_' . $targetTableName . '_uuid');
}
private function resolveShortClassName(string $currentClass): string
@ -47,4 +63,20 @@ final class UuidTableNameResolver
return (string) Strings::after($currentClass, '\\', -1);
}
private function resolveTableNameFromClass(Class_ $class): string
{
$tableTagValueNode = $this->doctrineDocBlockResolver->getDoctrineTableTagValueNode($class);
if ($tableTagValueNode !== null) {
$tableName = $tableTagValueNode->getName();
if ($tableName !== null) {
return $tableName;
}
}
/** @var string $className */
$className = $class->getAttribute(AttributeKey::CLASS_NAME);
return $this->resolveShortClassName($className);
}
}

View File

@ -6,6 +6,7 @@ use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="wohoo")
*/
class ToMany
{
@ -25,6 +26,7 @@ use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="wohoo")
*/
class ToMany
{
@ -34,7 +36,7 @@ class ToMany
private $amenity;
/**
* @ORM\ManyToMany(targetEntity="Rector\Doctrine\Tests\Rector\Class_\AddUuidMirrorForRelationPropertyRector\Source\AnotherEntity", cascade={"persist", "merge"})
* @ORM\JoinTable (name="tomany_uuid_anotherentity_uuid", joinColumns={@ORM\JoinColumn(referencedColumnName="uuid")}, inverseJoinColumns={@ORM\JoinColumn(referencedColumnName="uuid")})
* @ORM\JoinTable (name="wohoo_anotherentity_uuid", joinColumns={@ORM\JoinColumn(referencedColumnName="uuid")}, inverseJoinColumns={@ORM\JoinColumn(referencedColumnName="uuid")})
*/
private $amenityUuid;
}

View File

@ -14,7 +14,9 @@ final class AddUuidToEntityWhereMissingRectorTest extends AbstractRectorTestCase
__DIR__ . '/Fixture/already_has_constructor.php.inc',
__DIR__ . '/Fixture/process_string_id.php.inc',
__DIR__ . '/Fixture/with_parent_constructor.php.inc',
// skip
__DIR__ . '/Fixture/add_single_table_inheritance.php.inc',
__DIR__ . '/Fixture/add_single_table_inheritance_with_identifier.php.inc',
// skip
__DIR__ . '/Fixture/skip_id_with_uuid_type.php.inc',
__DIR__ . '/Fixture/skip_id_with_uuid_binary_type.php.inc',
]);

View File

@ -0,0 +1,68 @@
<?php
namespace Rector\Doctrine\Tests\Rector\Class_\AddUuidToEntityWhereMissingRector\Fixture;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\InheritanceType(value="SINGLE_TABLE")
*/
abstract class ParentSingleTableInheritance
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
}
/**
* @ORM\Entity
*/
class AddSingleTableInheritance extends ParentSingleTableInheritance
{
}
?>
-----
<?php
namespace Rector\Doctrine\Tests\Rector\Class_\AddUuidToEntityWhereMissingRector\Fixture;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\InheritanceType(value="SINGLE_TABLE")
*/
abstract class ParentSingleTableInheritance
{
public function __construct()
{
$this->uuid = \Ramsey\Uuid\Uuid::uuid4();
}
/**
* @var \Ramsey\Uuid\UuidInterface
* @ORM\Column (type="uuid_binary", unique=true, nullable=true)
*/
private $uuid;
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
}
/**
* @ORM\Entity
*/
class AddSingleTableInheritance extends ParentSingleTableInheritance
{
}
?>

View File

@ -0,0 +1,68 @@
<?php
namespace Rector\Doctrine\Tests\Rector\Class_\AddUuidToEntityWhereMissingRector\Fixture;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\InheritanceType(value="SINGLE_TABLE")
*/
abstract class ParentSingleTableInheritanceWithIdentifier
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $identifier;
}
/**
* @ORM\Entity
*/
class AddSingleTableInheritanceAgain extends ParentSingleTableInheritanceWithIdentifier
{
}
?>
-----
<?php
namespace Rector\Doctrine\Tests\Rector\Class_\AddUuidToEntityWhereMissingRector\Fixture;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\InheritanceType(value="SINGLE_TABLE")
*/
abstract class ParentSingleTableInheritanceWithIdentifier
{
public function __construct()
{
$this->uuid = \Ramsey\Uuid\Uuid::uuid4();
}
/**
* @var \Ramsey\Uuid\UuidInterface
* @ORM\Column (type="uuid_binary", unique=true, nullable=true)
*/
private $uuid;
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $identifier;
}
/**
* @ORM\Entity
*/
class AddSingleTableInheritanceAgain extends ParentSingleTableInheritanceWithIdentifier
{
}
?>

View File

@ -6,6 +6,11 @@ use Rector\DoctrinePhpDocParser\Ast\PhpDoc\AbstractDoctrineTagValueNode;
final class EntityTagValueNode extends AbstractDoctrineTagValueNode
{
/**
* @var string
*/
public const SHORT_NAME = '@ORM\Entity';
/**
* @var string|null
*/

View File

@ -0,0 +1,93 @@
<?php declare(strict_types=1);
namespace Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_;
use Rector\DoctrinePhpDocParser\Array_\ArrayItemStaticHelper;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\AbstractDoctrineTagValueNode;
final class TableTagValueNode extends AbstractDoctrineTagValueNode
{
/**
* @var string
*/
public const SHORT_NAME = '@ORM\Table';
/**
* @var string|null
*/
private $name;
/**
* @var string|null
*/
private $schema;
/**
* @var mixed[]|null
*/
private $indexes;
/**
* @var mixed[]|null
*/
private $uniqueConstaints;
/**
* @var mixed[]
*/
private $options = [];
/**
* @param mixed[] $options
*/
public function __construct(
?string $name,
?string $schema,
?array $indexes,
?array $uniqueConstaints,
array $options,
?string $originalContent = null
) {
$this->name = $name;
$this->schema = $schema;
$this->indexes = $indexes;
$this->uniqueConstaints = $uniqueConstaints;
$this->options = $options;
if ($originalContent !== null) {
$this->orderedVisibleItems = ArrayItemStaticHelper::resolveAnnotationItemsOrder($originalContent);
}
}
public function __toString(): string
{
$contentItems = [];
if ($this->name !== null) {
$contentItems['name'] = sprintf('name="%s"', $this->name);
}
if ($this->schema !== null) {
$contentItems['schema'] = sprintf('schema="%s"', $this->schema);
}
if ($this->indexes) {
$contentItems['indexes'] = $this->printArrayItem($this->indexes, 'indexes');
}
if ($this->uniqueConstaints) {
$contentItems['uniqueConstaints'] = $this->printArrayItem($this->uniqueConstaints, 'uniqueConstaints');
}
if ($this->options !== []) {
$contentItems['options'] = $this->printArrayItem($this->options, 'options');
}
return $this->printContentItems($contentItems);
}
public function getName(): ?string
{
return $this->name;
}
}

View File

@ -10,6 +10,7 @@ use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\Mapping\Table;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
@ -28,6 +29,7 @@ use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\ManyToManyTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\ManyToOneTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\OneToManyTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\OneToOneTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\TableTagValueNode;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineTagNodeInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
@ -80,9 +82,13 @@ final class OrmTagParser
// Entity tags
if ($node instanceof Class_) {
if ($tag === '@ORM\Entity') {
if ($tag === EntityTagValueNode::SHORT_NAME) {
return $this->createEntityTagValueNode($node, $annotationContent);
}
if ($tag === TableTagValueNode::SHORT_NAME) {
return $this->createTableTagValueNode($node, $annotationContent);
}
}
// Property tags
@ -101,6 +107,10 @@ final class OrmTagParser
Property $property,
string $annotationContent
): ?DoctrineTagNodeInterface {
if ($tag === IdTagValueNode::SHORT_NAME) {
return $this->createIdTagValueNode();
}
if ($tag === ColumnTagValueNode::SHORT_NAME) {
return $this->createColumnTagValueNode($property, $annotationContent);
}
@ -129,10 +139,6 @@ final class OrmTagParser
return $this->createJoinTableTagValeNode($property, $annotationContent);
}
if ($tag === IdTagValueNode::SHORT_NAME) {
return $this->createIdTagValueNode();
}
return null;
}
@ -146,6 +152,21 @@ final class OrmTagParser
));
}
private function createTableTagValueNode(Class_ $node, string $annotationContent): TableTagValueNode
{
/** @var Table $table */
$table = $this->nodeAnnotationReader->readDoctrineClassAnnotation($node, Table::class);
return new TableTagValueNode(
$table->name,
$table->schema,
$table->indexes,
$table->uniqueConstraints,
$table->options,
$annotationContent
);
}
private function createColumnTagValueNode(Property $property, string $annotationContent): ColumnTagValueNode
{
/** @var Column $column */