Skip to content

False positive "variable might not be defined" inside for loop #10657

Closed

Description

@t-gebauer

Bug report

Combining dependent types with a for loop causes PHPStan to falsely report an error.

In the linked code, $foo is guaranteed to be defined, when $flag is truthy.

Code snippet that reproduces the problem

https://phpstan.org/r/79ccd56f-c164-4389-8ed4-2e593b350449

Expected output

PHPStan should not report an error. Just like the same code without a loop: https://phpstan.org/r/45d91429-2383-4127-9f84-6b3e96eaa9be

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

Yes, every other day it finds possible bugs :)

Activity

  1. added this to the Dependent types milestone on May 30, 2024
  2. phpstan-bot commented on Mar 4, 2026

    @phpstan-bot
    Collaborator

    Closed via merging phpstan/phpstan-src#5125

  3. github-actions commented on Apr 5, 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.

  4. locked as resolved and limited conversation to collaborators on Apr 5, 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