Skip to content

False negative with array_push on property #2888

@muglug

Description

@muglug
<?php

namespace Test;

class MyClass
{
    /**
     * @var int[]
     */
    private $prop = [];

    /**
     * @return void
     */
    public function foo()
    {
        array_push($this->prop, 'string');
        array_unshift($this->prop, 'string');
    }

    /**
     * @return void
     */
    public function bar()
    {
        $this->prop[] = 'string';
    }
}

https://phpstan.org/r/7935ce10-c26f-4deb-bf05-5a40751a2a79

Expected:

+---------------------------------------------------------+
| Line | test.php                                         |
+---------------------------------------------------------+
| 17   | Array (array<int>) does not accept string.       |
| 18   | Array (array<int>) does not accept string.       |
| 26   | Array (array<int>) does not accept string.       |
+---------------------------------------------------------+

Actual:

+---------------------------------------------------------+
| Line | test.php                                         |
+---------------------------------------------------------+
| 26   | Array (array<int>) does not accept string.       |
+---------------------------------------------------------+

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