Skip to content

Variable inside loop might not be defined. #6830

Closed
phpstan/phpstan-src
#6385

Description

@hocker1

Code snippet here:

https://phpstan.org/r/37400131-6bac-49dd-9088-5fa3614957bf

The variable is "always true" and "might not be defined" at the same time, which doesn't make any sense to me. Without the "foreach" phpstan handles the code correctly.

P.S.: Thank You for awesome tool (phpstan), which helps me with refactoring legacy code a lot.

Activity

  1. added this to the Dependent types milestone on Mar 15, 2022
  2. hemberger commented on Jan 16, 2023

    @hemberger

    The thing that was so curious to me about this issue is that dumpType does infer the correct type in a lot of cases, but the "might not be defined" warning is still emitted (I misunderstood dumpType -- it seems that certainty of the type doesn't guarantee that it's defined).

    I have a lot of logic like this in templating code, which can't always be reorganized to circumvent the warning. I'm crossing my fingers for a fix some day from one of our amazing maintainers! ❤️

    I'll add my example here, though it is much the same as the original:
    https://phpstan.org/r/96490bfa-916f-487b-a915-30c752ef65d3

  3. phpstan-bot commented on Sep 24, 2024

    @phpstan-bot
    Contributor

    @hocker1 After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
    + 3: Function test() returns void but does not have any side effects.
     14: If condition is always true.
     14: Variable $x might not be defined.
    Full report
    Line Error
    3 Function test() returns void but does not have any side effects.
    14 If condition is always true.
    14 Variable $x might not be defined.
  4. phpstan-bot commented on Sep 24, 2024

    @phpstan-bot
    Contributor

    @hemberger After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
    + 4: Function test() returns void but does not have any side effects.
     10: Dumped type: 'foo'
     10: Variable $foo might not be defined.
    Full report
    Line Error
    4 Function test() returns void but does not have any side effects.
    10 Dumped type: 'foo'
    10 Variable $foo might not be defined.
  5. phpstan-bot commented on Mar 4, 2026

    @phpstan-bot
    Contributor

    @hemberger After the latest push in 2.2.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -10: Dumped type: 'foo'
    -10: Variable $foo might not be defined.
    + 4: Function test() returns void but does not have any side effects.
    +10: Dumped type: 'foo'
    Full report
    Line Error
    4 Function test() returns void but does not have any side effects.
    10 Dumped type: 'foo'
  6. added 3 commits that reference this issue on Mar 4, 2026
    906e8bf
    437d1f8
    44a1089
  7. hemberger commented on Mar 17, 2026

    @hemberger

    This has been fixed for my use case in the 2.1.41 release. Thank you for all your hard work!

  8. github-actions commented on Oct 13, 2026

    @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.

  9. locked as resolved and limited conversation to collaborators on Oct 13, 2026
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