Skip to content

Unable to infer type when modifying an array #1311

@TRowbotham

Description

@TRowbotham

Summary of a problem or a feature request

PHPStan is unable to infer the type of an array's values after it has been modified. I suspect this might be a difficult case to solve.

Code snippet that reproduces the problem

Both a foreach by reference and an ordinary for loop report an error.
https://phpstan.org/r/da003dee5a95afa552994b8f36ac3512 (foreach by reference)
https://phpstan.org/r/1179579fc016f23eff3768d5b5886ae2 (for loop)

Using array_map() doesn't report an error.
https://phpstan.org/r/6f6886451efa8231aa7d24d5789aa2d5

Expected output

PHPStan should not emit an error when the array is modified to contain the correct value type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions