improve new entity props collecting

This commit is contained in:
Tomas Votruba 2019-08-29 16:31:27 +02:00
parent 0223736948
commit bc9f3759cd
6 changed files with 90 additions and 49 deletions

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
{
@ -49,4 +50,9 @@ trait DoctrineTrait
{
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 EntitiesWithAddedPropertyCollector
{
/**
* @var string[][]
*/
private $propertiesByClasses = [];
public function addClassAndProperty(string $class, string $property): void
{
$this->propertiesByClasses[$class][] = $property;
}
/**
* @return string[][]
*/
public function getPropertiesByClasses(): array
{
return $this->propertiesByClasses;
}
}

View File

@ -0,0 +1,34 @@
<?php declare(strict_types=1);
namespace Rector\Doctrine\Collector;
final class EntityWithAddedPropertyCollector
{
/**
* @var string[][][]
*/
private $propertiesByClass = [];
public function addClassAndProperty(string $class, string $property): void
{
$this->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;
}
}

View File

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

View File

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

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\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;
}