psalm icon indicating copy to clipboard operation
psalm copied to clipboard

PossiblyNullReference false-positive

Open fluffycondor opened this issue 5 years ago • 15 comments

https://psalm.dev/r/6cfd65652c

Expected: no errors. Got: PossiblyNullReference

Something really unexplainable is going on here 🤔 Psalm correctly deducts that Foo::getDate() returns only DateTimeImmutable in this branch, but on the very next line it threats the result as DateTimeImmutable|null. The class Foo is marked as immutable as well.

fluffycondor avatar Jun 22 '21 12:06 fluffycondor

I found these snippets:

https://psalm.dev/r/6cfd65652c
<?php

/**
 * @psalm-immutable
 */
class Foo {
    private ?DateTimeImmutable $date = null;
    
    public function getDate(): ?DateTimeImmutable
    {
        return $this->date;
    }
}

class Bar {
    public ?DateTimeImmutable $date;
    public ?string $timezone;
    
    public function __construct(Foo $foo, ?DateTimeImmutable $date) {
        if (is_null($foo->getDate())) {
            $this->date = $date;
            $this->timezone = !is_null($date) ? $date->getTimezone()->getName() : null;
        } else {
            $this->date = $foo->getDate();
            /** @psalm-trace $this->date */;
            $this->timezone = $foo->getDate()->getTimezone()->getName();
        }
    }
}
Psalm output (using commit 6d4d166):

INFO: Trace - 25:44 - $this->date: DateTimeImmutable

ERROR: PossiblyNullReference - 26:48 - Cannot call method getTimezone on possibly null value

psalm-github-bot[bot] avatar Jun 22 '21 12:06 psalm-github-bot[bot]

Seems to be constructor-specific: https://psalm.dev/r/5ced18e821

weirdan avatar Jun 22 '21 12:06 weirdan

I found these snippets:

https://psalm.dev/r/5ced18e821
<?php

/**
 * @psalm-immutable
 */
class Foo {
    private ?DateTimeImmutable $date = null;
    
    public function getDate(): ?DateTimeImmutable
    {
        return $this->date;
    }
}

class Bar {
    public DateTimeImmutable $date2;
    
    public function __construct(Foo $foo) {
        if (null !== $foo->getDate()) {  
            $this->date2 = $foo->getDate();
        }
        $this->date2 = new DateTimeImmutable();
    }
    
    public function foo(Foo $foo): void {
        if (null !== $foo->getDate()) {  
            $this->date2 = $foo->getDate();
        }
        $this->date2 = new DateTimeImmutable();   
    }
}
Psalm output (using commit 6d4d166):

ERROR: PossiblyNullPropertyAssignmentValue - 20:28 - $this->date2 with non-nullable declared type 'DateTimeImmutable' cannot be assigned nullable type 'DateTimeImmutable|null'

psalm-github-bot[bot] avatar Jun 22 '21 12:06 psalm-github-bot[bot]

@weirdan we have the same problem, $this->logger is a LoggerInterface or null as a property, the constructor then sets it to be an instance of NullLogger from the psr package, however we still get a possibly null value response.

marcrobertscamao avatar Sep 01 '21 15:09 marcrobertscamao

I can replicate this simply in this snippet: https://psalm.dev/r/769852b21a

marcrobertscamao avatar Sep 01 '21 15:09 marcrobertscamao

I found these snippets:

https://psalm.dev/r/769852b21a
<?php

interface LoggerInterface {
    public function log():void;
}

class NullLogger implements LoggerInterface {
	public function log():void {}
}

trait Loggable {
    /**
     * @var LoggerInterface|null
     */
    protected $logger = null;
    
    public function setLogger(LoggerInterface $logger): void
    {
        $this->logger = $logger;
    }
}

class A {
    use Loggable;
    
    public function __constructor()
    {
        $this->logger = new NullLogger();
    }
    
    public function run():void {
        $this->logger->log();
    }
}
Psalm output (using commit eb973ab):

ERROR: PossiblyNullReference - 32:24 - Cannot call method log on possibly null value

psalm-github-bot[bot] avatar Sep 01 '21 15:09 psalm-github-bot[bot]

