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

Constant string param template type is generalized to string #10720

Closed
axlon opened this issue Mar 14, 2024 · 5 comments
Closed

Constant string param template type is generalized to string #10720

axlon opened this issue Mar 14, 2024 · 5 comments

Comments

@axlon
Copy link
Contributor

axlon commented Mar 14, 2024

Bug report

It seems that PHPStan generalizes template types somewhat when no clear bound is defined. In my example I pass a constant string which is not empty, a union between this template type and another that is bound to non-empty-string should result in non-empty-string but instead PHPStan reports string

Code snippet that reproduces the problem

https://phpstan.org/r/3c8a8a00-c08f-405d-9d98-b603d00b5631

Expected output

No errors.

The $default parameter to getOrDefault() is a constant string in the example, so we know both sides of the return type are not going to be empty.

Did PHPStan help you today? Did it make you happy in any way?

No response

@axlon
Copy link
Contributor Author

axlon commented Mar 14, 2024

As always, am happy to fix this myself if pointed in the right direction. 🙂

@mad-briller
Copy link
Contributor

mad-briller commented Mar 14, 2024

template generalization comes up quite often and it's hard problem to solve as there is no one-size-fits-all solution, take a look at this issue for some more information and possible inspiration for fixing it:
phpstan/phpstan-src#1038

@axlon
Copy link
Contributor Author

axlon commented Mar 14, 2024

@mad-briller thanks for the link, seems this might be a bit more complicated than I had initially thought. If I'm understanding the situation correctly it would be quite easy to make my use case work but it may create problems for others as templates might suddenly be stricter than they should be.

@ondrejmirtes
Copy link
Member

Your specific case is already fixed, if you enable bleeding edge: https://phpstan.org/r/ca05f1e3-d908-48a4-8b1e-5d4006687a97

(I figured out most of "generics generalization" problems are happening inside generic objects - if you were to to something like Foo<TDefault> and not just TDefault, it would still be generalized.)

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 Apr 15, 2024
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

3 participants