phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

Implement a rule that prevents final constructors in Doctrine Entities

Open gnutix opened this issue 4 years ago • 6 comments

Hello there,

I'm having quite a hard time getting my head around this : final methods in Doctrine Entities. There's a PR that added a check that Entities cannot be final (the class itself), but a comment from the author said he doesn't want to add checks for final methods.

However, from what I can see in my Doctrine proxied classes in var/cache in production, all methods of the class is inherited/overridden in proxies. So it just cannot work with a final method, whichever it is.

Why hasn't this check been added then ?

I'm curious about your insights. Thanks! gnutix

gnutix avatar Jul 27 '21 14:07 gnutix

Ask the author :) /cc @enumag

ondrejmirtes avatar Jul 27 '21 14:07 ondrejmirtes

Yeah I have a case where I don't want the proxy to override my method because that would in fact cause a bug for me. Granted it's a bit of an edge-case so adding such rule to PHPStan makes sense but please make it separate from the rule I added so that I can disable it in my app.

enumag avatar Jul 27 '21 14:07 enumag

@enumag But how could it even work in your app ? When I added final to a private constructor of an entity, PHP just broke saying "cannot override final method" (in the Doctrine Proxy). How can you have a final method in an entity then, and not having Doctrine's proxy to break ? :thinking:

gnutix avatar Jul 27 '21 15:07 gnutix

There is a misunderstanding... the method is not the constructor. When there is a final non-constructor method, Doctrine simply skips it.

enumag avatar Jul 27 '21 16:07 enumag

Okay, so you agree that there should be a rule (that is always loaded) guarding against final constructors for Doctrine Entities (not all methods) then ?

gnutix avatar Jul 29 '21 10:07 gnutix

Sure.

enumag avatar Jul 29 '21 11:07 enumag

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar May 07 '23 00:05 github-actions[bot]