psalm
psalm copied to clipboard
False-positive PropertyNotSetInConstructor when nesting traits
The snippet should speak for itself: https://psalm.dev/r/6ab3830574
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
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
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
Ah, good catch! But they seem different issues to me.