Skip to content

Cannot assign new offset to something that is clearly an array #8015

@derrabus

Description

@derrabus

Bug report

I'm operating on an array with dynamic keys. When trying to add an item to a nested array right after initializing it, I get an error, telling me that I cannot assign a new offset to that array.

Code snippet that reproduces the problem

https://phpstan.org/r/7989deba-bb1e-4ad0-b070-c6de9bc2e4f3

The crucial part is this piece:

$config['things'] = [];
foreach ($item as $thing) {
    $config['things'][] = (string) $thing;
}

PHPStan complains about the assignment inside the foreach loop. But at this point, $config['things'] is guaranteed to be an array.

Expected output

No error.

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

Only today? PHPStan improves my live as a developer every day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions