psalm icon indicating copy to clipboard operation
psalm copied to clipboard

False-positive PropertyNotSetInConstructor when method chaining

Open sgurdiel opened this issue 7 months ago • 1 comments

<?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

sgurdiel avatar May 23 '25 17:05 sgurdiel

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

psalm-github-bot[bot] avatar May 23 '25 17:05 psalm-github-bot[bot]