psalm
psalm copied to clipboard
False-positive PropertyNotSetInConstructor when method chaining
<?php
final class A
{
private int $amount;
public function __construct()
{
$this
->validA()
->validateAmount();
}
private function validA(): self
{
return $this;
}
private function validateAmount(): self
{
$this->amount = 0;
return $this;
}
}
Psalm output (using commit 6d671f6 on PHP 8.2.28):
ERROR: PropertyNotSetInConstructor - 5:17 - Property A::$amount is not defined in constructor of A or in any private or final methods called in the constructor
Example: https://psalm.dev/r/a514ddeb60
I found these snippets:
https://psalm.dev/r/a514ddeb60
<?php
final class A
{
private int $amount;
public function __construct()
{
$this
->validA()
->validateAmount();
}
private function validA(): self
{
return $this;
}
private function validateAmount(): self
{
$this->amount = 0;
return $this;
}
}
Psalm output (using commit 6d671f6):
ERROR: PropertyNotSetInConstructor - 5:17 - Property A::$amount is not defined in constructor of A or in any private or final methods called in the constructor