Skip to content

PHPStan should complain about using a non-array-key as an array key #7884

@MidnightDesign

Description

@MidnightDesign

Feature request

I just spent a couple of hours on this, and PHPStan didn't save me. My code looks something like this:

/** @var array<array-key, mixed> */
$myArr = [ ... ];
$k = array_search('myneedle', $myArr, true);
...
unset($myArr[$k]);

The problem was that I was unsetting $myArr[0] when array_search() returned false. PHPStan also doesn't complain about $myArr[42.69]. I think anything but strings and integers should be disallowed as array offsets.

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