rector/rules/php80/src/Rector/Class_/AnnotationToAttributeRector.php

88 lines
2.1 KiB
PHP
Raw Normal View History

2020-04-24 22:57:13 +00:00
<?php
declare(strict_types=1);
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 Rector\Core\Rector\AbstractRector;
use Rector\PhpAttribute\AnnotationToAttributeConverter;
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
* @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;
}
public function getRuleDefinition(): RuleDefinition
2020-04-24 22:57:13 +00:00
{
return new RuleDefinition('Change annotation to attribute', [
2020-04-24 22:57:13 +00:00
new CodeSample(
<<<'CODE_SAMPLE'
2020-04-24 22:57:13 +00:00
use Doctrine\ORM\Attributes as ORM;
/**
* @ORM\Entity
*/
class SomeClass
{
}
CODE_SAMPLE
2020-04-24 22:57:13 +00:00
,
<<<'CODE_SAMPLE'
2020-04-24 22:57:13 +00:00
use Doctrine\ORM\Attributes as ORM;
#[ORM\Entity]
2020-04-24 22:57:13 +00:00
class SomeClass
{
}
CODE_SAMPLE
2020-04-24 22:57:13 +00:00
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [
Class_::class,
Property::class,
ClassMethod::class,
Function_::class,
Closure::class,
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(Node $node): ?Node
{
return $this->annotationToAttributeConverter->convertNode($node);
}
}