Skip to content

constraining array shapes with unions #11300

@pilif

Description

@pilif

Feature request

It would be a great help if it were possible to specify a type describing an array with at least one key present.

For example If I have a type of array<string, int>&array{foo: int}, PHPStan thinks the type can only be array{foo: int} which is one reading of the type I could accept, but then this usage here:

https://phpstan.org/r/fcd288f1-5d16-4e64-9e07-f49a6a06f559

should report the return as an error because of the additional keys.

On the other hand, if you read the union as "any string, int array, but with at least a foo key", then I expect the dumpType in line 10 to continue to keep the full union and not only the more specific part.

As such, I'm a bit unsure whether this is a feature request ("please add a feature to type hint arrays as containing at least one key") or a bug report ("should report an error if too wide a set of keys is present despite a constraining union") (of course, for my case, I prefer the former 😛)

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

Not yet - the day only just started. But I'm pretty sure it's going to as the day progresses.

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