Error Identifier: possiblyImpure.propertyAssignByRef
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 $value = 0;
/**
* @phpstan-pure
*/
public function getRef(): int
{
$ref = &$this->value;
return 1;
}
}
Why is it reported? #
The function or method is marked as @phpstan-pure but creates a reference to a property via &$this->property or &$object->property. Assigning by reference to a property is a potential side effect because modifying the reference variable later would also modify the property. Pure functions must not have side effects.
How to fix it #
Remove the by-reference assignment, or remove the @phpstan-pure annotation:
/**
- * @phpstan-pure
*/
public function getRef(): int
{
- $ref = &$this->value;
+ $ref = $this->value;
return 1;
}
How to ignore this error #
You can use the identifier possiblyImpure.propertyAssignByRef to ignore this error using a comment:
// @phpstan-ignore possiblyImpure.propertyAssignByRef
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: possiblyImpure.propertyAssignByRef