-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix "Unsafe usage of new static()" errors reported by phpstan #14506
Comments
IMO if someone is extending the class they would want it to produce instance of that class instead of the parent, so using So next question is if we are to add |
I agree that new self() is a poor choice. I think it depends on whether we provide an initialization method (or something). |
Adding |
@garas Yes, adding It's unfortunate the there's no way in PHP to only allow overriding the constructor without changing it's signature. |
Other projects are grappling with the same issue https://www.drupal.org/project/drupal/issues/3118975. |
I don't know what drupal's code looks like, but I don't see how they never want to extend a class that they instantiate with new static(). The problem we'll have is when we have cases where we need to override the constructor internally. We can't just remove the final keyword because cake makes everything public use. Users will simply extend the base class and cause an issue. We need classes that have proper initialization routines so we don't have a problem with making base class constructors final. I don't know if we have this situation anywhere internally. |
I dont think we need to do anything here.
Personally, where this is relevant, I always use the method factory pattern
as those can be properly interfaced. |
So, are we OK using an interface contractable factory method as I suggested?
That should solve it without further hazzle and is BC enough as the added interface method is also implemented right away in the base class. |
I am inclined to not change anything. Just accept that what we have currently technically can cause issues but in practice anyone changing the constructor signature is expected to change related |
I think this is a 5.x design question around initialization interfaces. Unless everything is wrapped in a factory, new self isn't a great choice right now. |
Adding to 5.0 topics in wiki and close? |
phpstan currently reports error for statements like
new static(..)
in various class. For e.g. inSession
class:cakephp/src/Http/Session.php
Line 127 in e3c728d
The reason is if someone extends the class and changes the constructor signature the
new static()
calls could start generating errors.There are two ways to fix this error, which one should we chose?
final
keyword to the class constructornew static()
tonew self()
.The text was updated successfully, but these errors were encountered: