You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<?phpdeclare(strict_types=1);
interfaceComparable {
/** * @template T of mixed * * @param T $compareTo * * If the param is the same class as $this, then it's possible it will be true (but it could still be false) * If the param is a different class from $this, even if it implements Comparable, then it's impossible for it to be true. * * @return (T is static ? bool : false) */publicfunctioncompare(mixed$compareTo) : bool;
}
classFooimplementsComparable {
/** * @template T of mixed * @param T $compareTo * @return (T is static ? bool : false) */publicfunctioncompare(mixed$compareTo) : bool
{
return$this == $compareTo;
}
}
classBarimplementsComparable {
/** * @template T of mixed * @param T $compareTo * @return (T is static ? bool : false) */publicfunctioncompare(mixed$compareTo) : bool
{
return$this == $compareTo;
}
}
$a = newFoo();
$b = newBar();
/** @psalm-trace $ab */// these are different classes, so we should be able to infer it's false$ab = $a->compare($b);
/** @psalm-trace $ba */// these are different classes, so we should be able to infer it's false$ba = $b->compare($a);
/** @psalm-trace $aa */// these the same class, so we should infer bool as it could be either$aa = $a->compare($a);
Psalm output (using commit f8d1dd8):
INFO: Trace - 45:1 - $ab: false
INFO: Trace - 48:1 - $ba: false
INFO: Trace - 51:1 - $aa: false
INFO: UnusedVariable - 45:1 - $ab is never referenced or the value is not used
INFO: UnusedVariable - 48:1 - $ba is never referenced or the value is not used
INFO: UnusedVariable - 51:1 - $aa is never referenced or the value is not used
tdtm
changed the title
Using static in conditional return
Using static in conditional return
Oct 13, 2022
Should
@return (T is static ? bool : false)
work? Here anything I try seems to evaluateT is static
as false.https://psalm.dev/r/59c4ae8585
As comparison, in PhpStan, it does work: https://phpstan.org/r/1048dce8-8d8c-47ac-86e6-383ef17984d7
The text was updated successfully, but these errors were encountered: