Skip to content

Bug: Default Array keys aren't detected to be non-negative #8900

@Leoniebra

Description

@Leoniebra

Bug report

Hello!

I noticed that phpstan 1.9.17 treats the keys of arrays that are initialized empty and filled via $array[] = $value as possibly negative integers when using foreach to iterate through the array.
(For me it occurred with an array generated by the TYPO3 Extbase ObjectStorage ->toArray() method). This led to a match expression on which phpstan required more cases than I expected to be necessary.

Code snippet that reproduces the problem

I tried reproducing the issue here. Declaring the key within the foreaach loop as a positive-int was the best approach to a solution I found, sadly it didn't work either tho.

https://phpstan.org/r/1ee9ce43-4954-4188-9ac8-6f780f9223dd

Expected output

I expect this snippet to generate no issues, for I am sure that the keys of this array are always greater than or equal to 0.

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

It sure did! While discovering this issue, I also found a heap of correctly reported problems within our code. I'm creating this issue because I enjoy using this tool a lot :)

Thanks in advance everyone!

Best,
Leo (it / she)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions