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
Psalm does not recognize check on array key existence #4347
Labels
Comments
I found these snippets: https://psalm.dev/r/a2bb6bbf43<?php
function foo (array $a): void {
if (!array_key_exists('foo', $a) || !(is_string($a['foo']) || is_null($a['foo'])) ||
!array_key_exists('bar', $a) || !(is_string($a['bar']) || is_null($a['bar'])) ||
!array_key_exists('baz', $a) || !(is_string($a['baz']) || is_null($a['baz']))
) {
throw new \Exception();
}
$foo = $a['foo'];
$bar = $a['bar'];
}
|
OK, more clearly: https://psalm.dev/r/01eae6d98e |
I found these snippets: https://psalm.dev/r/01eae6d98e<?php
function two(array $a): void {
if (!array_key_exists('a', $a) || !(is_string($a['a']) || is_null($a['a'])) ||
!array_key_exists('b', $a) || !(is_string($a['b']) || is_null($a['b']))
) {
throw new \Exception();
}
echo $a['a'];
echo $a['b'];
}
function three(array $a): void {
if (!array_key_exists('a', $a) || !(is_string($a['a']) || is_null($a['a'])) ||
!array_key_exists('b', $a) || !(is_string($a['b']) || is_null($a['b'])) ||
!array_key_exists('c', $a) || !(is_string($a['c']) || is_null($a['c']))
) {
throw new \Exception();
}
echo $a['a'];
echo $a['b'];
}
|
Ah, this was just a good ol' combinatorial explosion issue. |
muglug
added a commit
that referenced
this issue
Oct 19, 2020
danog
pushed a commit
to danog/psalm
that referenced
this issue
Jan 29, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Consider this snippet of code https://psalm.dev/r/a2bb6bbf43
If I remove the checks on
$a['baz']
, then Psalm detects correctly that$a['foo']
and$a['bar']
are present. With the checks onbaz
present, it states that$a['foo']
and$a['bar']
could be undefinedThe text was updated successfully, but these errors were encountered: