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

Accessing undefined array key could result to null #3924

Closed
davidbrnovjak opened this issue Oct 7, 2020 · 2 comments
Closed

Accessing undefined array key could result to null #3924

davidbrnovjak opened this issue Oct 7, 2020 · 2 comments

Comments

@davidbrnovjak
Copy link

davidbrnovjak commented Oct 7, 2020

Bug report

When accessing annotated array it is possible to get null as a result when the key is undefined.
Currently no error is emitted and even checking if result is null is considered as an error (comparsion is always false).

(Sorry if duplicate, I really tried to search hard this time. This might be related #3126 )

Code snippet that reproduces the problem

https://phpstan.org/r/aedb5786-c0ba-4fc8-b311-f2a1c150f6c8

/** @var string[] */
$arr = ['a', 'b'];

$value = $arr[5];
if ($value === null) { // comparsion is always false emitted here
}

Expected output

Phpstan should treat accessing array by index as possible null.

@davidbrnovjak davidbrnovjak changed the title Accessing undefined array key should result to null Accessing undefined array key could result to null Oct 7, 2020
@ondrejmirtes
Copy link
Member

Hi, this isn't considered because you should check that the key exists using array_key_exists or isset. Accessing an undefined array key generates a warning, you should log notices and warnings in production and fix them so that your error logs are clean.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants