dead-code-detector icon indicating copy to clipboard operation
dead-code-detector copied to clipboard

Detect dead enum cases

Open janedbal opened this issue 8 months ago • 3 comments

janedbal avatar Apr 04 '25 13:04 janedbal

Just my 2 cents: I'm not sure this is a good idea. I once made a PHPStan rule to detect them. And most of the time, our enums are mapped to database state. Not every case is tested. So it will produce lots of errors. I couldn't find anything worth deleting.

ruudk avatar Apr 04 '25 13:04 ruudk

@ruudk Thx, I can imagine such problems, but you can always disable the analysis:

ignoreErrors:
   - identifier: shipmonk.deadEnumCase

And some codebases may benefit from this. Also, those mapped to database can be easily detected in custom usage provider (assuming Doctrine):

#[Column(type: Types::STRING, enumType: InvoiceStatus::class]
private InvoiceStatus $status;

janedbal avatar Apr 04 '25 13:04 janedbal

I just realized that we should indeed create a usage detector, great 😁

ruudk avatar Apr 04 '25 13:04 ruudk

I just had an idea, whenever from or tryFrom is called on an enum, we should mark the enum as not dead.

Together with the fact that it's used inside #[Column(enumType: TheEnum::type))

ruudk avatar Jun 26 '25 08:06 ruudk

I just had an idea, whenever from or tryFrom is called on an enum, we should mark the enum as not dead.

Together with the fact that it's used inside #[Column(enumType: TheEnum::type))

Both those are already implemented. The only issue with this being enabled by default are typically API input objects, where enums are typically deserialized into the objects. Those cannot be easily natively supported and each app need to add own provider.

Basically, this PR is done and working (I even fully used it in our huge codebase), but I'm still thinking it might be disabled by default.

janedbal avatar Jun 26 '25 09:06 janedbal