TomasVotruba / class-leak

Find leaking classes that you never use... and get rid of them.
https://tomasvotruba.com/blog/how-to-avoid-maintaining-classes-you-dont-use
MIT License
75 stars 6 forks source link

Class used in the same namespace but marked as unused #21

Closed manhunto closed 6 months ago

manhunto commented 1 year ago

Hi, @TomasVotruba thanks for creating this tool. We started using it in our company, and it found many classes that can be removed! We love it!

To the point, we found this tool has problems when a class is used in the same namespace. It marks it as unused, but in fact, a class is used.

Example:

<?php

namespace App;

class A {
    public function main()
    {
        return B::someMethod();
    }
}
<?php

namespace App;

class B {
    public static function someMethod()
    {
        return 1;
    }
}

In this example, class B is marked as unused. To fix it, I have to add use, but in this case it is redundant. Rule no_unused_imports in phpcs remove it.

<?php

namespace App;

+use App/B;

class A {
    public function main()
    {
        return B::someMethod();
    }
}

and then class-leak tool doesn't mark class B as unused.

I haven't taken a look in codebase deeply, but I would love to introduce a fix for that, but I want to hear that it is possible/doable without refactoring a whole tool :D

TomasVotruba commented 8 months ago

Hi, thanks for reporting, this should work :+1:

@samsonasik Hi Abdul, could you look into fixing this? :pray:

samsonasik commented 8 months ago

@TomasVotruba I need to understand how class-leak library is working.

@manhunto could you create reproducible repo with step by step that show the bug, thank you.

manhunto commented 8 months ago

@samsonasik Sure, I will try to do it today or at the beginning of the following week ;)

ruudk commented 6 months ago

Fixed in https://github.com/TomasVotruba/class-leak/pull/34