Error Identifier: property.readOnlyByPhpDocAssignNotInConstructor

← Back to property.*

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
{
	/** @readonly */
	private int $value;

	public function __construct(int $value)
	{
		$this->value = $value; // OK
	}

	public function update(int $newValue): void
	{
		$this->value = $newValue; // ERROR
	}
}

Why is it reported? #

The property is annotated with @readonly in its PHPDoc, which means it should only be assigned in the constructor. Assigning it outside the constructor violates the immutability contract declared by the @readonly tag.

Assigning the property in a helper method is allowed when that method is only ever run from the constructor: a private method (or a protected method of a final class without a parent) that the class calls on $this only from its constructor, directly or through other such methods. The assignment is reported when the method can also be called from elsewhere, for example from another public method.

How to fix it #

Move the assignment to the constructor, or use a pattern that returns a new instance with the updated value:

 <?php declare(strict_types = 1);
 
 class Foo
 {
 	/** @readonly */
 	private int $value;

 	public function __construct(int $value)
 	{
 		$this->value = $value;
 	}

-	public function update(int $newValue): void
+	public function withValue(int $newValue): self
 	{
-		$this->value = $newValue;
+		return new self($newValue);
 	}
 }

If the assignment happens in a helper method, make sure only the constructor calls it:

 <?php declare(strict_types = 1);
 
 class Foo
 {
 	/** @readonly */
 	private string $foo;

 	public function __construct()
 	{
 		$this->init();
 	}

-	public function reset(): void
-	{
-		$this->init();
-	}
-
 	private function init(): void
 	{
 		$this->foo = 'bar';
 	}
 }

How to ignore this error #

You can use the identifier property.readOnlyByPhpDocAssignNotInConstructor to ignore this error using a comment:

// @phpstan-ignore property.readOnlyByPhpDocAssignNotInConstructor
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: property.readOnlyByPhpDocAssignNotInConstructor

Rules that report this error #

  • PHPStan\Rules\Properties\ReadOnlyByPhpDocPropertyAssignRule [1]
Theme
© 2026 PHPStan s.r.o.