mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-08 20:22:24 +00:00
[Php81] Do not add final modifier on class constant that the class has children on FinalizePublicClassConstantRector (#1739)
* Add failing test fixture for FinalizePublicClassConstantRector # Failing Test for FinalizePublicClassConstantRector Based on https://getrector.org/demo/1ec7f537-6973-6358-8246-11db50a94ec6 * Rename parent_class.php.inc to class_inheritance.php.inc * Update rules-tests/Php81/Rector/ClassConst/FinalizePublicClassConstantRector/Fixture/class_inheritance.php.inc Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> * Closes #1738 * final touch: clean up * plural fix * Update rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php Co-authored-by: Tomas Votruba <tomas.vot@gmail.com> Co-authored-by: Alexandre Bertrand <trandbert37@users.noreply.github.com> Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
This commit is contained in:
parent
a734965d99
commit
79dd9bb5f4
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php81\Rector\ClassConst\FinalizePublicClassConstantRector\Fixture;
|
||||
|
||||
abstract class ParentClass
|
||||
{
|
||||
public const TEST_CONSTANT = 'value1';
|
||||
}
|
||||
|
||||
class ChildClass extends ParentClass
|
||||
{
|
||||
public const TEST_CONSTANT = 'value2';
|
||||
}
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Php81\Rector\ClassConst\FinalizePublicClassConstantRector\Fixture;
|
||||
|
||||
abstract class ParentClass
|
||||
{
|
||||
public const TEST_CONSTANT = 'value1';
|
||||
}
|
||||
|
||||
class ChildClass extends ParentClass
|
||||
{
|
||||
public final const TEST_CONSTANT = 'value2';
|
||||
}
|
||||
?>
|
|
@ -7,8 +7,11 @@ namespace Rector\Php81\Rector\ClassConst;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
|
||||
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -22,13 +25,16 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
final class FinalizePublicClassConstantRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly FamilyRelationsAnalyzer $familyRelationsAnalyzer,
|
||||
private readonly ReflectionProvider $reflectionProvider,
|
||||
private readonly ClassAnalyzer $classAnalyzer,
|
||||
private readonly VisibilityManipulator $visibilityManipulator
|
||||
) {
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add final to constants that', [
|
||||
return new RuleDefinition('Add final to constants that does not have children', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
|
@ -61,13 +67,13 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$parentClass = $this->betterNodeFinder->findParentType($node, Class_::class);
|
||||
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
|
||||
|
||||
if (! $parentClass instanceof Class_) {
|
||||
if (! $class instanceof Class_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($parentClass->isFinal()) {
|
||||
if ($class->isFinal()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -83,6 +89,10 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
if ($this->isClassHasChildren($class)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->visibilityManipulator->makeFinal($node);
|
||||
return $node;
|
||||
}
|
||||
|
@ -91,4 +101,20 @@ CODE_SAMPLE
|
|||
{
|
||||
return PhpVersionFeature::FINAL_CLASS_CONSTANTS;
|
||||
}
|
||||
|
||||
private function isClassHasChildren(Class_ $class): bool
|
||||
{
|
||||
if ($this->classAnalyzer->isAnonymousClass($class)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$className = (string) $this->nodeNameResolver->getName($class);
|
||||
if (! $this->reflectionProvider->hasClass($className)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$classReflection = $this->reflectionProvider->getClass($className);
|
||||
|
||||
return $this->familyRelationsAnalyzer->getChildrenOfClassReflection($classReflection) !== [];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user