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

Wrong TypeDoesNotContainNull determination #9894

Closed
chriskapp opened this issue Jun 11, 2023 · 5 comments
Closed

Wrong TypeDoesNotContainNull determination #9894

chriskapp opened this issue Jun 11, 2023 · 5 comments
Labels

Comments

@chriskapp
Copy link

Hi, I have found also another case where Psalm thinks that in the example the $existing variable does not contain null s.

https://psalm.dev/r/74b5ec5ba5

But it could be null in case $foo is 1 and $bar is null. It looks like Psalm does not recognize the elseif and thinks that there is a standalone if which would indeed remove the null type.

Maybe this is also related to #7099

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/74b5ec5ba5
<?php

function getFoo(): ?int {
    return rand(0, 1) === 1 ? 1 : null;
}

function test(?stdClass $existing = null): void {
    $foo = getFoo();
    $bar = getFoo();

	/** @psalm-trace $existing */;
    
    if ($foo !== null) {
        assert($foo === 1);
    } elseif ($existing === null) {
        throw new \RuntimeException('foo');
    }

	/** @psalm-trace $existing */;
    
    if ($bar !== null) {
        assert($bar === 1);
    } elseif ($existing === null) {
        throw new \RuntimeException('bar');
    }
}

test(null);
Psalm output (using commit b6cba5a):

INFO: Trace - 11:31 - $existing: null|stdClass

INFO: Trace - 19:31 - $existing: stdClass

ERROR: TypeDoesNotContainNull - 23:15 - stdClass does not contain null

@chriskapp
Copy link
Author

If the elseif block is moved into a separate if inside an else it works like expected s.
https://psalm.dev/r/d274855806

@psalm-github-bot
Copy link

I found these snippets:

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

function getFoo(): ?int {
    return rand(0, 1) === 1 ? 1 : null;
}

function test(?stdClass $existing = null): void {
    $foo = getFoo();
    $bar = getFoo();

	/** @psalm-trace $existing */;
    
    if ($foo !== null) {
        assert($foo === 1);
    } else {
    	if ($existing === null) {
    	    throw new \RuntimeException('foo');
	    }
    }

	/** @psalm-trace $existing */;
    
    if ($bar !== null) {
        assert($bar === 1);
    } elseif ($existing === null) {
        throw new \RuntimeException('bar');
    }
}

test(null);
Psalm output (using commit b6cba5a):

INFO: Trace - 11:31 - $existing: null|stdClass

INFO: Trace - 21:31 - $existing: null|stdClass

@theodorejb
Copy link
Contributor

Is this issue still relevant? It looks like it was fixed.

@weirdan
Copy link
Collaborator

weirdan commented Jan 30, 2024

I believe this is fixed in #9922

@weirdan weirdan closed this as completed Jan 30, 2024
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

4 participants