psalm icon indicating copy to clipboard operation
psalm copied to clipboard

DocblockTypeContradiction after first value-of<Enum> @property test

Open flaviovs opened this issue 1 year ago • 2 comments

https://psalm.dev/r/d629b4ca4e

flaviovs avatar Jun 29 '24 01:06 flaviovs

I found these snippets:

https://psalm.dev/r/d629b4ca4e
<?php

enum Foo: string {
    case Bar = 'bar';
    case Lee = 'lee';
};

/** @property value-of<Foo> $attr */
class Bar
{
    public function __set(string $name, mixed $value) {}
    public function __get(string $name): mixed { return null; }    
    
    public function doSomething(): void
    {
        if ($this->attr === Foo::Bar->value) {
            echo 'Is Bar';
            return;
        }

        if ($this->attr === Foo::Lee->value) {
            echo 'Is Lee';
            return;
        }
	}
}
Psalm output (using commit 16b24bd):

ERROR: DocblockTypeContradiction - 21:13 - 'lee' does not contain value-of<Foo>

ERROR: DocblockTypeContradiction - 21:13 - Docblock-defined type value-of<Foo> for $this->attr is never =string(lee)

psalm-github-bot[bot] avatar Jun 29 '24 01:06 psalm-github-bot[bot]

Hope this info is useful: just noticed that replacing the second if with elseif makes the warning go away.

flaviovs avatar Jun 29 '24 01:06 flaviovs