Skip to content

Confusing messaging for undefined variables in loops #2032

Closed
phpstan/phpstan-src
#6130

Description

@muglug

I think this was the root of the confusion in #821 (comment)

function noloop(array $foo) {
    if ($undefined) {}
}

function loop(array $foo) {
    foreach ($foo as $bar) {
        if ($undefined) {}
    }
}

Expected:

4: Undefined variable: $undefined
9: Undefined variable: $undefined

Actual:

4: Undefined variable: $undefined
9: Variable $undefined might not be defined.

https://phpstan.org/r/d64a6ad0-4cff-4eb6-95be-ef00b57596c1

Activity

  1. phpstan-bot commented on Mar 9, 2021

    @phpstan-bot
    Collaborator

    @muglug PHPStan now reports different result with your code snippet:

    @@ @@
    + 3: Function noloop() has no return typehint specified.
    + 3: Function noloop() has parameter $foo with no value type specified in iterable type array.
      4: Undefined variable: $undefined
    +11: Function loop() has no return typehint specified.
    +11: Function loop() has parameter $foo with no value type specified in iterable type array.
     13: Variable $undefined might not be defined.
    Full report
    Line Error
    3 Function noloop() has no return typehint specified.
    3 Function noloop() has parameter $foo with no value type specified in iterable type array.
    4 Undefined variable: $undefined
    11 Function loop() has no return typehint specified.
    11 Function loop() has parameter $foo with no value type specified in iterable type array.
    13 Variable $undefined might not be defined.
  2. phpstan-bot commented on Sep 16, 2021

    @phpstan-bot
    Collaborator

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

    @@ @@
    + 3: Function noloop() has no return type specified.
    + 3: Function noloop() has parameter $foo with no value type specified in iterable type array.
      4: Undefined variable: $undefined
    +11: Function loop() has no return type specified.
    +11: Function loop() has parameter $foo with no value type specified in iterable type array.
     13: Variable $undefined might not be defined.
    Full report
    Line Error
    3 Function noloop() has no return type specified.
    3 Function noloop() has parameter $foo with no value type specified in iterable type array.
    4 Undefined variable: $undefined
    11 Function loop() has no return type specified.
    11 Function loop() has parameter $foo with no value type specified in iterable type array.
    13 Variable $undefined might not be defined.
  3. 13 remaining items

  4. github-actions commented on Aug 29, 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.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions