when I run PHPstan at level 8, I get for example with this code:
/**
* @return Collection<int, Account>
*/
public function getCustomersAttribute(): Collection
{
return $this->account->children;
}
I get the following message:
Cannot access property $children on App\Account|null
I also get this message with many other classes, only with a different attribute.
Since I cannot determine any access to this accessor e.g.
$model->customers
I also don't know whether an empty collection is always returned or zero (not my project, but taken over from an ex-colleague). So no error would be displayed:
/**
* @return Collection<int, Account>|null
*/
public function getCustomersAttribute(): Collection|null
{
return $this->account?->children;
}
I suspect that PHPstan does not recognise that the account attribute is only referenced at runtime. Is there a PHPstan annotation for the Account class to tell PHPstan that the Account model can be null? Can anyone help me with this. I have about 100 of these messages and don't want to check for null everywhere since the code works.
I have tried in some places to check if the attribute is zero and if it is, I have returned zero, which has often led to errors. PHPstan should be able to recognise that the account model can also be null.