Error Identifier: propertySetHook.noAssign
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 {
get => $this->count;
set {
echo "Setting count to $value";
}
}
}
Why is it reported? #
A set hook on a non-virtual (backed) property is expected to assign a value to the backing property. If the hook body never assigns to $this->propertyName, the property value does not change when set is called, which is almost certainly a bug.
How to fix it #
Assign the value to the property inside the set hook:
public int $count {
get => $this->count;
set {
echo "Setting count to $value";
+ $this->count = $value;
}
}
Or if the property is intentionally virtual (no backing store), remove the get hook that references $this->count so the property becomes fully virtual.
How to ignore this error #
You can use the identifier propertySetHook.noAssign to ignore this error using a comment:
// @phpstan-ignore propertySetHook.noAssign
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.noAssign
Rules that report this error #
- PHPStan\Rules\Properties\SetNonVirtualPropertyHookAssignRule [1]