Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find null pointer with array index access #2112

Closed
eigan opened this issue May 7, 2019 · 7 comments
Closed

Cannot find null pointer with array index access #2112

eigan opened this issue May 7, 2019 · 7 comments

Comments

@eigan
Copy link

eigan commented May 7, 2019

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

@adaamz
Copy link
Contributor

adaamz commented May 8, 2019

Mixed is mixed, it means that mixed can be passed everywhere.

btw there is waiting PR that will introduce new rule that checks whether you have always fully typed iterables phpstan/phpstan-strict-rules#70

@ondrejmirtes
Copy link
Member

ondrejmirtes commented May 8, 2019

I think there's another open issue about this. Basically, we need to add value type to HasOffsetType accessory type - specify the value type of this key after assignment.

@ondrejmirtes ondrejmirtes added this to the 0.12 milestone May 9, 2019
@ondrejmirtes ondrejmirtes removed this from the 0.12 milestone Oct 28, 2019
@eigan
Copy link
Author

eigan commented Jan 18, 2020

Another related case I got hit by today with level 4:
https://phpstan.org/r/2af4459a-caae-45dc-bbd7-c41164eb3d0e

It's a bit weird that isset() doesn't give the same error.

EDIT: Using array<int, FooBar|null> solves the issue, but shouldnt PHPStan always assume that it might not be set?

@ondrejmirtes
Copy link
Member

@eigan This is completely unrelated bug, already reported at #2816.

@eigan
Copy link
Author

eigan commented Jan 19, 2020

Hm, okay? Doesnt look related to me. This issue is about PHPStan assuming a value at some index is never NULL. If it was related to the one you linked, then moving the isset (or removing) should affect the report(?), but it doesnt.

https://phpstan.org/r/8d999363-5ea6-41ad-93c8-7f00ce192a33
Here is another case: array_key_exists is true so there is something at the index. PHPStan now insist that the isset call is not needed. But the value can still be NULL.

... But I don't know the internals of these - so they might be related anyhow. Sorry if this causes noise for you.

@ondrejmirtes
Copy link
Member

@eigan It's still unrelated to #2112. But I was mistaken. It's still reported even without the isset (https://phpstan.org/r/842c1e02-c375-4300-bd36-1bd2bf360ce8), but it's a feature. Since the array contains objects, if ($this->foo[$index]) will be always true, or it will crash. PHPStan deliberately does not report possibly nonexistent array offsets on general arrays because it'd be too annoying in practice.

This example https://phpstan.org/r/8d999363-5ea6-41ad-93c8-7f00ce192a33 looks like a real bug so please open a new issue for that.

I'm locking this thread so that it doesn't veer off from the OP even more.

@phpstan phpstan locked as off-topic and limited conversation to collaborators Jan 19, 2020
@ondrejmirtes
Copy link
Member

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants