Skip to content

array_keys with > 1 argument returns too-broad type #11928

@ManuelHu

Description

@ManuelHu

Bug report

array_keys would never return a key that is not already part of the (narrowed) key type.

a simple first fix (i suppose): just removing this check: https://github.com/phpstan/phpstan-src/blob/612ee600d58315b23506a8a148b0100597b7661b/src/Type/Php/ArrayKeysFunctionDynamicReturnTypeExtension.php#L30

While this might not fix all cases, it would certainly fix the cases of narrowed array-key types to int or string? (*)

(*) with the known caveat of the internal type conversion of integer strings, of course

Code snippet that reproduces the problem

https://phpstan.org/r/f43ddd65-69db-4d3e-acb4-9d2689e5f5b3

Expected output

8: Dumped type: list<int>
11: Dumped type: list<int>

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