Menu

← Back to propertySetHook.*

Error Identifier: propertySetHook.nativeParameterType

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 string $name {
		set(int $value) { // ERROR: Native type int of set hook parameter $value is not contravariant with native type string of property Foo::$name.
			$this->name = (string) $value;
		}
	}
}

Why is it reported? #

The native type of the set hook parameter must be compatible with the property’s native type. Specifically, the parameter type must be contravariant with (a supertype of) the property type. This ensures type safety – any value that the property can hold must also be accepted by the set hook.

This error is also reported when:

  • The set hook parameter has a native type but the property does not
  • The property has a native type but the set hook parameter does not

How to fix it #

Make the set hook parameter type match or be a supertype of the property type:

 <?php declare(strict_types = 1);
 
 class Foo
 {
 	public string $name {
-		set(int $value) {
-			$this->name = (string) $value;
+		set(string $value) {
+			$this->name = $value;
 		}
 	}
 }

Or widen the set hook parameter type to accept the property type:

 <?php declare(strict_types = 1);
 
 class Foo
 {
 	public string $name {
-		set(int $value) {
-			$this->name = (string) $value;
+		set(string|int $value) {
+			$this->name = (string) $value;
 		}
 	}
 }

Non-ignorable error #

This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.

Rules that report this error #

  • PHPStan\Rules\Properties\SetPropertyHookParameterRule [1] [2] [3]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.