Skip to content
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

Bleeding Edge: Type xxx of property xxx is not the same as type xxx of overridden property #5494

Closed
Dgame opened this issue Aug 20, 2021 · 6 comments

Comments

@Dgame
Copy link

Dgame commented Aug 20, 2021

Bug report

I get the error

Type array<class-string> of property MyHandler::$dontReport is not the same as type array of overridden property Handler::$dontReport.

with bleeding-edge enabled. Link to the Playground.

I'm overriding the property of the lumen error handler so I can't change the parent class. But IMO, since I'm making the type more concrete, this shouldn't be an error in the first place.

Expected output

No error, as we make the type more concrete than it is.

@ondrejmirtes
Copy link
Member

Hi, this is expected. Property types can't be covariant/contravariant - they're invariant. This helps you to be protected against the parent class writing something that isn't class-string to the property.

You can fix this issue with stub files: https://phpstan.org/user-guide/stub-files

@Dgame
Copy link
Author

Dgame commented Aug 20, 2021

In that case, how am I supposed to overwrite third-party library code that expects me to overwrite it but is not phpstan-compatible? I can't - at least not without StubFiles as it seems. But instead, I and probably many others will just write @phpstan-ignore-next-line and that's it. Wouldn't it make more sense to recognise that I'm specifying the type, especially if the parent type is just array? I would understand it if I made int into string, or array<int> into array<string>. Even if it was array<mixed> and I tried to specify it as array<int>. But if it is just array in the parent and I specify it as array<int>, then IMO it should be perfectly fine. I suspect you probably consider array to be an implicit array<mixed>, but I think that is wrong in this case.

@ondrejmirtes ondrejmirtes reopened this Aug 21, 2021
@ondrejmirtes
Copy link
Member

Yeah, I'll relax those rules a bit, same as method parameters and return types are not enforced fully by default. The strictest variant will be an opt-in via a separate option (and enabled in phpstan-strict-rules).

@ondrejmirtes
Copy link
Member

Implemented: phpstan/phpstan-src@24f6264

The rules are relaxed a bit, you should get the stricter version with reportMaybesInPropertyPhpDocTypes: true and/or with phpstan-strict-rules.

There are also included tips in the error messages telling you about this option and stub files.

@Dgame
Copy link
Author

Dgame commented Aug 21, 2021

Thank you very much!

@github-actions
Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators Sep 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants