2022-01-26 18:39:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodingStyle\Rector\ClassConst;
|
2022-01-26 18:39:23 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2022-01-26 18:39:23 +00:00
|
|
|
/**
|
|
|
|
* @changelog https://php.watch/versions/8.1/final-class-const
|
|
|
|
*
|
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector\RemoveFinalFromConstRectorTest
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveFinalFromConstRector extends AbstractRector implements MinPhpVersionInterface
|
2022-01-26 18:39:23 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Rector\Privatization\NodeManipulator\VisibilityManipulator
|
|
|
|
*/
|
|
|
|
private $visibilityManipulator;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(VisibilityManipulator $visibilityManipulator)
|
2022-01-26 18:39:23 +00:00
|
|
|
{
|
|
|
|
$this->visibilityManipulator = $visibilityManipulator;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2022-01-26 18:39:23 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove final from constants in classes defined as final', [new CodeSample(<<<'CODE_SAMPLE'
|
2022-01-26 18:39:23 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
final public const NAME = 'value';
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public const NAME = 'value';
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2023-03-29 20:33:54 +00:00
|
|
|
return [Class_::class];
|
2022-01-26 18:39:23 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-03-29 20:33:54 +00:00
|
|
|
* @param Class_ $node
|
2022-01-26 18:39:23 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2022-01-26 18:39:23 +00:00
|
|
|
{
|
2023-03-29 20:33:54 +00:00
|
|
|
if (!$node->isFinal()) {
|
2022-01-26 18:39:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-03-29 20:33:54 +00:00
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($node->getConstants() as $classConst) {
|
|
|
|
if (!$classConst->isFinal()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->visibilityManipulator->removeFinal($classConst);
|
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
if ($hasChanged) {
|
2022-01-26 18:39:23 +00:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::FINAL_CLASS_CONSTANTS;
|
2022-01-26 18:39:23 +00:00
|
|
|
}
|
|
|
|
}
|