Skip to content

Type inferring is incorrect on @template K of array-key, yet it is correct for (almost) the same code with @template T #7279

@gnutix

Description

@gnutix

Bug report

Hello there. While adding some types and types' tests for a bunch of Functional-related functions in our codebase, I noticed some weird behavior.

I have two functions :

  1. find(array $array, mixed $value): mixed (|null being implied)
  2. findKey(array $array, int|string $key): int|string|null

The implementation is strictly the same (as far as PHP code is concerned), yet the produced types are wildly different. I guess it's because of array-key and how it is processed ?

(I don't currently have the time to work on an implementation, but maybe someone will.)

Code snippet that reproduces the problem

https://phpstan.org/r/d979d850-c552-4598-bb72-50ee7cf7d4eb

Expected output

The above code sample should produce the expected types.

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

(I have a bunch of small issues to create, sorry if I don't come up with something new for each of them 🙏 )

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions