Menu

← Back to assign.*

Error Identifier: assign.propertyProtectedSet

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
{
	public protected(set) int $value = 0;
}

function doFoo(Foo $foo): void
{
	$foo->value = 5;
}

Why is it reported? #

The property uses asymmetric visibility with protected(set), meaning it can be read publicly but can only be written to from within the declaring class or its subclasses. The code is attempting to assign a value to this property from outside the class hierarchy, which violates the write visibility restriction.

Asymmetric visibility is a PHP 8.4+ feature that allows separate read and write access levels for properties.

In the example above, $foo->value is publicly readable but can only be assigned from within Foo or its subclasses. The assignment in doFoo() is outside the class hierarchy and therefore not allowed.

How to fix it #

Use a public method on the class to modify the property:

 <?php declare(strict_types = 1);
 
 class Foo
 {
 	public protected(set) int $value = 0;

+	public function setValue(int $value): void
+	{
+		$this->value = $value;
+	}
 }

 function doFoo(Foo $foo): void
 {
-	$foo->value = 5;
+	$foo->setValue(5);
 }

Or change the property’s write visibility if external writes are intended:

 <?php declare(strict_types = 1);
 
 class Foo
 {
-	public protected(set) int $value = 0;
+	public int $value = 0;
 }

 function doFoo(Foo $foo): void
 {
 	$foo->value = 5;
 }

How to ignore this error #

You can use the identifier assign.propertyProtectedSet to ignore this error using a comment:

// @phpstan-ignore assign.propertyProtectedSet
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: assign.propertyProtectedSet

Rules that report this error #

  • PHPStan\Rules\Properties\AccessPropertiesInAssignRule [1]
  • PHPStan\Rules\Properties\AccessPropertiesRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.