2020-04-24 22:57:13 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php80\Rector\Class_;
|
2020-04-24 22:57:13 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\AttributeGroup;
|
|
|
|
use PhpParser\Node\Expr\ArrowFunction;
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Function_;
|
2022-09-27 14:38:17 +00:00
|
|
|
use PhpParser\Node\Stmt\Interface_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Property;
|
2022-08-20 20:49:07 +00:00
|
|
|
use PhpParser\Node\Stmt\Use_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Node as DocNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
2023-09-20 12:41:26 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
2023-09-11 09:08:21 +00:00
|
|
|
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
2024-02-04 23:14:35 +00:00
|
|
|
use Rector\Exception\Configuration\InvalidConfigurationException;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Naming\Naming\UseImportsResolver;
|
2022-09-15 14:08:37 +00:00
|
|
|
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Php80\NodeFactory\AttrGroupsFactory;
|
|
|
|
use Rector\Php80\NodeManipulator\AttributeGroupNamedArgumentManipulator;
|
|
|
|
use Rector\Php80\ValueObject\AnnotationToAttribute;
|
|
|
|
use Rector\Php80\ValueObject\DoctrineTagAndAnnotationToAttribute;
|
|
|
|
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
|
2022-08-29 20:49:28 +00:00
|
|
|
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-04-01 16:51:34 +00:00
|
|
|
use RectorPrefix202404\Webmozart\Assert\Assert;
|
2020-04-24 22:57:13 +00:00
|
|
|
/**
|
2021-06-14 17:05:09 +00:00
|
|
|
* @changelog https://wiki.php.net/rfc/attributes_v2
|
2023-08-09 19:20:00 +00:00
|
|
|
* @changelog https://wiki.php.net/rfc/new_in_initializers
|
2020-04-24 22:57:13 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\AnnotationToAttributeRectorTest
|
2023-08-09 19:20:00 +00:00
|
|
|
* @see \Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Php81NestedAttributesRectorTest
|
2020-04-24 22:57:13 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class AnnotationToAttributeRector extends AbstractRector implements ConfigurableRectorInterface, MinPhpVersionInterface
|
2020-04-24 22:57:13 +00:00
|
|
|
{
|
2021-03-20 23:16:21 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-05-28 00:27:30 +00:00
|
|
|
* @var \Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory
|
2021-03-20 23:16:21 +00:00
|
|
|
*/
|
|
|
|
private $phpAttributeGroupFactory;
|
2021-07-08 12:36:20 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-07-08 12:36:20 +00:00
|
|
|
* @var \Rector\Php80\NodeFactory\AttrGroupsFactory
|
|
|
|
*/
|
|
|
|
private $attrGroupsFactory;
|
2021-10-13 19:26:54 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-11-18 21:01:01 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover
|
2021-10-13 19:26:54 +00:00
|
|
|
*/
|
2021-11-18 21:01:01 +00:00
|
|
|
private $phpDocTagRemover;
|
2022-04-23 22:28:13 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-04-23 22:28:13 +00:00
|
|
|
* @var \Rector\Php80\NodeManipulator\AttributeGroupNamedArgumentManipulator
|
|
|
|
*/
|
|
|
|
private $attributeGroupNamedArgumentManipulator;
|
2022-05-28 00:27:30 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-05-28 00:27:30 +00:00
|
|
|
* @var \Rector\Naming\Naming\UseImportsResolver
|
|
|
|
*/
|
|
|
|
private $useImportsResolver;
|
2022-09-15 14:08:37 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-09-15 14:08:37 +00:00
|
|
|
* @var \Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer
|
|
|
|
*/
|
|
|
|
private $phpAttributeAnalyzer;
|
2023-09-11 09:08:21 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Comments\NodeDocBlock\DocBlockUpdater
|
|
|
|
*/
|
|
|
|
private $docBlockUpdater;
|
2023-09-20 12:41:26 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
|
|
|
*/
|
|
|
|
private $phpDocInfoFactory;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var AnnotationToAttribute[]
|
|
|
|
*/
|
|
|
|
private $annotationsToAttributes = [];
|
2023-09-20 12:41:26 +00:00
|
|
|
public function __construct(PhpAttributeGroupFactory $phpAttributeGroupFactory, AttrGroupsFactory $attrGroupsFactory, PhpDocTagRemover $phpDocTagRemover, AttributeGroupNamedArgumentManipulator $attributeGroupNamedArgumentManipulator, UseImportsResolver $useImportsResolver, PhpAttributeAnalyzer $phpAttributeAnalyzer, DocBlockUpdater $docBlockUpdater, PhpDocInfoFactory $phpDocInfoFactory)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-03-20 23:16:21 +00:00
|
|
|
$this->phpAttributeGroupFactory = $phpAttributeGroupFactory;
|
2021-07-08 12:36:20 +00:00
|
|
|
$this->attrGroupsFactory = $attrGroupsFactory;
|
2021-11-18 21:01:01 +00:00
|
|
|
$this->phpDocTagRemover = $phpDocTagRemover;
|
2022-04-23 22:28:13 +00:00
|
|
|
$this->attributeGroupNamedArgumentManipulator = $attributeGroupNamedArgumentManipulator;
|
2022-05-28 00:27:30 +00:00
|
|
|
$this->useImportsResolver = $useImportsResolver;
|
2022-09-15 14:08:37 +00:00
|
|
|
$this->phpAttributeAnalyzer = $phpAttributeAnalyzer;
|
2023-09-11 09:08:21 +00:00
|
|
|
$this->docBlockUpdater = $docBlockUpdater;
|
2023-09-20 12:41:26 +00:00
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
2020-04-24 22:57:13 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-04-24 22:57:13 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change annotation to attribute', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2021-02-28 07:47:48 +00:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2020-04-24 22:57:13 +00:00
|
|
|
|
2021-02-28 07:47:48 +00:00
|
|
|
class SymfonyRoute
|
2020-04-24 22:57:13 +00:00
|
|
|
{
|
2021-02-28 07:47:48 +00:00
|
|
|
/**
|
2024-02-21 15:17:39 +00:00
|
|
|
* @Route("/path", name="action") api route
|
2021-02-28 07:47:48 +00:00
|
|
|
*/
|
|
|
|
public function action()
|
|
|
|
{
|
|
|
|
}
|
2020-04-24 22:57:13 +00:00
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-02-28 07:47:48 +00:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2020-04-24 22:57:13 +00:00
|
|
|
|
2021-02-28 07:47:48 +00:00
|
|
|
class SymfonyRoute
|
2020-04-24 22:57:13 +00:00
|
|
|
{
|
2024-02-21 15:17:39 +00:00
|
|
|
#[Route(path: '/path', name: 'action')] // api route
|
2021-02-28 07:47:48 +00:00
|
|
|
public function action()
|
|
|
|
{
|
|
|
|
}
|
2020-04-24 22:57:13 +00:00
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2022-06-07 09:46:15 +00:00
|
|
|
, [new AnnotationToAttribute('Symfony\\Component\\Routing\\Annotation\\Route')])]);
|
2020-04-24 22:57:13 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-04-24 22:57:13 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-04-24 22:57:13 +00:00
|
|
|
{
|
2022-09-27 14:38:17 +00:00
|
|
|
return [Class_::class, Property::class, Param::class, ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class, Interface_::class];
|
2020-04-24 22:57:13 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-09-27 14:38:17 +00:00
|
|
|
* @param Class_|Property|Param|ClassMethod|Function_|Closure|ArrowFunction|Interface_ $node
|
2020-04-24 22:57:13 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-04-24 22:57:13 +00:00
|
|
|
{
|
2023-08-28 12:45:54 +00:00
|
|
|
if ($this->annotationsToAttributes === []) {
|
2024-02-04 21:53:39 +00:00
|
|
|
throw new InvalidConfigurationException(\sprintf('The "%s" rule requires configuration.', self::class));
|
2023-08-28 12:45:54 +00:00
|
|
|
}
|
2021-03-20 23:16:21 +00:00
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$phpDocInfo instanceof PhpDocInfo) {
|
2021-03-20 23:16:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-27 17:32:34 +00:00
|
|
|
$uses = $this->useImportsResolver->resolveBareUses();
|
2024-02-13 04:45:07 +00:00
|
|
|
// 1. bare tags without annotation class, e.g. "@require"
|
2021-07-08 16:33:18 +00:00
|
|
|
$genericAttributeGroups = $this->processGenericTags($phpDocInfo);
|
2021-06-30 21:52:31 +00:00
|
|
|
// 2. Doctrine annotation classes
|
2022-05-28 00:27:30 +00:00
|
|
|
$annotationAttributeGroups = $this->processDoctrineAnnotationClasses($phpDocInfo, $uses);
|
2021-07-08 16:33:18 +00:00
|
|
|
$attributeGroups = \array_merge($genericAttributeGroups, $annotationAttributeGroups);
|
|
|
|
if ($attributeGroups === []) {
|
|
|
|
return null;
|
2021-03-20 23:16:21 +00:00
|
|
|
}
|
2023-09-11 09:08:21 +00:00
|
|
|
// 3. Reprint docblock
|
|
|
|
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
|
2022-08-24 11:51:48 +00:00
|
|
|
$this->attributeGroupNamedArgumentManipulator->decorate($attributeGroups);
|
2021-07-08 16:33:18 +00:00
|
|
|
$node->attrGroups = \array_merge($node->attrGroups, $attributeGroups);
|
|
|
|
return $node;
|
2021-03-20 23:16:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-11-28 17:01:20 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-03-20 23:16:21 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2021-03-20 23:16:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allIsAOf($configuration, AnnotationToAttribute::class);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->annotationsToAttributes = $configuration;
|
2020-04-24 22:57:13 +00:00
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::ATTRIBUTES;
|
2021-07-21 09:35:57 +00:00
|
|
|
}
|
2021-05-28 12:08:33 +00:00
|
|
|
/**
|
2021-07-08 16:33:18 +00:00
|
|
|
* @return AttributeGroup[]
|
2021-05-28 12:08:33 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processGenericTags(PhpDocInfo $phpDocInfo) : array
|
2021-05-28 12:08:33 +00:00
|
|
|
{
|
2021-07-08 16:33:18 +00:00
|
|
|
$attributeGroups = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
$phpDocNodeTraverser = new PhpDocNodeTraverser();
|
2023-09-11 10:41:08 +00:00
|
|
|
$phpDocNodeTraverser->traverseWithCallable($phpDocInfo->getPhpDocNode(), '', function (DocNode $docNode) use(&$attributeGroups) : ?int {
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$docNode instanceof PhpDocTagNode) {
|
2021-07-08 16:33:18 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$docNode->value instanceof GenericTagValueNode) {
|
2021-07-08 16:33:18 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$tag = \trim($docNode->name, '@');
|
|
|
|
// not a basic one
|
|
|
|
if (\strpos($tag, '\\') !== \false) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-28 12:08:33 +00:00
|
|
|
foreach ($this->annotationsToAttributes as $annotationToAttribute) {
|
2021-06-30 21:52:31 +00:00
|
|
|
$desiredTag = $annotationToAttribute->getTag();
|
2021-07-08 16:33:18 +00:00
|
|
|
if ($desiredTag !== $tag) {
|
2021-06-30 21:52:31 +00:00
|
|
|
continue;
|
2021-05-28 12:08:33 +00:00
|
|
|
}
|
2021-07-08 16:33:18 +00:00
|
|
|
$attributeGroups[] = $this->phpAttributeGroupFactory->createFromSimpleTag($annotationToAttribute);
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpDocNodeTraverser::NODE_REMOVE;
|
2021-05-28 12:08:33 +00:00
|
|
|
}
|
2021-07-08 16:33:18 +00:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
return $attributeGroups;
|
2021-06-22 16:12:07 +00:00
|
|
|
}
|
2021-06-30 21:52:31 +00:00
|
|
|
/**
|
2022-08-20 20:49:07 +00:00
|
|
|
* @param Use_[] $uses
|
2021-07-08 16:33:18 +00:00
|
|
|
* @return AttributeGroup[]
|
2021-06-30 21:52:31 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo, array $uses) : array
|
2021-05-28 12:08:33 +00:00
|
|
|
{
|
2021-11-18 19:27:39 +00:00
|
|
|
if ($phpDocInfo->getPhpDocNode()->children === []) {
|
|
|
|
return [];
|
|
|
|
}
|
2021-07-01 23:51:20 +00:00
|
|
|
$doctrineTagAndAnnotationToAttributes = [];
|
2022-09-15 14:08:37 +00:00
|
|
|
$doctrineTagValueNodes = [];
|
2021-11-18 21:01:01 +00:00
|
|
|
foreach ($phpDocInfo->getPhpDocNode()->children as $phpDocChildNode) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$phpDocChildNode instanceof PhpDocTagNode) {
|
2021-11-18 21:01:01 +00:00
|
|
|
continue;
|
2021-07-01 23:51:20 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$phpDocChildNode->value instanceof DoctrineAnnotationTagValueNode) {
|
2021-11-18 21:01:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-11-20 10:45:53 +00:00
|
|
|
$doctrineTagValueNode = $phpDocChildNode->value;
|
|
|
|
$annotationToAttribute = $this->matchAnnotationToAttribute($doctrineTagValueNode);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$annotationToAttribute instanceof AnnotationToAttribute) {
|
2021-11-18 21:01:01 +00:00
|
|
|
continue;
|
2021-07-08 16:33:18 +00:00
|
|
|
}
|
2022-08-18 17:55:50 +00:00
|
|
|
$doctrineTagAndAnnotationToAttributes[] = new DoctrineTagAndAnnotationToAttribute($doctrineTagValueNode, $annotationToAttribute);
|
2022-09-15 14:08:37 +00:00
|
|
|
$doctrineTagValueNodes[] = $doctrineTagValueNode;
|
|
|
|
}
|
|
|
|
$attributeGroups = $this->attrGroupsFactory->create($doctrineTagAndAnnotationToAttributes, $uses);
|
|
|
|
if ($this->phpAttributeAnalyzer->hasRemoveArrayState($attributeGroups)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
foreach ($doctrineTagValueNodes as $doctrineTagValueNode) {
|
2021-11-20 10:45:53 +00:00
|
|
|
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $doctrineTagValueNode);
|
2021-10-29 11:12:52 +00:00
|
|
|
}
|
2022-09-15 14:08:37 +00:00
|
|
|
return $attributeGroups;
|
2021-10-13 19:26:54 +00:00
|
|
|
}
|
2022-08-11 14:29:35 +00:00
|
|
|
private function matchAnnotationToAttribute(DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode) : ?\Rector\Php80\ValueObject\AnnotationToAttribute
|
2021-11-20 10:45:53 +00:00
|
|
|
{
|
|
|
|
foreach ($this->annotationsToAttributes as $annotationToAttribute) {
|
|
|
|
if (!$doctrineAnnotationTagValueNode->hasClassName($annotationToAttribute->getTag())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return $annotationToAttribute;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2020-04-24 22:57:13 +00:00
|
|
|
}
|