Skip to content

False positive with array_key_first and reportPossiblyNonexistentGeneralArrayOffset #13546

@SplotyCode

Description

@SplotyCode

Bug report

public static function first(array $array): mixed    {
  $key = array_key_first($array);
  return $key !== null ? $array[$key] : null; /** @phpstan-ignore offsetAccess.notFound */
}

i feel like the root cause here is that phpstan does not infer that a non null array_key_first implies that the array/list is non-empty.
https://phpstan.org/r/6534e052-27c3-4b69-9090-bb37c99971ba

Code snippet that reproduces the problem

https://phpstan.org/r/b4e4961e-3c1e-47a6-b80b-ee7a7413dd98

Expected output

Key always exists as its the output of array_key_first

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