psalm
psalm copied to clipboard
false positive PossiblyNullPropertyFetch?
see code example: https://psalm.dev/r/960a481894
shouldn't the fact that $compared
is true
not infer that $visitorUser
is not null?
I found these snippets:
https://psalm.dev/r/960a481894
<?php
class User {
public function __construct(
public ?int $id = null,
) {
}
}
class Visitor {
public function __construct(
public ?User $user = null,
) {
}
}
class Employee {
public function __construct(
public ?User $user = null,
) {
}
}
class Foo
{
public function __construct(
public Visitor $visitor,
public Employee $employee,
) {
}
public function compare(): mixed
{
$visitorUser = $this->visitor->user;
$employeeUser = $this->employee->user;
$compared = $visitorUser !== null && $employeeUser === $visitorUser;
if ($compared === false) {
return 0;
}
return $visitorUser->id;
}
}
Psalm output (using commit cb0e6a1):
ERROR: PossiblyNullPropertyFetch - 39:16 - Cannot get property on possibly null variable $visitorUser of type User|null
This works: https://psalm.dev/r/235b00ed02
I found these snippets:
https://psalm.dev/r/235b00ed02
<?php
class User {
public function __construct(
public ?int $id = null,
) {
}
}
class Visitor {
public function __construct(
public ?User $user = null,
) {
}
}
class Employee {
public function __construct(
public ?User $user = null,
) {
}
}
class Foo
{
public function __construct(
public Visitor $visitor,
public Employee $employee,
) {
}
public function compare(): mixed
{
$visitorUser = $this->visitor->user;
$employeeUser = $this->employee->user;
$compared = $visitorUser !== null && $employeeUser === $visitorUser;
if ($compared) {
return $visitorUser->id;
}
return 0;
}
}
Psalm output (using commit cb0e6a1):
No issues!