psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Class constants referenced from traits report overlapping & conflicting errors

Open adamaveray opened this issue 11 months ago • 1 comments

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

adamaveray avatar Feb 28 '24 13:02 adamaveray

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

psalm-github-bot[bot] avatar Feb 28 '24 13:02 psalm-github-bot[bot]