Error Identifier: assign.propertyType
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 $count;
public function setCount(string $value): void
{
$this->count = $value;
}
}
Why is it reported? #
The value being assigned to a property does not match the property’s declared type. In the example above, the property $count is declared as int, but a string value is being assigned to it. This would cause a TypeError at runtime when strict types are enabled, or an unexpected implicit type coercion otherwise.
How to fix it #
Ensure the assigned value matches the property’s type:
<?php declare(strict_types = 1);
class Foo
{
public int $count;
- public function setCount(string $value): void
+ public function setCount(int $value): void
{
$this->count = $value;
}
}
Or convert the value to the correct type before assignment:
<?php declare(strict_types = 1);
class Foo
{
public int $count;
public function setCount(string $value): void
{
- $this->count = $value;
+ $this->count = (int) $value;
}
}
How to ignore this error #
You can use the identifier assign.propertyType to ignore this error using a comment:
// @phpstan-ignore assign.propertyType
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: assign.propertyType
Rules that report this error #
- PHPStan\Rules\Properties\TypesAssignedToPropertiesRule [1]