Skip to content

Empty array passed to foreach #1826

@dbu

Description

@dbu

Since updating from 0.10.6 to 0.11.1 we get false positives about supposedly empty arrays.

A minimal example to reproduce the problem is this:

<?php

namespace Test;

class Foo
{
    public $bar;

    public function baz(Foo $foo)
    {
        $foo->bar = ['a', 'b'];
    }

    public static function doStuff()
    {
        $foo = new Foo();
        $foo->bar = [];
        $foo->baz($foo);
        // phpstan 0.11.1 reports on this line that $foo->bar is always empty
        foreach ($foo->bar as $value) {
            var_dump($value);
        }
    }
}

IMHO the information that $foo->bar is empty should be discarded when we pass $foo to a method because that method might change the value of the object.

The same is when we call a method on the object itself:

<?php

namespace Test;

class Foo
{
    public $bar;

    public function baz()
    {
        $this->bar = ['a', 'b'];
    }

    public function doStuff()
    {
        $this->bar = [];
        $this->baz();
        // phpstan 0.11.1 reports on this line that $this->bar is always empty
        foreach ($this->bar as $value) {
            var_dump($value);
        }
    }
}

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