Skip to content

Updating an array in foreach is wrongly inferred when strict mode is used. #10922

@VincentLanglet

Description

@VincentLanglet

Bug report

In the following example
https://phpstan.org/r/ec2a3c8e-4b32-443f-81b1-f2ba78836f3e
the array is understood as

Dumped type: array<string, array{foo: string, bar: ''}>

but with strict mode we're getting
https://phpstan.org/r/5a249928-665b-4b87-aff3-9e98b53c7fad

Dumped type: array<string, array{foo: string, bar?: ''}>

we have to update the array to a non-empty-array in order to get the right result
https://phpstan.org/r/0302aab8-0877-413b-9dff-ad41d7a8afcf

Code snippet that reproduces the problem

https://phpstan.org/r/5a249928-665b-4b87-aff3-9e98b53c7fad

Expected output

I expect to get

array<string, array{foo: string, bar: ''}>

even if the array in the foreach is empty.

Because if the array is empty, it's [] and it respect the definition array<string, array{foo: string, bar: ''}>.

This would avoid an error on the following code: https://phpstan.org/r/483aa742-4928-47f0-a0b2-02a07d8a684f

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions