2022-08-18 17:55:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
|
|
|
namespace Rector\Php80\Rector\Property;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\AttributeGroup;
|
2022-10-15 22:34:17 +00:00
|
|
|
use PhpParser\Node\Param;
|
2022-08-18 18:39:25 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2022-08-18 17:55:50 +00:00
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
|
|
|
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
|
|
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\ValueObject\PhpVersion;
|
|
|
|
use Rector\Naming\Naming\UseImportsResolver;
|
2022-10-15 22:31:48 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2022-08-18 17:55:50 +00:00
|
|
|
use Rector\Php80\NodeFactory\NestedAttrGroupsFactory;
|
2022-10-15 22:31:48 +00:00
|
|
|
use Rector\Php80\ValueObject\AnnotationPropertyToAttributeClass;
|
2022-08-18 17:55:50 +00:00
|
|
|
use Rector\Php80\ValueObject\NestedAnnotationToAttribute;
|
|
|
|
use Rector\Php80\ValueObject\NestedDoctrineTagAndAnnotationToAttribute;
|
2022-10-15 22:31:48 +00:00
|
|
|
use Rector\PostRector\Collector\UseNodesToAddCollector;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
2022-08-18 17:55:50 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2023-09-01 11:10:35 +00:00
|
|
|
use RectorPrefix202309\Webmozart\Assert\Assert;
|
2022-08-18 17:55:50 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\Php80\Rector\Property\NestedAnnotationToAttributeRector\NestedAnnotationToAttributeRectorTest
|
|
|
|
*
|
|
|
|
* @changelog https://www.doctrine-project.org/projects/doctrine-orm/en/2.13/reference/attributes-reference.html#joincolumn-inversejoincolumn
|
|
|
|
*/
|
|
|
|
final class NestedAnnotationToAttributeRector extends AbstractRector implements ConfigurableRectorInterface, MinPhpVersionInterface
|
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-08-18 17:55:50 +00:00
|
|
|
* @var \Rector\Naming\Naming\UseImportsResolver
|
|
|
|
*/
|
|
|
|
private $useImportsResolver;
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-08-18 17:55:50 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover
|
|
|
|
*/
|
|
|
|
private $phpDocTagRemover;
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-08-18 17:55:50 +00:00
|
|
|
* @var \Rector\Php80\NodeFactory\NestedAttrGroupsFactory
|
|
|
|
*/
|
|
|
|
private $nestedAttrGroupsFactory;
|
2022-10-15 22:31:48 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-10-15 22:31:48 +00:00
|
|
|
* @var \Rector\PostRector\Collector\UseNodesToAddCollector
|
|
|
|
*/
|
|
|
|
private $useNodesToAddCollector;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var NestedAnnotationToAttribute[]
|
|
|
|
*/
|
|
|
|
private $nestedAnnotationsToAttributes = [];
|
2022-10-15 22:31:48 +00:00
|
|
|
public function __construct(UseImportsResolver $useImportsResolver, PhpDocTagRemover $phpDocTagRemover, NestedAttrGroupsFactory $nestedAttrGroupsFactory, UseNodesToAddCollector $useNodesToAddCollector)
|
2022-08-18 17:55:50 +00:00
|
|
|
{
|
|
|
|
$this->useImportsResolver = $useImportsResolver;
|
|
|
|
$this->phpDocTagRemover = $phpDocTagRemover;
|
|
|
|
$this->nestedAttrGroupsFactory = $nestedAttrGroupsFactory;
|
2022-10-15 22:31:48 +00:00
|
|
|
$this->useNodesToAddCollector = $useNodesToAddCollector;
|
2022-08-18 17:55:50 +00:00
|
|
|
}
|
|
|
|
public function getRuleDefinition() : RuleDefinition
|
|
|
|
{
|
|
|
|
return new RuleDefinition('Changed nested annotations to attributes', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
|
|
|
|
|
|
class SomeEntity
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @ORM\JoinTable(name="join_table_name",
|
|
|
|
* joinColumns={@ORM\JoinColumn(name="origin_id")},
|
|
|
|
* inverseJoinColumns={@ORM\JoinColumn(name="target_id")}
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
private $collection;
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
|
|
|
|
|
|
class SomeEntity
|
|
|
|
{
|
|
|
|
#[ORM\JoinTable(name: 'join_table_name')]
|
|
|
|
#[ORM\JoinColumn(name: 'origin_id')]
|
|
|
|
#[ORM\InverseJoinColumn(name: 'target_id')]
|
|
|
|
private $collection;
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2022-10-15 22:31:48 +00:00
|
|
|
, [[new NestedAnnotationToAttribute('Doctrine\\ORM\\Mapping\\JoinTable', [new AnnotationPropertyToAttributeClass('Doctrine\\ORM\\Mapping\\JoinColumn', 'joinColumns'), new AnnotationPropertyToAttributeClass('Doctrine\\ORM\\Mapping\\InverseJoinColumn', 'inverseJoinColumns')])]])]);
|
2022-08-18 17:55:50 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-10-15 22:34:17 +00:00
|
|
|
return [Property::class, Class_::class, Param::class];
|
2022-08-18 17:55:50 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-10-15 22:34:17 +00:00
|
|
|
* @param Property|Class_|Param $node
|
2022-08-18 17:55:50 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node) : ?Node
|
|
|
|
{
|
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
|
|
|
|
if (!$phpDocInfo instanceof PhpDocInfo) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-27 17:32:34 +00:00
|
|
|
$uses = $this->useImportsResolver->resolveBareUses();
|
2022-08-18 17:55:50 +00:00
|
|
|
$attributeGroups = $this->transformDoctrineAnnotationClassesToAttributeGroups($phpDocInfo, $uses);
|
|
|
|
if ($attributeGroups === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-11-28 10:38:42 +00:00
|
|
|
$node->attrGroups = \array_merge($node->attrGroups, $attributeGroups);
|
2022-10-15 22:31:48 +00:00
|
|
|
$this->completeExtraUseImports($attributeGroups);
|
2022-08-18 17:55:50 +00:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param mixed[] $configuration
|
|
|
|
*/
|
|
|
|
public function configure(array $configuration) : void
|
|
|
|
{
|
|
|
|
Assert::allIsInstanceOf($configuration, NestedAnnotationToAttribute::class);
|
|
|
|
$this->nestedAnnotationsToAttributes = $configuration;
|
|
|
|
}
|
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
|
|
|
return PhpVersion::PHP_80;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Node\Stmt\Use_[] $uses
|
|
|
|
* @return AttributeGroup[]
|
|
|
|
*/
|
|
|
|
private function transformDoctrineAnnotationClassesToAttributeGroups(PhpDocInfo $phpDocInfo, array $uses) : array
|
|
|
|
{
|
|
|
|
if ($phpDocInfo->getPhpDocNode()->children === []) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$nestedDoctrineTagAndAnnotationToAttributes = [];
|
|
|
|
foreach ($phpDocInfo->getPhpDocNode()->children as $phpDocChildNode) {
|
|
|
|
if (!$phpDocChildNode instanceof PhpDocTagNode) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$phpDocChildNode->value instanceof DoctrineAnnotationTagValueNode) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$doctrineTagValueNode = $phpDocChildNode->value;
|
|
|
|
$nestedAnnotationToAttribute = $this->matchAnnotationToAttribute($doctrineTagValueNode);
|
|
|
|
if (!$nestedAnnotationToAttribute instanceof NestedAnnotationToAttribute) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $doctrineTagValueNode);
|
|
|
|
$nestedDoctrineTagAndAnnotationToAttributes[] = new NestedDoctrineTagAndAnnotationToAttribute($doctrineTagValueNode, $nestedAnnotationToAttribute);
|
|
|
|
}
|
|
|
|
return $this->nestedAttrGroupsFactory->create($nestedDoctrineTagAndAnnotationToAttributes, $uses);
|
|
|
|
}
|
|
|
|
private function matchAnnotationToAttribute(DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode) : ?\Rector\Php80\ValueObject\NestedAnnotationToAttribute
|
|
|
|
{
|
|
|
|
foreach ($this->nestedAnnotationsToAttributes as $nestedAnnotationToAttribute) {
|
|
|
|
$doctrineResolvedClass = $doctrineAnnotationTagValueNode->identifierTypeNode->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS);
|
|
|
|
if ($doctrineResolvedClass !== $nestedAnnotationToAttribute->getTag()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return $nestedAnnotationToAttribute;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-10-15 22:31:48 +00:00
|
|
|
/**
|
|
|
|
* @param AttributeGroup[] $attributeGroups
|
|
|
|
*/
|
|
|
|
private function completeExtraUseImports(array $attributeGroups) : void
|
|
|
|
{
|
|
|
|
foreach ($attributeGroups as $attributeGroup) {
|
|
|
|
foreach ($attributeGroup->attrs as $attr) {
|
|
|
|
$namespacedAttrName = $attr->name->getAttribute(AttributeKey::EXTRA_USE_IMPORT);
|
|
|
|
if (!\is_string($namespacedAttrName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->useNodesToAddCollector->addUseImport(new FullyQualifiedObjectType($namespacedAttrName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-18 17:55:50 +00:00
|
|
|
}
|