rector/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php

159 lines
6.7 KiB
PHP
Raw Normal View History

2020-04-24 22:57:13 +00:00
<?php
declare (strict_types=1);
2020-04-24 22:57:13 +00:00
namespace Rector\Php80\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
2020-04-24 22:57:13 +00:00
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
2020-04-24 22:57:13 +00:00
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
2020-04-24 22:57:13 +00:00
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\PhpAttribute\Printer\PhpAttributeGroupFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix20210614\Webmozart\Assert\Assert;
2020-04-24 22:57:13 +00:00
/**
2021-04-10 18:47:17 +00:00
* @changelog https://wiki.php.net/rfc/attributes_v2 https://wiki.php.net/rfc/shorter_attribute_syntax https://wiki.php.net/rfc/shorter_attribute_syntax_change
2020-04-24 22:57:13 +00:00
*
* @see \Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\AnnotationToAttributeRectorTest
2020-04-24 22:57:13 +00:00
*/
final class AnnotationToAttributeRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
2020-04-24 22:57:13 +00:00
{
/**
* @var string
2020-04-24 22:57:13 +00:00
*/
public const ANNOTATION_TO_ATTRIBUTE = 'annotation_to_attribute';
/**
* @var AnnotationToAttribute[]
*/
private $annotationsToAttributes = [];
/**
* @var \Rector\PhpAttribute\Printer\PhpAttributeGroupFactory
*/
private $phpAttributeGroupFactory;
/**
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover
*/
private $phpDocTagRemover;
public function __construct(\Rector\PhpAttribute\Printer\PhpAttributeGroupFactory $phpAttributeGroupFactory, \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover $phpDocTagRemover)
{
$this->phpAttributeGroupFactory = $phpAttributeGroupFactory;
$this->phpDocTagRemover = $phpDocTagRemover;
2020-04-24 22:57:13 +00:00
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-04-24 22:57:13 +00:00
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change annotation to attribute', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
use Symfony\Component\Routing\Annotation\Route;
2020-04-24 22:57:13 +00:00
class SymfonyRoute
2020-04-24 22:57:13 +00:00
{
/**
* @Route("/path", name="action")
*/
public function action()
{
}
2020-04-24 22:57:13 +00:00
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
use Symfony\Component\Routing\Annotation\Route;
2020-04-24 22:57:13 +00:00
class SymfonyRoute
2020-04-24 22:57:13 +00:00
{
#[Route(path: '/path', name: 'action')]
public function action()
{
}
2020-04-24 22:57:13 +00:00
}
CODE_SAMPLE
, [self::ANNOTATION_TO_ATTRIBUTE => [new \Rector\Php80\ValueObject\AnnotationToAttribute('Symfony\\Component\\Routing\\Annotation\\Route')]])]);
2020-04-24 22:57:13 +00:00
}
/**
* @return array<class-string<Node>>
2020-04-24 22:57:13 +00:00
*/
public function getNodeTypes() : array
2020-04-24 22:57:13 +00:00
{
return [\PhpParser\Node\Stmt\Class_::class, \PhpParser\Node\Stmt\Property::class, \PhpParser\Node\Stmt\ClassMethod::class, \PhpParser\Node\Stmt\Function_::class, \PhpParser\Node\Expr\Closure::class, \PhpParser\Node\Expr\ArrowFunction::class];
2020-04-24 22:57:13 +00:00
}
/**
* @param Class_|Property|ClassMethod|Function_|Closure|ArrowFunction $node
2020-04-24 22:57:13 +00:00
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
2020-04-24 22:57:13 +00:00
{
if (!$this->isAtLeastPhpVersion(\Rector\Core\ValueObject\PhpVersionFeature::ATTRIBUTES)) {
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (!$phpDocInfo instanceof \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo) {
return null;
}
$tags = $phpDocInfo->getAllTags();
$hasNewAttrGroups = $this->processApplyAttrGroups($tags, $phpDocInfo, $node);
if ($hasNewAttrGroups) {
return $node;
}
return null;
}
/**
* @param array<string, AnnotationToAttribute[]> $configuration
*/
public function configure(array $configuration) : void
{
$annotationsToAttributes = $configuration[self::ANNOTATION_TO_ATTRIBUTE] ?? [];
\RectorPrefix20210614\Webmozart\Assert\Assert::allIsInstanceOf($annotationsToAttributes, \Rector\Php80\ValueObject\AnnotationToAttribute::class);
$this->annotationsToAttributes = $annotationsToAttributes;
2020-04-24 22:57:13 +00:00
}
/**
* @param array<PhpDocTagNode> $tags
* @param Class_|Property|ClassMethod|Function_|Closure|ArrowFunction $node
*/
private function processApplyAttrGroups(array $tags, \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $phpDocInfo, \PhpParser\Node $node) : bool
{
$hasNewAttrGroups = \false;
foreach ($tags as $tag) {
foreach ($this->annotationsToAttributes as $annotationToAttribute) {
$annotationToAttributeTag = $annotationToAttribute->getTag();
if ($phpDocInfo->hasByName($annotationToAttributeTag)) {
// 1. remove php-doc tag
$this->phpDocTagRemover->removeByName($phpDocInfo, $annotationToAttributeTag);
// 2. add attributes
$node->attrGroups[] = $this->phpAttributeGroupFactory->createFromSimpleTag($annotationToAttribute);
$hasNewAttrGroups = \true;
continue 2;
}
if ($this->shouldSkip($tag->value, $phpDocInfo, $annotationToAttributeTag)) {
continue;
}
// 1. remove php-doc tag
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $tag->value);
// 2. add attributes
/** @var DoctrineAnnotationTagValueNode $tagValue */
$tagValue = $tag->value;
$node->attrGroups[] = $this->phpAttributeGroupFactory->create($tagValue, $annotationToAttribute);
$hasNewAttrGroups = \true;
continue 2;
}
}
return $hasNewAttrGroups;
}
private function shouldSkip(\PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode $phpDocTagValueNode, \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $phpDocInfo, string $annotationToAttributeTag) : bool
{
$doctrineAnnotationTagValueNode = $phpDocInfo->getByAnnotationClass($annotationToAttributeTag);
if ($phpDocTagValueNode !== $doctrineAnnotationTagValueNode) {
return \true;
}
return !$phpDocTagValueNode instanceof \Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
}
2020-04-24 22:57:13 +00:00
}