psalm
psalm copied to clipboard
Class constants referenced from traits report overlapping & conflicting errors
A trait referencing a constant with different values in classes that use it causes Psalm to report issues for all the overlapping types. This happens for both self::
and static::
references.
https://psalm.dev/r/71569f75d7
I found these snippets:
https://psalm.dev/r/71569f75d7
<?php
trait TestTrait {
public function testFunction(): void {
if (self::VALUE) {
echo 'True';
} else {
echo 'False';
}
if (static::VALUE) {
echo 'True';
} else {
echo 'False';
}
}
}
final class ClassOne {
use TestTrait;
public const VALUE = true;
}
final class ClassTwo {
use TestTrait;
public const VALUE = false;
}
(new ClassOne())->testFunction(); // Outputs "TrueTrue"
(new ClassTwo())->testFunction(); // Outputs "FalseFalse"
Psalm output (using commit b940c7e):
ERROR: RedundantCondition - 4:13 - Operand of type true is always truthy
ERROR: RedundantCondition - 9:13 - Operand of type true is always truthy
ERROR: TypeDoesNotContainType - 4:13 - Operand of type false is always falsy
ERROR: TypeDoesNotContainType - 9:13 - Operand of type false is always falsy