Menu
Error Identifier: property.readOnlyByPhpDocDefaultValue
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 */
public int $value = 0;
}
Why is it reported? #
A property marked with @readonly (or @phpstan-readonly) in PHPDoc has a default value. Readonly properties should only be initialized in the constructor, not with a default value in the property declaration.
How to fix it #
Remove the default value and initialize the property in the constructor:
<?php declare(strict_types = 1);
class Foo
{
/** @readonly */
- public int $value = 0;
+ public int $value;
+
+ public function __construct()
+ {
+ $this->value = 0;
+ }
}
How to ignore this error #
You can use the identifier property.readOnlyByPhpDocDefaultValue to ignore this error using a comment:
// @phpstan-ignore property.readOnlyByPhpDocDefaultValue
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.readOnlyByPhpDocDefaultValue
Rules that report this error #
- PHPStan\Rules\Properties\ReadOnlyByPhpDocPropertyRule [1]