Skip to content

Commit

Permalink
Support attributes in RemoveFinalFromEntityRector (#3727)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexndlm committed May 6, 2023
1 parent dab0910 commit e171f17
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\Restoration\Rector\Class_\RemoveFinalFromEntityRector\Fixture;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]

final class SomeClass
{
}

?>
-----
<?php

namespace Rector\Tests\Restoration\Rector\Class_\RemoveFinalFromEntityRector\Fixture;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]class SomeClass
{
}

?>
23 changes: 22 additions & 1 deletion rules/Restoration/Rector/Class_/RemoveFinalFromEntityRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
) {
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit e171f17

Please sign in to comment.