shipmonk-rnd / dead-code-detector

💀 PHP unused code detection via PHPStan extension. Detects dead cycles, supports libs like Symfony, Doctrine, PHPUnit etc. Can automatically remove dead PHP code.
105 stars 6 forks source link

feat: ignore methods with Override PHP Attribute #68

Closed vincentchalamon closed 1 month ago

vincentchalamon commented 2 months ago

Ignore methods with Override PHP Attribute as dead code should be detected on the interface or parent class.

janedbal commented 2 months ago

What is your actual usecase?

Currently, we do very similar thing for any vendor originated stuff:


Also, we properly detect "possible descendant calls", which means that this marks all children of FooInterface as used (even without the Override attribute):

interface FooInterface
{
    public function doSomething(): void;
}

class Foo implements FooInterface
{
    public function doSomething(): void {} // not dead
}

function (FooInterface $interface) {
    $interface->doSomething();
}

But mainly, I believe it is incorrect to assume that any method with #[Override] is not dead. Here is an example of such method being dead:


class ParentClass 
{

    public static function method() {}
}

class ChildClass extends ParentClass
{
    #[\Override]
    public static function method() {}
}

ParentClass::method();
vincentchalamon commented 2 months ago

What is your actual usecase?

I currently have an interface with some methods. Those methods are implemented in multiple objects, for instance:

interface Period
{
    public function hasOccurrenc(): bool;
    public function ranges(): iterable;
}

class SubscribedCalendarPeriod implements Period
{
    #[\Override]
    public function hasOccurrence(): bool
    {
        return true;
    }

    #[\Override]
    public function ranges(): iterable
    {
        return [];
    }
}

class CustomCalendarPeriod implements Period
{
    #[\Override]
    public function hasOccurrence(): bool
    {
        return false;
    }

    #[\Override]
    public function ranges(): iterable
    {
        return [];
    }
}

None of those methods are currently called, but it shows alerts on the interface and also on each class:

 ------ ---------------------------------------------------------------------------------- 
  Line   Domain/Model/CustomCalendarPeriod.php                                             
 ------ ---------------------------------------------------------------------------------- 
  56     Unused App\Domain\Model\CustomCalendarPeriod::ranges         
  108    Unused App\Domain\Model\CustomCalendarPeriod::hasOccurrence  
 ------ ----------------------------------------------------------------------------------

 ------ -------------------------------------------------------------------- 
  Line   Domain/Model/Period.php                                             
 ------ -------------------------------------------------------------------- 
  19     Unused App\Domain\Model\Period::ranges         
  40     Unused App\Domain\Model\Period::hasOccurrence  
 ------ -------------------------------------------------------------------- 

 ------ -------------------------------------------------------------------------------------- 
  Line   Domain/Model/SubscribedCalendarPeriod.php                                             
 ------ -------------------------------------------------------------------------------------- 
  95     Unused App\Domain\Model\SubscribedCalendarPeriod::ranges         
  155    Unused App\Domain\Model\SubscribedCalendarPeriod::hasOccurrence  
 ------ --------------------------------------------------------------------------------------

Am I missing something which should make it work with the actual state of the plugin?

But mainly, I believe it is incorrect to assume that any method with #[Override] is not dead. Here is an example of such method being dead:

You're right!

janedbal commented 2 months ago

None of those methods are currently called, but it shows alerts on the interface and also on each class Am I missing something which should make it work with the actual state of the plugin?

If none of your methods are called, all of them are expected to be reported as dead.


But you can always ignore any error with all the possible ignore approaches that PHPStan offers. Also, you can implement custom EntrypointProvider if you want some more complex "ignore" (e.g. based on parent or sth).

vincentchalamon commented 1 month ago

Thanks for your reply. Considering that some Override use-case might be dead-code (as you presented), it makes sense!