useImportsResolver = $useImportsResolver; $this->phpDocTagRemover = $phpDocTagRemover; $this->nestedAttrGroupsFactory = $nestedAttrGroupsFactory; $this->useNodesToAddCollector = $useNodesToAddCollector; $this->docBlockUpdater = $docBlockUpdater; $this->phpDocInfoFactory = $phpDocInfoFactory; } 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 , [[new NestedAnnotationToAttribute('Doctrine\\ORM\\Mapping\\JoinTable', [new AnnotationPropertyToAttributeClass('Doctrine\\ORM\\Mapping\\JoinColumn', 'joinColumns'), new AnnotationPropertyToAttributeClass('Doctrine\\ORM\\Mapping\\InverseJoinColumn', 'inverseJoinColumns')])]])]); } /** * @return array> */ public function getNodeTypes() : array { return [Property::class, Class_::class, Param::class]; } /** * @param Property|Class_|Param $node */ public function refactor(Node $node) : ?Node { $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); if (!$phpDocInfo instanceof PhpDocInfo) { return null; } $uses = $this->useImportsResolver->resolveBareUses(); $attributeGroups = $this->transformDoctrineAnnotationClassesToAttributeGroups($phpDocInfo, $uses); if ($attributeGroups === []) { return null; } // 3. Reprint docblock $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); $node->attrGroups = \array_merge($node->attrGroups, $attributeGroups); $this->completeExtraUseImports($attributeGroups); 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 { $doctrineResolvedClass = $doctrineAnnotationTagValueNode->identifierTypeNode->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS); foreach ($this->nestedAnnotationsToAttributes as $nestedAnnotationToAttribute) { foreach ($nestedAnnotationToAttribute->getAnnotationPropertiesToAttributeClasses() as $annotationClass) { if ($annotationClass->getAttributeClass() === $doctrineResolvedClass) { return $nestedAnnotationToAttribute; } } if ($doctrineResolvedClass !== $nestedAnnotationToAttribute->getTag()) { continue; } return $nestedAnnotationToAttribute; } return null; } /** * @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)); } } } }