Menu

← Back to property.*

Error Identifier: property.readOnlyByPhpDocAssignNotInConstructor

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.

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);
 	}
 }

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]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.