Skip to content

Use of result of impure function in max(...) prevents type narrowing #12731

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

Open
claudepache opened this issue Mar 12, 2025 · 2 comments · May be fixed by phpstan/phpstan-src#3928
Open

Use of result of impure function in max(...) prevents type narrowing #12731

claudepache opened this issue Mar 12, 2025 · 2 comments · May be fixed by phpstan/phpstan-src#3928

Comments

@claudepache
Copy link

claudepache commented Mar 12, 2025

Bug report

Given an impure function f() returning an int, PHPStan fails to narrow the type of max(4, f()) to int<4, max> (it considers it as int).

Code snippet that reproduces the problem

https://phpstan.org/r/bace30de-7627-425a-9098-9ea3b9b6cf5b

Expected output

Lline 19: Dumped type: int<4, max>

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

No response

@claudepache claudepache changed the title Use of result of impure function in max(...) prevents type narrowing> Use of result of impure function in max(...) prevents type narrowing Mar 12, 2025
@staabm
Copy link
Contributor

staabm commented Mar 12, 2025

@claudepache
Copy link
Author

Likly needs a fix in https://github.com/phpstan/phpstan-src/blob/2.1.x/src/Type/Php/MinMaxFunctionReturnTypeExtension.php or in logic triggered from it

Indeed, on line 54:

// rewrite min($x, $y) as $x < $y ? $x : $y

is incorrect if $y is not constant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants