Skip to content

Cannot find null pointer with array index access #2112

@eigan

Description

@eigan

Bug report

When a method have a return type of array will PHPStan not be able to find any potenial null pointers.

Using @return Foo[] on getFoos() will resolve all issues in the snippet.

Code snippet that reproduces the problem

https://phpstan.org/r/ec493869-6fbb-4511-99f7-3de3630c6297

Expected output

+-----------------------------------------------------------------------------+
| Line | test.php                                                             |
+-----------------------------------------------------------------------------+
| 19   | Parameter #1 $foo of function useTheFoo expects Foo, mixed|null given. |
| 22   | Parameter #1 (mixed|null) of echo cannot be converted to string.       |
| 25   | Parameter #1 $foo of function useTheFoo expects Foo, mixed|null given. |
+-----------------------------------------------------------------------------+

Edit: Updated the expected output, it said "Foo|null given". But PHPStan have no way of knowing it was Foo returned from getFoos(), changed to mixed|null

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