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

TypeDoesNotContainType false positive #4274

Closed
morozov opened this issue Oct 2, 2020 · 3 comments
Closed

TypeDoesNotContainType false positive #4274

morozov opened this issue Oct 2, 2020 · 3 comments
Labels

Comments

@morozov
Copy link
Contributor

morozov commented Oct 2, 2020

Please see the code example in https://psalm.dev/r/fe40e86258.

Depending on the values of $flag and $options, a call to array_shift() may return a non-null value but this code is reported as:

Type null for $length is always null

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/fe40e86258
<?php

/**
 * @param mixed[] $options
 */
function doStuff(bool $flag, array $values, array $options): void
{
    $lengths = $flag ? $options['lengths'] : [];

    foreach ($values as $_) {
        $length = array_shift($lengths);

        if ($length !== null) {
            echo 1;
        }
    }
}
Psalm output (using commit f55e5ea):

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

INFO: MixedAssignment - 8:5 - Unable to determine the type that $lengths is being assigned to

INFO: MixedArgument - 11:31 - Argument 1 of array_shift cannot be array<empty, empty>|mixed, expecting array<array-key, mixed>

ERROR: TypeDoesNotContainType - 13:13 - Type null for $length is always null

@weirdan
Copy link
Collaborator

weirdan commented Oct 2, 2020

Simplified: https://psalm.dev/r/03258c2b11

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/03258c2b11
<?php

/**
 * @param mixed $options
 * @psalm-suppress MixedAssignment,MixedArgument
 */
function doStuff($options): void
{
    $options = rand(0,1) ? $options : [];
    $opt = array_shift($options);
    if (null === $opt) {}
}
Psalm output (using commit 211553c):

ERROR: RedundantCondition - 11:9 - Type null for $opt is always null

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