<?php
class T { }
class X {
public function __construct(T $t) {
$this->t = $t;
}
}
class Builder
{
/**
* @var T|null
*/
public $t;
public function createX(): X
{
$this->validate();
// PHPStan complains that at this line, $this->t could be null, except we've already checked it isn't...
// Parameter #1 $t of class X constructor expects T, T|null given.
return new X($this->t);
}
private function validate()
{
if (is_null($this->t)) {
throw new \Exception;
}
}
}
$builder = new Builder;
$builder->t = new T;
$builder->createX();