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

PossiblyNullPropertyFetch after check with nullsafe operator #5873

Open
Syzgyn opened this issue Jun 1, 2021 · 6 comments
Open

PossiblyNullPropertyFetch after check with nullsafe operator #5873

Syzgyn opened this issue Jun 1, 2021 · 6 comments

Comments

@Syzgyn
Copy link

Syzgyn commented Jun 1, 2021

https://psalm.dev/r/2d78032069

I could be wrong, but it seems to me that if you check for not-null against something using a null safe operator, then it should validate the entire chain as not null, but that's not what happens.

For example assert($a->b?->c?->d?->e != null) should mean that a through e are all not null, correct? Otherwise the assert would fail.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/2d78032069
<?php

class a
{
    public ?b $b = null;
}

class b
{
    public ?int $c = null;
}

function test(a $a): void
{
	// This breaks...
    if ($a->b?->c != null) {
        echo $a->b->c;
    }
    
    // ...but is equivalent to this, right?
    if ($a->b != null && $a->b->c != null) {
    	echo $a->b->c;
	}
}
Psalm output (using commit f3fc112):

ERROR: PossiblyNullPropertyFetch - 17:14 - Cannot get property on possibly null variable $a->b of type b|null

@weirdan weirdan added the bug label Jun 2, 2021
@signor-pedro
Copy link

Experiencing the same.

Here is a snippet:

if ($criteria?->publishedAfter) {
    $queryBuilder->setParameter('publishedAfter', $criteria->publishedAfter);
}

This should be type-safe I guess?

@tm1000
Copy link
Contributor

tm1000 commented Mar 24, 2022

Another useful example: https://psalm.dev/r/86ffcdda1f

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/86ffcdda1f
<?php

function foo(): void {
    /** @var object|null */
    $foo = new stdClass();
    if($foo?->bar === null) {
		return;
	}
    
    /** @psalm-trace $foo */
}
Psalm output (using commit 5baf85e):

INFO: Trace - 10:0 - $foo: null|object

@SCIF
Copy link
Contributor

SCIF commented Jul 1, 2022

Hi @tm1000 , @Syzgyn ,

Looks like this could be fixed in master branch. At least it does for me.

@tm1000
Copy link
Contributor

tm1000 commented Jul 30, 2022

@SCIF the two snippets still fail in the same way so this doesn't seem resolved

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

No branches or pull requests

6 participants