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

PHPStan incorrectly thinks variable is 0 after switch when using integer-backed enums #9723

Closed
mind-bending-forks opened this issue Aug 5, 2023 · 2 comments
Labels
Milestone

Comments

@mind-bending-forks
Copy link

Bug report

In the following two examples, which make use of integer-backed enums, PHPStan seems to be certain that an integer variable has the value 0 after a switch statement, when that's not the case. As a result, it emits a misleading error message when the variable is tested:

  1. https://phpstan.org/r/199dc290-8894-4b30-aa2d-33eb9ce83456
  2. https://phpstan.org/r/342eb782-8277-4464-9566-f7dabe71e2dd

In the second example, the error message also suggests that it may be confused about the value of the enumeration constants.

If the enums are removed and replaced by integers, PHPStan correctly reports no errors. Here are the plain integer (no-enum) equivalents of the cases above:

  1. https://phpstan.org/r/b69e8646-7258-4729-a5b9-270065f2389d
  2. https://phpstan.org/r/ba586671-5e9d-4d99-b852-b823536bd49e

Code snippet that reproduces the problem

See above

Expected output

For the first two cases with integer-backed enums above, PHPStan should not have reported an error message. It should have behaved exactly like the pure integer non-enum cases.

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

PHPStan is integrated into our continuous integration pipeline. It improves code quality by helping catch easy-to-make but hard-to-spot errors straight away, during development. It also forces us to be more precise and to think more carefully about validation and error handling, therefore making the code more robust. One area where we can see an improvement is in code handling rare failure cases. Before PHPStan, it was more likely that glitches in such parts of the code would have made their way through to testing, or past testing - possibly until a long time after deployment - when that rare failure case actually occurred. Thank you PHPStan!

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src#2787

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 Dec 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants