Error Identifier: property.onlyRead
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
{
private int $counter;
public function getCounter(): int
{
return $this->counter;
}
}
Why is it reported? #
The private property is read but never written to anywhere in the class. Since it is private, no code outside the class can assign a value to it either. This means the property will always hold its default value (or be uninitialized), which usually indicates dead code or a missing assignment.
How to fix it #
Write to the property where appropriate, for example in the constructor:
<?php declare(strict_types = 1);
class Foo
{
private int $counter;
+ public function __construct(int $initial)
+ {
+ $this->counter = $initial;
+ }
+
public function getCounter(): int
{
return $this->counter;
}
}
If the property is no longer needed, remove it and its usages.
How to ignore this error #
You can use the identifier property.onlyRead to ignore this error using a comment:
// @phpstan-ignore property.onlyRead
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.onlyRead
Rules that report this error #
- PHPStan\Rules\DeadCode\UnusedPrivatePropertyRule [1]