I think it is not constructor related as it also happens in other functions: https://psalm.dev/r/db552ea25f

MisatoTremor avatar Jun 14 '22 12:06 MisatoTremor

I found these snippets:

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

class A {
	private B|null $b = null;

    public function getB(): B|null
    {
        return $this->b;
    }

    public function bar(): void
    {
        if (null !== $this->getB())
        {
            echo $this->getB()->getFoo();
        }
    }
}

class B {
    private string $foo = 'foo';

    public function getFoo(): string
    {
        return $this->foo;
    }
}
Psalm output (using commit 10ea05a):

ERROR: PossiblyNullReference - 15:33 - Cannot call method getFoo on possibly null value

psalm-github-bot[bot] avatar Jun 14 '22 12:06 psalm-github-bot[bot]

@MisatoTremor your snippet is indeed error prone. Nothing exclude errors if the getter is not pure: https://psalm.dev/r/96c247415c

orklah avatar Jun 14 '22 13:06 orklah

I found these snippets:

https://psalm.dev/r/96c247415c
<?php

class A {
	private B|null $b = null;

    public function getB(): B|null
    {
        return rand(0,1) ? null : $this->b;
    }

    public function bar(): void
    {
        if (null !== $this->getB())
        {
            echo $this->getB()->getFoo();
        }
    }
}

class B {
    private string $foo = 'foo';

    public function getFoo(): string
    {
        return $this->foo;
    }
}
Psalm output (using commit 10ea05a):

ERROR: PossiblyNullReference - 15:33 - Cannot call method getFoo on possibly null value

psalm-github-bot[bot] avatar Jun 14 '22 13:06 psalm-github-bot[bot]

This example from the original issue has gotten stranger, the trace is happening twice with different types...

AndrolGenhald avatar Jun 14 '22 14:06 AndrolGenhald

I found these snippets:

https://psalm.dev/r/6cfd65652c
<?php

/**
 * @psalm-immutable
 */
class Foo {
    private ?DateTimeImmutable $date = null;
    
    public function getDate(): ?DateTimeImmutable
    {
        return $this->date;
    }
}

class Bar {
    public ?DateTimeImmutable $date;
    public ?string $timezone;
    
    public function __construct(Foo $foo, ?DateTimeImmutable $date) {
        if (is_null($foo->getDate())) {
            $this->date = $date;
            $this->timezone = !is_null($date) ? $date->getTimezone()->getName() : null;
        } else {
            $this->date = $foo->getDate();
            /** @psalm-trace $this->date */;
            $this->timezone = $foo->getDate()->getTimezone()->getName();
        }
    }
}
Psalm output (using commit 10ea05a):

INFO: Trace - 25:44 - $this->date: DateTimeImmutable

INFO: Trace - 25:44 - $this->date: DateTimeImmutable|null

ERROR: PossiblyNullReference - 26:48 - Cannot call method getTimezone on possibly null value

psalm-github-bot[bot] avatar Jun 14 '22 14:06 psalm-github-bot[bot]

Could this issue be related to my issue?

https://github.com/vimeo/psalm/issues/8548

ghost avatar Oct 07 '22 14:10 ghost

@MisatoTremor your snippet is indeed error prone. Nothing exclude errors if the getter is not pure: https://psalm.dev/r/96c247415c

Thanks, now I get it! But that applies to the original issue as well, as it follows the same principle. So this is not a false-positive at all

MisatoTremor avatar Oct 14 '22 11:10 MisatoTremor

I found these snippets:

https://psalm.dev/r/96c247415c
<?php

class A {
	private B|null $b = null;

    public function getB(): B|null
    {
        return rand(0,1) ? null : $this->b;
    }

    public function bar(): void
    {
        if (null !== $this->getB())
        {
            echo $this->getB()->getFoo();
        }
    }
}

class B {
    private string $foo = 'foo';

    public function getFoo(): string
    {
        return $this->foo;
    }
}
Psalm output (using commit e7f05c3):

ERROR: PossiblyNullReference - 15:33 - Cannot call method getFoo on possibly null value

psalm-github-bot[bot] avatar Oct 14 '22 11:10 psalm-github-bot[bot]