Error Identifier: propertySetHook.parameterType
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
class Foo
{
public int $value {
/** @param positive-int $v */
set (int $v) { // ERROR: Type int<1, max> of set hook parameter $v is not contravariant with type int of property Foo::$value.
}
}
}
Why is it reported? #
In PHP 8.4 property hooks, the type of the set hook’s parameter must be contravariant with (i.e. the same as or wider than) the property type. This rule applies to PHPDoc types as well as native types.
In the example above, the set hook parameter has a PHPDoc type of positive-int (int<1, max>), which is narrower than the property’s int type. This means the set hook would reject some values that the property type allows, creating an inconsistency.
How to fix it #
Make the set hook parameter type at least as wide as the property type:
<?php declare(strict_types = 1);
class Foo
{
public int $value {
- /** @param positive-int $v */
set (int $v) {
}
}
}
Alternatively, narrow the property type to match the parameter constraint:
<?php declare(strict_types = 1);
class Foo
{
+ /** @var positive-int */
public int $value {
/** @param positive-int $v */
set (int $v) {
}
}
}
How to ignore this error #
You can use the identifier propertySetHook.parameterType to ignore this error using a comment:
// @phpstan-ignore propertySetHook.parameterType
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: propertySetHook.parameterType
Rules that report this error #
- PHPStan\Rules\Properties\SetPropertyHookParameterRule [1]