From bc9f3759cd673480240f700311c3542ca0b35d38 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 29 Aug 2019 16:31:27 +0200 Subject: [PATCH] improve new entity props collecting --- .../src/AbstarctRector/DoctrineTrait.php | 6 ++++ .../EntitiesWithAddedPropertyCollector.php | 24 ------------- .../EntityWithAddedPropertyCollector.php | 34 +++++++++++++++++++ ...tiesWithAddedPropertiesFinishExtension.php | 29 ++++++++-------- ...AddUuidMirrorForRelationPropertyRector.php | 34 ++++++++++++++++--- .../AddUuidToEntityWhereMissingRector.php | 12 +++---- 6 files changed, 90 insertions(+), 49 deletions(-) delete mode 100644 packages/Doctrine/src/Collector/EntitiesWithAddedPropertyCollector.php create mode 100644 packages/Doctrine/src/Collector/EntityWithAddedPropertyCollector.php diff --git a/packages/Doctrine/src/AbstarctRector/DoctrineTrait.php b/packages/Doctrine/src/AbstarctRector/DoctrineTrait.php index cf097841b36..d2d02ec40bb 100644 --- a/packages/Doctrine/src/AbstarctRector/DoctrineTrait.php +++ b/packages/Doctrine/src/AbstarctRector/DoctrineTrait.php @@ -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 { @@ -49,4 +50,9 @@ trait DoctrineTrait { return $this->doctrineDocBlockResolver->getTargetEntity($property); } + + protected function getDoctrineRelationTagValueNode(Property $property): ?DoctrineRelationTagValueNodeInterface + { + return $this->doctrineDocBlockResolver->getDoctrineRelationTagValueNode($property); + } } diff --git a/packages/Doctrine/src/Collector/EntitiesWithAddedPropertyCollector.php b/packages/Doctrine/src/Collector/EntitiesWithAddedPropertyCollector.php deleted file mode 100644 index 8b4ad07d1f1..00000000000 --- a/packages/Doctrine/src/Collector/EntitiesWithAddedPropertyCollector.php +++ /dev/null @@ -1,24 +0,0 @@ -propertiesByClasses[$class][] = $property; - } - - /** - * @return string[][] - */ - public function getPropertiesByClasses(): array - { - return $this->propertiesByClasses; - } -} diff --git a/packages/Doctrine/src/Collector/EntityWithAddedPropertyCollector.php b/packages/Doctrine/src/Collector/EntityWithAddedPropertyCollector.php new file mode 100644 index 00000000000..95285f336d0 --- /dev/null +++ b/packages/Doctrine/src/Collector/EntityWithAddedPropertyCollector.php @@ -0,0 +1,34 @@ +propertiesByClass[$class]['properties'][] = $property; + } + + public function addClassToManyRelationProperty(string $class, string $property): void + { + $this->propertiesByClass[$class]['to_many_relations'][] = $property; + } + + public function addClassToOneRelationProperty(string $class, string $property): void + { + $this->propertiesByClass[$class]['to_one_relations'][] = $property; + } + + /** + * @return string[][][] + */ + public function getPropertiesByClass(): array + { + return $this->propertiesByClass; + } +} diff --git a/packages/Doctrine/src/Extension/ReportEntitiesWithAddedPropertiesFinishExtension.php b/packages/Doctrine/src/Extension/ReportEntitiesWithAddedPropertiesFinishExtension.php index 229886bd78c..66f06d1ce7e 100644 --- a/packages/Doctrine/src/Extension/ReportEntitiesWithAddedPropertiesFinishExtension.php +++ b/packages/Doctrine/src/Extension/ReportEntitiesWithAddedPropertiesFinishExtension.php @@ -2,16 +2,17 @@ namespace Rector\Doctrine\Extension; +use Nette\Utils\Json; use Rector\Contract\Extension\FinishingExtensionInterface; -use Rector\Doctrine\Collector\EntitiesWithAddedPropertyCollector; +use Rector\Doctrine\Collector\EntityWithAddedPropertyCollector; use Symfony\Component\Console\Style\SymfonyStyle; final class ReportEntitiesWithAddedPropertiesFinishExtension implements FinishingExtensionInterface { /** - * @var EntitiesWithAddedPropertyCollector + * @var EntityWithAddedPropertyCollector */ - private $entitiesWithAddedPropertyCollector; + private $entityWithAddedPropertyCollector; /** * @var SymfonyStyle @@ -19,27 +20,27 @@ final class ReportEntitiesWithAddedPropertiesFinishExtension implements Finishin private $symfonyStyle; public function __construct( - EntitiesWithAddedPropertyCollector $entitiesWithAddedPropertyCollector, + EntityWithAddedPropertyCollector $entityWithAddedPropertyCollector, SymfonyStyle $symfonyStyle ) { - $this->entitiesWithAddedPropertyCollector = $entitiesWithAddedPropertyCollector; + $this->entityWithAddedPropertyCollector = $entityWithAddedPropertyCollector; $this->symfonyStyle = $symfonyStyle; } public function run(): void { - $classes = $this->entitiesWithAddedPropertyCollector->getPropertiesByClasses(); - - if ($classes === []) { + $propertiesByClass = $this->entityWithAddedPropertyCollector->getPropertiesByClass(); + if ($propertiesByClass === []) { return; } - $this->symfonyStyle->title('Entities with new properties'); + $data = [ + 'title' => 'Entities with new properties', + 'added_properties_by_class' => $propertiesByClass, + ]; - foreach ($this->entitiesWithAddedPropertyCollector->getPropertiesByClasses() as $class => $properties) { - $this->symfonyStyle->section($class); - $this->symfonyStyle->listing($properties); - $this->symfonyStyle->newLine(1); - } + $jsonContent = Json::encode($data, Json::PRETTY); + + $this->symfonyStyle->writeln($jsonContent); } } diff --git a/packages/Doctrine/src/Rector/Class_/AddUuidMirrorForRelationPropertyRector.php b/packages/Doctrine/src/Rector/Class_/AddUuidMirrorForRelationPropertyRector.php index f4583a2e0b5..054bc91d7c7 100644 --- a/packages/Doctrine/src/Rector/Class_/AddUuidMirrorForRelationPropertyRector.php +++ b/packages/Doctrine/src/Rector/Class_/AddUuidMirrorForRelationPropertyRector.php @@ -10,10 +10,12 @@ use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\PropertyProperty; use PhpParser\Node\VarLikeIdentifier; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; +use Rector\Doctrine\Collector\EntityWithAddedPropertyCollector; use Rector\Doctrine\PhpDocParser\Ast\PhpDoc\PhpDocTagNodeFactory; 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 +35,19 @@ final class AddUuidMirrorForRelationPropertyRector extends AbstractRector */ private $phpDocTagNodeFactory; + /** + * @var EntityWithAddedPropertyCollector + */ + private $entityWithAddedPropertyCollector; + public function __construct( DocBlockManipulator $docBlockManipulator, - PhpDocTagNodeFactory $phpDocTagNodeFactory + PhpDocTagNodeFactory $phpDocTagNodeFactory, + EntityWithAddedPropertyCollector $entityWithAddedPropertyCollector ) { $this->docBlockManipulator = $docBlockManipulator; $this->phpDocTagNodeFactory = $phpDocTagNodeFactory; + $this->entityWithAddedPropertyCollector = $entityWithAddedPropertyCollector; } public function getDefinition(): RectorDefinition @@ -99,18 +108,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); @@ -189,4 +198,19 @@ 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); + + if ($doctrineRelationTagValueNode instanceof ToManyTagNodeInterface) { + $this->entityWithAddedPropertyCollector->addClassToManyRelationProperty($className, $propertyName); + } elseif ($doctrineRelationTagValueNode instanceof ToOneTagNodeInterface) { + $this->entityWithAddedPropertyCollector->addClassToOneRelationProperty($className, $propertyName); + } + } } diff --git a/packages/Doctrine/src/Rector/Class_/AddUuidToEntityWhereMissingRector.php b/packages/Doctrine/src/Rector/Class_/AddUuidToEntityWhereMissingRector.php index c7e145ac3b6..05752ec0bbc 100644 --- a/packages/Doctrine/src/Rector/Class_/AddUuidToEntityWhereMissingRector.php +++ b/packages/Doctrine/src/Rector/Class_/AddUuidToEntityWhereMissingRector.php @@ -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\EntitiesWithAddedPropertyCollector; +use Rector\Doctrine\Collector\EntityWithAddedPropertyCollector; 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 EntitiesWithAddedPropertyCollector + * @var EntityWithAddedPropertyCollector */ - private $entitiesWithAddedPropertyCollector; + private $entityWithAddedPropertyCollector; public function __construct( EntityUuidNodeFactory $entityUuidNodeFactory, ClassManipulator $classManipulator, - EntitiesWithAddedPropertyCollector $entitiesWithAddedPropertyCollector + EntityWithAddedPropertyCollector $entityWithAddedPropertyCollector ) { $this->entityUuidNodeFactory = $entityUuidNodeFactory; $this->classManipulator = $classManipulator; - $this->entitiesWithAddedPropertyCollector = $entitiesWithAddedPropertyCollector; + $this->entityWithAddedPropertyCollector = $entityWithAddedPropertyCollector; } public function getDefinition(): RectorDefinition @@ -95,7 +95,7 @@ final class AddUuidToEntityWhereMissingRector extends AbstractRector /** @var string $class */ $class = $this->getName($node); - $this->entitiesWithAddedPropertyCollector->addClassAndProperty($class, 'uuid'); + $this->entityWithAddedPropertyCollector->addClassAndProperty($class, 'uuid'); return $node; }