Skip to content

Commit

Permalink
[DeadCode] Skip Class Constant used in Enum on RemoveUnusedPrivateCla… (
Browse files Browse the repository at this point in the history
#3174)

* Add failing test fixture for RemoveUnusedPrivateClassConstantRector

# Failing Test for RemoveUnusedPrivateClassConstantRector

Based on https://getrector.org/demo/2333a008-e4b0-4d11-9ade-3b37412193d2

* [DeadCode] Skip Class Constant used in Enum on RemoveUnusedPrivateClassConstantRector

* Explicitly check for class-like parent

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>

* Use class import

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
eliashaeussler and samsonasik committed Dec 9, 2022
1 parent 4ba03ab commit b23e1fb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\Fixture;

enum SkipClassConstantUsedInEnum
{
case Foo;
case Baz;

private const SOME_CONST = [
self::Foo,
self::Baz,
];

public function run(): bool
{
return in_array($this, self::SOME_CONST, true);
}
}
7 changes: 4 additions & 3 deletions src/NodeManipulator/ClassConstManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Enum_;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
Expand All @@ -25,9 +26,9 @@ public function __construct(

public function hasClassConstFetch(ClassConst $classConst, ClassReflection $classReflection): bool
{
$class = $this->betterNodeFinder->findParentType($classConst, Class_::class);
if (! $class instanceof Class_) {
return false;
$class = $this->betterNodeFinder->findParentByTypes($classConst, [Class_::class, Enum_::class]);
if (! $class instanceof ClassLike) {
return true;
}

$className = (string) $this->nodeNameResolver->getName($class);
Expand Down

0 comments on commit b23e1fb

Please sign in to comment.