2020-04-24 22:57:13 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2020-04-24 22:57:13 +00:00
namespace Rector\Php80\Rector\Class_ ;
use PhpParser\Node ;
2020-10-21 21:06:24 +00:00
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 ;
2020-10-21 21:06:24 +00:00
use PhpParser\Node\Stmt\Function_ ;
2020-04-24 22:57:13 +00:00
use PhpParser\Node\Stmt\Property ;
2021-05-28 12:08:33 +00:00
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode ;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode ;
2021-04-04 09:01:11 +00:00
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode ;
2021-03-20 23:16:21 +00:00
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 ;
2021-03-20 23:16:21 +00:00
use Rector\Core\ValueObject\PhpVersionFeature ;
use Rector\Php80\ValueObject\AnnotationToAttribute ;
use Rector\PhpAttribute\Printer\PhpAttributeGroupFactory ;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2021-06-14 02:16:47 +00:00
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
*
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\AnnotationToAttributeRectorTest
2020-04-24 22:57:13 +00:00
*/
2021-05-10 22:23:08 +00:00
final class AnnotationToAttributeRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
2020-04-24 22:57:13 +00:00
{
/**
2021-03-20 23:16:21 +00:00
* @ var string
2020-04-24 22:57:13 +00:00
*/
2021-03-20 23:16:21 +00:00
public const ANNOTATION_TO_ATTRIBUTE = 'annotation_to_attribute' ;
/**
* @ var AnnotationToAttribute []
*/
private $annotationsToAttributes = [];
/**
2021-05-10 23:39:21 +00:00
* @ var \Rector\PhpAttribute\Printer\PhpAttributeGroupFactory
2021-03-20 23:16:21 +00:00
*/
private $phpAttributeGroupFactory ;
/**
2021-05-10 23:39:21 +00:00
* @ var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover
2021-03-20 23:16:21 +00:00
*/
private $phpDocTagRemover ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\PhpAttribute\Printer\PhpAttributeGroupFactory $phpAttributeGroupFactory , \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover $phpDocTagRemover )
2021-05-09 20:15:43 +00:00
{
2021-03-20 23:16:21 +00:00
$this -> phpAttributeGroupFactory = $phpAttributeGroupFactory ;
$this -> phpDocTagRemover = $phpDocTagRemover ;
2020-04-24 22:57:13 +00:00
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-04-24 22:57:13 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Change annotation to attribute' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\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
/**
* @ Route ( " /path " , name = " action " )
*/
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
{
2021-02-28 07:47:48 +00:00
#[Route(path: '/path', name: 'action')]
public function action ()
{
}
2020-04-24 22:57:13 +00:00
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-06-05 21:47:54 +00:00
, [ self :: ANNOTATION_TO_ATTRIBUTE => [ new \Rector\Php80\ValueObject\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
{
2021-05-10 22:23:08 +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
}
/**
2020-10-21 21:06:24 +00:00
* @ param Class_ | Property | ClassMethod | Function_ | Closure | ArrowFunction $node
2020-04-24 22:57:13 +00:00
*/
2021-05-10 22:23:08 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2020-04-24 22:57:13 +00:00
{
2021-05-10 22:23:08 +00:00
if ( ! $this -> isAtLeastPhpVersion ( \Rector\Core\ValueObject\PhpVersionFeature :: ATTRIBUTES )) {
2021-03-20 23:16:21 +00:00
return null ;
}
$phpDocInfo = $this -> phpDocInfoFactory -> createFromNode ( $node );
2021-05-10 22:23:08 +00:00
if ( ! $phpDocInfo instanceof \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo ) {
2021-03-20 23:16:21 +00:00
return null ;
}
2021-05-28 12:08:33 +00:00
$tags = $phpDocInfo -> getAllTags ();
$hasNewAttrGroups = $this -> processApplyAttrGroups ( $tags , $phpDocInfo , $node );
2021-03-20 23:16:21 +00:00
if ( $hasNewAttrGroups ) {
return $node ;
}
return null ;
}
/**
* @ param array < string , AnnotationToAttribute [] > $configuration
*/
2021-05-09 20:15:43 +00:00
public function configure ( array $configuration ) : void
2021-03-20 23:16:21 +00:00
{
$annotationsToAttributes = $configuration [ self :: ANNOTATION_TO_ATTRIBUTE ] ? ? [];
2021-06-14 02:16:47 +00:00
\RectorPrefix20210614\Webmozart\Assert\Assert :: allIsInstanceOf ( $annotationsToAttributes , \Rector\Php80\ValueObject\AnnotationToAttribute :: class );
2021-03-20 23:16:21 +00:00
$this -> annotationsToAttributes = $annotationsToAttributes ;
2020-04-24 22:57:13 +00:00
}
2021-05-28 12:08:33 +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
}