2020-04-24 22:57:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
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;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\PhpAttribute\AnnotationToAttributeConverter;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-04-24 22:57:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://wiki.php.net/rfc/attributes_v2
|
2020-10-04 11:12:41 +00:00
|
|
|
* @see https://wiki.php.net/rfc/shorter_attribute_syntax
|
|
|
|
* @see https://wiki.php.net/rfc/shorter_attribute_syntax_change - FINAL #[...] syntax !
|
2020-04-24 22:57:13 +00:00
|
|
|
*
|
|
|
|
* @see \Rector\Php80\Tests\Rector\Class_\AnnotationToAttributeRector\AnnotationToAttributeRectorTest
|
|
|
|
*/
|
|
|
|
final class AnnotationToAttributeRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var AnnotationToAttributeConverter
|
|
|
|
*/
|
|
|
|
private $annotationToAttributeConverter;
|
|
|
|
|
|
|
|
public function __construct(AnnotationToAttributeConverter $annotationToAttributeConverter)
|
|
|
|
{
|
|
|
|
$this->annotationToAttributeConverter = $annotationToAttributeConverter;
|
|
|
|
}
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2020-04-24 22:57:13 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Change annotation to attribute', [
|
2020-04-24 22:57:13 +00:00
|
|
|
new CodeSample(
|
2020-09-15 08:23:13 +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", name="action")
|
|
|
|
*/
|
|
|
|
public function action()
|
|
|
|
{
|
|
|
|
}
|
2020-04-24 22:57:13 +00:00
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-04-24 22:57:13 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +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
|
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
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2020-10-21 21:06:24 +00:00
|
|
|
return [
|
|
|
|
Class_::class,
|
|
|
|
Property::class,
|
|
|
|
ClassMethod::class,
|
|
|
|
Function_::class,
|
|
|
|
Closure::class,
|
|
|
|
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
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
return $this->annotationToAttributeConverter->convertNode($node);
|
|
|
|
}
|
|
|
|
}
|