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
88 stars 7 forks source link

false positivs in Symfony projects #19

Closed pa- closed 9 months ago

pa- commented 1 year ago

Hi Tomas,

thanks for this Tool! Maybe i am doing something wrong, but right now i am getting a lot of false positivs in a Symfony project.

For example:

1) Constraints/Validators Only the Contraint is used (e.q. in an Entity) and not the Validator itself.

From the docs:

you create a custom Constraint (e.g. MyConstraint), Symfony will automatically look for another class, MyConstraintValidator when actually performing the validation.

So no direct use of the Validator

2) Tagged Iterator

I am using the tagged Iterator (https://symfony.com/doc/current/service_container/tags.html#reference-tagged-services). I tag all classes implementing some Interface and inject them into a Service. So there is no direct use of the class implementing the Interface

3) Mutations/Resolver in GraphQL

This is maybe not directly related to Symfony, but maybe you have some thoughts on how to handle this. im use the overblog/GraphQLBundle to handle GQL Request. If you are not familier with GQL i'll try to give you some quick introduction. You have an Query (or Mutation), which is defined in an yaml (see Example below). In this example: "adresseUsage". You can think of it as an "action" in a Symfony Controller. Every GQL Request send to a single REST Endpoint and internally handled by the configured Resolver ("AdresseResolver")

AdresseQuery:
    type: object
    config:
        fields:
            adresseUsage:
                type: AdresseUsage
                resolve: '@=resolver("App\\Resolver\\AdresseResolver", [info, value, args])'
                args:
                    adressId:
                        type: ID

So, again no direct use of the Resolver itself :disappointed:

4) Messenges/MessageHandler Very similar to Constraints/Validator. You have same Messages dispatched through an MessageBus and the handled by a MessageHandler. The MessageHandlers are configured by an Attribute and Symfony will take care of everything.


Any toughts on how to solve these issues?

best regards!

ilnytskyi commented 1 year ago

Got similar problem with other tools (deptrac) for tracking dependencies. Both use nikic/php-parser under the hood and check actual php imports. The solution might be to extend the dependency collector to scan .yaml .xml other codegen places

TomasVotruba commented 9 months ago

Thanks for feedback :+1:

We've added a white list of classes, that should be skipped to help with this use case.

If your Symfony/Doctrine/framework class is missing there, please add it so it's passing: https://github.com/TomasVotruba/class-leak/blob/ab39660d55919f1273d8f77aac64dea8588bd482/src/Filtering/PossiblyUnusedClassesFilter.php#L18