Skip to content

False positive about empty array with foreach loop #15147

Closed
phpstan/phpstan-src
#6605

Description

@franck-paul

Bug report

I have an empty ArrayObject, which can be populated (or not) by a called method and then I get the array to loop on it as described in snippet.

PHPStan signals a foreach.emptyArray error which I think is not relevant here.

Am I wrong?

Code snippet that reproduces the problem

https://phpstan.org/r/45eca57f-0a03-493a-984e-bf4c7ec33584

Expected output

No error reported as the array might be not empty

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

No response

Activity

  1. ondrejmirtes commented on Sep 1, 2026

    @ondrejmirtes
    Member

    Yes, the knowledge what $alias is should definitely update after $this->populate($alias); Because it's an object passed by reference and this usually works fine in PHPStan (at least it invalidates remembered method calls on the object, but probably doesn't change the generics).

    Generics should change probably only if the initial object is empty (has NeverType templates). Otherwise it could lead to some unsound code.

  2. added this to the Easy fixes milestone on Sep 1, 2026
  3. added a commit that references this issue on Sep 26, 2026
  4. added a commit that references this issue on Sep 26, 2026
  5. franck-paul commented on Oct 27, 2026

    @franck-paul
    Author

    Is the Playground using the last release or "last-dev-commit" of PHPStan?

    As in my example (https://phpstan.org/r/45eca57f-0a03-493a-984e-bf4c7ec33584) I still have the error.

  6. staabm commented on Oct 27, 2026

    @staabm
    Contributor

    the improved behaviour (the fixed bug) is only visible with enabled bleeding-edge for now: https://phpstan.org/r/42c879e8-2a76-4737-8395-ff78a3d9e441

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