Skip to content

[DeadCode] RemoveUnusedPrivateMethodRector don't address callables #1700

@ravanscafi

Description

@ravanscafi

Description

Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector is removing valid code due to some false positives regarding callables. I'm not quite sure how to tackle this, so I'm just leaving the issue, sorry.

Current Behaviour

 final class MyClass
 {
     public function run()
     {
         $array  = [3, 2, 1];

         usort($array, [$this, 'sort']);

         return $array;
     }
-
-    private function sort($a, $b)
-    {
-        return $a <=> $b;
-    }
 }

Either [$this, 'sort'], [self::class, 'sort'], [static::class, 'sort'], [MyClass::class, 'sort'] are valid callables that I can recall.

Minimal PHP Code Causing Issue

Probably \Rector\NodeContainer\ParsedNodesByType::collect is not accounting for it.

Expected Behaviour

 final class MyClass
 {
     public function run()
     {
         $array  = [3, 2, 1];

         usort($array, [$this, 'sort']);

         return $array;
     }

    private function sort($a, $b)
    {
        return $a <=> $b;
    }
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions