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

in_array assertion with dynamic haystack doesn't reconcile needle type to array's value type #6220

Closed
supersmile2009 opened this issue Aug 1, 2021 · 2 comments · Fixed by #6233
Labels

Comments

@supersmile2009
Copy link
Contributor

Example:

function inArrayFromRange(?int $val): int {
    if (!in_array($val, range(0, 4), true)) {
        throw new \Exception();
    }
    
    return $val;
}

Errors:

ERROR: NullableReturnStatement - 29:12 - The declared return type 'int' for inArrayFromRange is not nullable, but the function returns 'int|null'

ERROR: InvalidNullableReturnType - 24:39 - The declared return type 'int' for inArrayFromRange is not nullable, but 'int|null' contains null

I expect $val to be int type after the assertion.
In all the cases I tested with haystack being non-empty-list<T> psalm fails to reconcile the needle type to T after the assertion. It works fine with fixed arrays though (e. g. [0, 1])

More examples:
https://psalm.dev/r/53fed31716

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/53fed31716
<?php

// this one works
function inFixedArray(?int $val): int {
    if (!in_array($val, [0, 1], true)) {
        throw new \Exception();
    }
    
    return $val;
}

// The others don't work
/**
 * @param int $x
 */
function inArrayWithVar(?int $val, $x): int {
    if (in_array($val, [0, 1, $x], true)) {
        throw new \Exception();
    }
    
    return $val;
}

function inArrayFromRange(?int $val): int {
    if (!in_array($val, range(0, 4), true)) {
        throw new \Exception();
    }
    
    return $val;
}

/**
 * @param non-empty-list<int> $x
 */
function inNonEmptyList(?int $val, $x): int {
    if (!in_array($val, $x, true)) {
        throw new \Exception();
    }
    
    return $val;
}

/**
 * @param non-empty-list<string> $x
 */
function inNonEmptyListStr(?string $val, $x): string {
    if (!in_array($val, $x, true)) {
        throw new \Exception();
    }
    
    return $val;
}
Psalm output (using commit 82b3564):

ERROR: NullableReturnStatement - 21:12 - The declared return type 'int' for inArrayWithVar is not nullable, but the function returns 'int|null'

ERROR: InvalidNullableReturnType - 16:41 - The declared return type 'int' for inArrayWithVar is not nullable, but 'int|null' contains null

ERROR: NullableReturnStatement - 29:12 - The declared return type 'int' for inArrayFromRange is not nullable, but the function returns 'int|null'

ERROR: InvalidNullableReturnType - 24:39 - The declared return type 'int' for inArrayFromRange is not nullable, but 'int|null' contains null

ERROR: NullableReturnStatement - 40:12 - The declared return type 'int' for inNonEmptyList is not nullable, but the function returns 'int|null'

ERROR: InvalidNullableReturnType - 35:41 - The declared return type 'int' for inNonEmptyList is not nullable, but 'int|null' contains null

ERROR: NullableReturnStatement - 51:12 - The declared return type 'string' for inNonEmptyListStr is not nullable, but the function returns 'null|string'

ERROR: InvalidNullableReturnType - 46:47 - The declared return type 'string' for inNonEmptyListStr is not nullable, but 'null|string' contains null

@orklah orklah added the bug label Aug 1, 2021
@orklah
Copy link
Collaborator

orklah commented Aug 1, 2021

If you want to take a look, it's handled in AssertionFinder::getInarrayAssertions

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

Successfully merging a pull request may close this issue.

2 participants