Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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
Expand Down Expand Up @@ -181,6 +182,11 @@ public function getDoctrineIdTagValueNode(): ?IdTagValueNode
return $this->matchChildValueNodeOfType(IdTagValueNode::class);
}

public function getDoctrineTableTagValueNode(): ?TableTagValueNode
{
return $this->matchChildValueNodeOfType(TableTagValueNode::class);
}

public function getDoctrineManyToManyTagValueNode(): ?ManyToManyTagValueNode
{
return $this->matchChildValueNodeOfType(ManyToManyTagValueNode::class);
Expand Down
25 changes: 25 additions & 0 deletions packages/Doctrine/src/AbstarctRector/DoctrineTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\Doctrine\PhpDocParser\DoctrineDocBlockResolver;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineRelationTagValueNodeInterface;

trait DoctrineTrait
{
Expand All @@ -26,8 +27,32 @@ protected function isDoctrineEntityClass(Class_ $class): bool
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);
}
}

This file was deleted.

37 changes: 37 additions & 0 deletions packages/Doctrine/src/Collector/UuidMigrationDataCollector.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

This file was deleted.

21 changes: 21 additions & 0 deletions packages/Doctrine/src/PhpDocParser/DoctrineDocBlockResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;

Expand Down Expand Up @@ -41,6 +42,16 @@ public function getTargetEntity(Property $property): ?string
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);
Expand All @@ -51,6 +62,16 @@ public function getDoctrineRelationTagValueNode(Property $property): ?DoctrineRe
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
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;
Expand All @@ -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
Expand Down Expand Up @@ -99,18 +116,18 @@ private function createMirrorNullable(Property $property): Property
$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);
Expand All @@ -130,9 +147,8 @@ private function refactorToManyPropertyPhpDocInfo(PhpDocInfo $propertyPhpDocInfo
$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
Expand Down Expand Up @@ -189,4 +205,25 @@ private function shouldSkipProperty(Class_ $class, Property $property): bool

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);
}
}
}
Loading