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);
/**
* @property-read int $readOnlyProperty
*/
class Foo
{
public function __get(string $name): int
{
return 0;
}
public function doFoo(): void
{
$this->readOnlyProperty = 1;
}
}
Why is it reported? #
A value is being assigned to a property that is declared as read-only via a @property-read PHPDoc tag. Such a property is intended only for reading — there is no corresponding @property-write or @property tag that would allow writes.
In the example above, $readOnlyProperty is declared with @property-read, so assigning to it inside the class is not allowed.
How to fix it #
Remove the assignment, or if writes are needed, change the PHPDoc tag to @property to allow both reading and writing:
-/**
- * @property-read int $readOnlyProperty
- */
+/**
+ * @property int $readOnlyProperty
+ */
class Foo
{
How to ignore this error #
You can use the identifier assign.propertyReadOnly to ignore this error using a comment:
// @phpstan-ignore assign.propertyReadOnly
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.propertyReadOnly
Rules that report this error #
- PHPStan\Rules\Properties\WritingToReadOnlyPropertiesRule [1]