Support property type narrowing in @phpstan-param
Author: rensPolsCreated Mar 26, 2026Updated Sep 27, 2026
Feature request
Assert a narrower property type when a model is passed as an argument into a function. Proposed syntax:
/**
* @phpstan-param Model{property: int} $model
*/
public function foo(Model $model): void {}This syntax would closely resemble the syntax used for narrowing an associative array type.
Example of a possible Model:
final readonly class Model {
public function __construct(
public ?int $property = null,
) {}
}When calling foo(), you now need to have asserted that the property on the model is not null. Example: We have a $model which is of the type Model.
// PHPstan will throw
foo($model);// PHPstan won't throw
if ($model->property === null) {
return;
}
foo($model);An alternative solution for this would be to transform Model to a Model&object{property: ?int} and when assert(is_int($model->property)) is called, it will automatically adapt to Model&object{property: int}. But I'm not sure if this would look "clean" enough
Did PHPStan help you today? Did it make you happy in any way?
Love the tool! Keep up the good work!
Source: phpstan/phpstan