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

Psalm does not recognize check on array key existence #4347

Closed
marcosh opened this issue Oct 16, 2020 · 4 comments
Closed

Psalm does not recognize check on array key existence #4347

marcosh opened this issue Oct 16, 2020 · 4 comments
Labels

Comments

@marcosh
Copy link
Contributor

marcosh commented Oct 16, 2020

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 on baz present, it states that $a['foo'] and $a['bar'] could be undefined

@psalm-github-bot
Copy link

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'];
}
Psalm output (using commit ffe7874):

INFO: PossiblyUndefinedStringArrayOffset - 11:9 - Possibly undefined array offset 'string(foo)' is risky given expected type 'array-key'. Consider using isset beforehand.

INFO: MixedAssignment - 11:2 - Unable to determine the type that $foo is being assigned to

INFO: PossiblyUndefinedStringArrayOffset - 12:12 - Possibly undefined array offset 'string(bar)' is risky given expected type 'array-key'. Consider using isset beforehand.

INFO: MixedAssignment - 12:5 - Unable to determine the type that $bar is being assigned to

INFO: UnusedVariable - 11:2 - Variable $foo is never referenced

INFO: UnusedVariable - 12:5 - Variable $bar is never referenced

@muglug muglug added the bug label Oct 16, 2020
@muglug
Copy link
Collaborator

muglug commented Oct 16, 2020

OK, more clearly: https://psalm.dev/r/01eae6d98e

@psalm-github-bot
Copy link

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'];
}
Psalm output (using commit ffe7874):

INFO: PossiblyUndefinedStringArrayOffset - 22:7 - Possibly undefined array offset 'string(a)' is risky given expected type 'array-key'. Consider using isset beforehand.

INFO: PossiblyUndefinedStringArrayOffset - 23:10 - Possibly undefined array offset 'string(b)' is risky given expected type 'array-key'. Consider using isset beforehand.

@muglug muglug closed this as completed in 083cc29 Oct 16, 2020
@muglug
Copy link
Collaborator

muglug commented Oct 16, 2020

Ah, this was just a good ol' combinatorial explosion issue.

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

No branches or pull requests

2 participants