Skip to content

"$variable might not be defined" errors when using exhaustive if statements with constant enums #5266

Closed

Description

@ekisu

Bug report

When using constant enums on function parameters, variables that are declared inside an exhaustive if statement are reported as might not be defined, even though it's certainly defined.

Code snippet that reproduces the problem

https://phpstan.org/r/4a1aeeb3-758f-4dc9-99ab-dee8055071c6

Expected output

There should be no error reported, as every case of the constant enum is covered by the if statement, and every branch of the if statement declares the $message variable

Activity

  1. ondrejmirtes commented on Jul 8, 2021

    @ondrejmirtes
    Member
  2. added this to the Easy fixes milestone on Jul 8, 2021
  3. phpstan-bot commented on Sep 23, 2021

    @phpstan-bot
    Collaborator

    @ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

    @@ @@
    -No errors
    +-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40
    +Run PHPStan with --debug option and post the stack trace to:
    +https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
    Full report
    Line Error
    -1 Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
  4. phpstan-bot commented on Oct 4, 2021

    @phpstan-bot
    Collaborator

    @ekisu After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

    @@ @@
    -19: Variable $message might not be defined.
    +No errors
  5. schlndh commented on Oct 20, 2023

    @schlndh
    Contributor

    @ondrejmirtes This issue can be closed.

  6. added a commit that references this issue on Nov 2, 2023
  7. github-actions commented on Dec 4, 2023

    @github-actions

    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.

  8. locked as resolved and limited conversation to collaborators on Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions