Skip to content

Improve the return type of array_map() for constant arrays #11180

Open
@zonuexe

Description

@zonuexe

Feature request

When the input to the array_map() function is a constant array, it is useful for the elements of the return value to be typed with a constant as well.

$a = range(0, 1);
assertType("array{'0', '1'}", array_map('strval', $a));
// actual: array{string, string}
assertType("array{'0', '1'}", array_map(strval(...), $a));
// actual: array{string, string}
assertType("array{'0', '1'}", array_map(fn ($v) => strval($v), $a));
// actual: array{'0'|'1', '0'|'1'}
assertType("array{'0', '1'}", array_map(fn ($v) => (string)$v, $a));
// actual: array{'0'|'1', '0'|'1'}

https://phpstan.org/r/ab228eea-8143-4090-8cca-00bbc28b9d59

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

No response

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