Skip to content

Commit

Permalink
[Php81] Do not add final modifier on class constant that the class ha…
Browse files Browse the repository at this point in the history
…s 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>
  • Loading branch information
3 people committed Jan 28, 2022
1 parent a734965 commit 79dd9bb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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';
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
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;
Expand All @@ -22,13 +25,16 @@
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
Expand Down Expand Up @@ -61,13 +67,13 @@ public function getNodeTypes(): array
*/
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;
}

Expand All @@ -83,6 +89,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->isClassHasChildren($class)) {
return null;
}

$this->visibilityManipulator->makeFinal($node);
return $node;
}
Expand All @@ -91,4 +101,20 @@ public function provideMinPhpVersion(): int
{
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) !== [];
}
}

0 comments on commit 79dd9bb

Please sign in to comment.