Back to Phpstan

impure.propertyAssignByRef

website/errors/impure.propertyAssignByRef.md

2.2.11.3 KB
Original Source

Code example

php
<?php declare(strict_types = 1);

class Foo
{
	public int $value = 0;

	/** @phpstan-pure */
	public function doSomething(): int
	{
		$ref = &$this->value;

		return 1;
	}
}

Why is it reported?

This identifier is not reported in practice because assigning by reference to a property is reported with a different identifier. PHPStan reports possiblyImpure.propertyAssignByRef instead.

The method is declared as pure using @phpstan-pure, but it creates a reference to an object property ($ref = &$this->value). Assigning a variable by reference to a property is an impure operation because modifying the reference variable later would mutate the object's state, which violates the purity contract.

Pure functions and methods must not have side effects -- they should only compute and return a value based on their inputs.

How to fix it

If the method needs to reference object properties, remove the @phpstan-pure annotation:

diff-php
-	/** @phpstan-pure */
 	public function doSomething(): int
 	{
 		$ref = &$this->value;

 		return 1;
 	}

If the method should remain pure, avoid creating references to properties:

diff-php
 	/** @phpstan-pure */
 	public function doSomething(): int
 	{
-		$ref = &$this->value;
+		$ref = $this->value;

 		return 1;
 	}