psalm icon indicating copy to clipboard operation
psalm copied to clipboard

False-positive PropertyNotSetInConstructor when nesting traits

Open tvdijen opened this issue 2 years ago • 1 comments

The snippet should speak for itself: https://psalm.dev/r/6ab3830574

tvdijen avatar Aug 21 '22 17:08 tvdijen

I found these snippets:

https://psalm.dev/r/6ab3830574
<?php

declare(strict_types=1);

class MyClass {
    use MyTrait; // When using MySecondaryTrait directly, the error disappears

    public function __construct(string $text)
    {
        $this->setContent($text);
    }
};

trait MyTrait {
    use MySecondaryTrait;
};

trait MySecondaryTrait {
    protected string $content;

    public function setContent(string $text): void
    {
        $this->content = $text;
    }
};
Psalm output (using commit afe85fa):

ERROR: PropertyNotSetInConstructor - 5:7 - Property MyClass::$content is not defined in constructor of MyClass or in any methods called in the constructor

psalm-github-bot[bot] avatar Aug 21 '22 17:08 psalm-github-bot[bot]

I think it doesn't come from the nesting, but from the property of the trait being private vs. protected. I modified the snippet: https://psalm.dev/r/e88b5151b9

ThomasLandauer avatar Oct 17 '22 15:10 ThomasLandauer

I found these snippets:

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

declare(strict_types=1);

class MyClass {
    use MySecondaryTrait;

    public function __construct(string $text)
    {
        $this->setContent($text);
    }
}





trait MySecondaryTrait {
    private string $content;

    public function setContent(string $text): void
    {
        $this->content = $text;
    }
}
Psalm output (using commit 61ef140):

ERROR: PropertyNotSetInConstructor - 5:7 - Property MyClass::$content is not defined in constructor of MyClass or in any private or final methods called in the constructor

psalm-github-bot[bot] avatar Oct 17 '22 15:10 psalm-github-bot[bot]

Ah, good catch! But they seem different issues to me.

tvdijen avatar Oct 17 '22 15:10 tvdijen