psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Support for null-safe operators

Open pavelsklyar opened this issue 11 months ago • 1 comments

Hello!

I found an error when using the null-safe operator.

Code with error: https://psalm.dev/r/35e6ae5c78

Code without error: https://psalm.dev/r/e78beca1b1

pavelsklyar avatar Mar 01 '24 11:03 pavelsklyar

I found these snippets:

https://psalm.dev/r/35e6ae5c78
<?php

class O
{
	public function __construct(
        public int $type
    ) {}
}

function get(): O
{
	$o = rand(0, 1) === 0 ? null : new O(1);
    
    if ($o?->type === 10) {
        return $o;
    }
    
    throw new \DomainException();
}
Psalm output (using commit b940c7e):

ERROR: NullableReturnStatement - 15:16 - The declared return type 'O' for get is not nullable, but the function returns 'O|null'

ERROR: InvalidNullableReturnType - 10:17 - The declared return type 'O' for get is not nullable, but 'O|null' contains null
https://psalm.dev/r/e78beca1b1
<?php

class O
{
	public function __construct(
        public int $type
    ) {}
}

function get(): O
{
	$o = rand(0, 1) === 0 ? null : new O(1);
    
    if ($o !== null && $o->type === 10) {
        return $o;
    }
    
    throw new \DomainException();
}
Psalm output (using commit b940c7e):

No issues!

psalm-github-bot[bot] avatar Mar 01 '24 11:03 psalm-github-bot[bot]