website/errors/impure.propertyAssign.md
<?php declare(strict_types = 1);
class Counter
{
public int $count = 0;
/** @phpstan-pure */
public function increment(): int
{
$this->count++;
return $this->count;
}
}
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.
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.