mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 00:10:51 +00:00
[RemoveFinalFromConstRector] (#1732)
This commit is contained in:
parent
75c0b27d37
commit
15e28c6de8
|
@ -1,4 +1,4 @@
|
|||
# 519 Rules Overview
|
||||
# 520 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
|||
|
||||
- [CodeQuality](#codequality) (70)
|
||||
|
||||
- [CodingStyle](#codingstyle) (34)
|
||||
- [CodingStyle](#codingstyle) (35)
|
||||
|
||||
- [Compatibility](#compatibility) (1)
|
||||
|
||||
|
@ -2253,6 +2253,22 @@ Non-magic PHP object methods cannot start with "__"
|
|||
|
||||
<br>
|
||||
|
||||
### RemoveFinalFromConstRector
|
||||
|
||||
Remove final from constants in classes defined as final
|
||||
|
||||
- class: [`Rector\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector`](../rules/CodingStyle/Rector/ClassConst/RemoveFinalFromConstRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
- final public const NAME = 'value';
|
||||
+ public const NAME = 'value';
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### ReturnArrayClassMethodToYieldRector
|
||||
|
||||
Turns array return to yield return in specific type and method
|
||||
|
|
|
@ -6,6 +6,7 @@ use Rector\CodingStyle\Rector\Assign\PHPStormVarAnnotationRector;
|
|||
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
|
||||
use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
|
||||
use Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector;
|
||||
use Rector\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector;
|
||||
use Rector\CodingStyle\Rector\ClassConst\SplitGroupedConstantsAndPropertiesRector;
|
||||
use Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector;
|
||||
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
|
||||
|
@ -68,4 +69,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
$services->set(PostIncDecToPreIncDecRector::class);
|
||||
$services->set(UnSpreadOperatorRector::class);
|
||||
$services->set(NewlineAfterStatementRector::class);
|
||||
$services->set(RemoveFinalFromConstRector::class);
|
||||
};
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector;
|
||||
|
||||
use Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector;
|
||||
use Rector\Php81\Rector\ClassConst\FinalizePublicClassConstantRector;
|
||||
use Rector\Php81\Rector\ClassMethod\NewInInitializerRector;
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector\Fixture;
|
||||
|
||||
final class SkipNonFinalConst
|
||||
{
|
||||
public const NAME = 'value';
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
final public const NAME = 'value';
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public const NAME = 'value';
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class RemoveFinalFromConstRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(RemoveFinalFromConstRector::class);
|
||||
};
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Rector\ClassConst;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @changelog https://php.watch/versions/8.1/final-class-const
|
||||
*
|
||||
* @see \Rector\Tests\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector\RemoveFinalFromConstRectorTest
|
||||
*/
|
||||
final class RemoveFinalFromConstRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly VisibilityManipulator $visibilityManipulator
|
||||
) {
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Remove final from constants in classes defined as final', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
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
|
||||
{
|
||||
return [ClassConst::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassConst $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$parentClass = $this->betterNodeFinder->findParentType($node, Class_::class);
|
||||
|
||||
if (! $parentClass instanceof Class_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($parentClass->isFinal() && $node->isFinal()) {
|
||||
$this->visibilityManipulator->removeFinal($node);
|
||||
return $node;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function provideMinPhpVersion(): int
|
||||
{
|
||||
return PhpVersionFeature::FINAL_CLASS_CONSTANTS;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user