diff --git a/rules-tests/Restoration/Rector/Class_/RemoveFinalFromEntityRector/Fixture/with_attribute.php.inc b/rules-tests/Restoration/Rector/Class_/RemoveFinalFromEntityRector/Fixture/with_attribute.php.inc new file mode 100644 index 00000000000..8e692169b11 --- /dev/null +++ b/rules-tests/Restoration/Rector/Class_/RemoveFinalFromEntityRector/Fixture/with_attribute.php.inc @@ -0,0 +1,25 @@ + +----- + diff --git a/rules/Restoration/Rector/Class_/RemoveFinalFromEntityRector.php b/rules/Restoration/Rector/Class_/RemoveFinalFromEntityRector.php index a8da8af6125..7670cac2c4e 100644 --- a/rules/Restoration/Rector/Class_/RemoveFinalFromEntityRector.php +++ b/rules/Restoration/Rector/Class_/RemoveFinalFromEntityRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\Class_; use Rector\Core\Rector\AbstractRector; +use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer; use Rector\Privatization\NodeManipulator\VisibilityManipulator; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -16,8 +17,25 @@ */ final class RemoveFinalFromEntityRector extends AbstractRector { + /** + * @var string[] + */ + private const ALLOWED_ANNOTATIONS = [ + 'Doctrine\ORM\Mapping\Entity', + 'Doctrine\ORM\Mapping\Embeddable' + ]; + + /** + * @var string[] + */ + private const ALLOWED_ATTRIBUTES = [ + 'Doctrine\ORM\Mapping\Entity', + 'Doctrine\ORM\Mapping\Embeddable' + ]; + public function __construct( private readonly VisibilityManipulator $visibilityManipulator, + private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer ) { } @@ -64,7 +82,10 @@ public function getNodeTypes(): array public function refactor(Node $node): ?Node { $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - if (! $phpDocInfo->hasByAnnotationClasses(['Doctrine\ORM\Mapping\Entity', 'Doctrine\ORM\Mapping\Embeddable'])) { + if ( + ! $phpDocInfo->hasByAnnotationClasses(self::ALLOWED_ANNOTATIONS) + && ! $this->phpAttributeAnalyzer->hasPhpAttributes($node, self::ALLOWED_ATTRIBUTES) + ) { return null; }