psalm
psalm copied to clipboard
Support for null-safe operators
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
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!