Error Identifier: impure.propertyAssign
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 Counter
{
public int $count = 0;
/** @phpstan-pure */
public function increment(): int
{
$this->count++;
return $this->count;
}
}
Why is it reported? #
A function or method marked as @phpstan-pure contains a property assignment. Pure functions must not have side effects, and modifying an object’s property is a side effect because it changes the observable state.
How to fix it #
Remove the property assignment from the pure function:
<?php declare(strict_types = 1);
class Counter
{
public int $count = 0;
- /** @phpstan-pure */
- public function increment(): int
+ public function increment(): int
{
$this->count++;
return $this->count;
}
}
Or restructure the code so the pure function does not mutate state.
How to ignore this error #
You can use the identifier impure.propertyAssign to ignore this error using a comment:
// @phpstan-ignore impure.propertyAssign
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: impure.propertyAssign