Skip to content

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

@mind-bending-forks

Description

@mind-bending-forks

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions