Skip to content

Invalid array index detection #8222

@theofidry

Description

@theofidry

Bug report

For some downstream code, I am constraining a value to be positive-int|0. In the upstream code, this comes from an array for which the only operations are appending a value ($array[] = $value) and removing a value (unset($array[$index])).

However PHPStan doesn't appear to like that very much as it infers int for the index instead of positive-int|0, which is what it is in this specific case where the index is never explicitly set.

Code snippet that reproduces the problem

https://phpstan.org/r/b27d9595-2fb0-4e53-bae6-66728a05a53c

Expected output

No error reported.

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

First PHPStan issue I believe so it works :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions