Skip to content

False positives on array access on iterable property with PHPDoc type that allows it #6243

@julienfalque

Description

@julienfalque

Bug report

Given a property with native type iterable and PHPDoc type that allows array access, e.g. array, reading or writing to this property using array access is reported as an error.

Code snippet that reproduces the problem

<?php

class Foo
{
    /** @var list<string>|(\ArrayAccess<int, string>&iterable<int, string>) */
    private iterable $values;

    /**
     * @param list<string> $values
     */
    public function update(array $values): void {
        foreach ($this->values as $key => $_) {
            unset($this->values[$key]);
        }

        foreach ($values as $value) {
            $this->values[] = $value;
        }
    }
}

PHPStan reports the following errors:

13 | Cannot access offset int on (ArrayAccess<int, string>&iterable<int, string>)\|iterable<int, string>.
-- | --
17 | Cannot access an offset on iterable<int, string>.

https://phpstan.org/r/42af72fd-760d-4132-8e1b-14449f2efe59

Expected output

No errors

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