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

Asserting false on result of is_null should remove null from type #3858

Closed
mr-feek opened this issue Jul 21, 2020 · 2 comments
Closed

Asserting false on result of is_null should remove null from type #3858

mr-feek opened this issue Jul 21, 2020 · 2 comments
Labels

Comments

@mr-feek
Copy link
Contributor

mr-feek commented Jul 21, 2020

https://psalm.dev/r/4de47158f1

Per https://github.com/facade/ignition/pull/292/files#r455046472, I believe psalm should be able to infer this.

The expected functionality seems to work for the inverse:

https://psalm.dev/r/afc769579e

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/4de47158f1
<?php

/**
* @param mixed $foo
* @psalm-assert false $foo
*/
function abort_if($foo): void
{
    if ($foo) {
        throw new \RuntimeException();
    }
}

/**
* @param string|null $foo
*/
function removeNullable($foo): string
{
    abort_if(is_null($foo));
    return $foo;
}
Psalm output (using commit 56047dd):

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

ERROR: InvalidNullableReturnType - 17:32 - The declared return type 'string' for removeNullable is not nullable, but 'null|string' contains null
https://psalm.dev/r/afc769579e
<?php

/**
* @param mixed $foo
* @psalm-assert true $foo
*/
function abort_if_false($foo): void
{
    if (!$foo) {
        throw new \RuntimeException();
    }
}

/**
* @param string|null $foo
* @return null
*/
function onlyNullable($foo)
{
    abort_if_false(is_null($foo));
    return $foo;
}
Psalm output (using commit 56047dd):

No issues!

@mr-feek
Copy link
Contributor Author

mr-feek commented Jul 21, 2020

Thanks @muglug !

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