psalm icon indicating copy to clipboard operation
psalm copied to clipboard

False-positive `PropertyTypeCoercion` for `static` in trait if used in final class

Open gharlan opened this issue 11 months ago • 1 comments

https://psalm.dev/r/2cb05e09b4

The trait can be used in non-final and final classes, so I can't change static to self. There should be no error in final class Baz.

gharlan avatar Mar 16 '24 11:03 gharlan

I found these snippets:

https://psalm.dev/r/2cb05e09b4
<?php

trait Foo {
    /** @var static|null */
    private static $instance;
    
    static function foo(): void {
    	self::$instance = new static();
    }
}

/** @psalm-consistent-constructor */
class Bar {
    use Foo;
}

final class Baz {
    use Foo;
}
Psalm output (using commit ef3b018):

ERROR: PropertyTypeCoercion - 8:24 - Baz::$instance expects 'Baz&static|null',  parent type 'Baz' provided

psalm-github-bot[bot] avatar Mar 16 '24 11:03 psalm-github-bot[bot]