website/errors/property.onlyRead.md
<?php declare(strict_types = 1);
class Foo
{
private int $counter;
public function getCounter(): int
{
return $this->counter;
}
}
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.
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.
If the property is written through a mechanism not visible to PHPStan (such as reflection, serialization, or dependency injection), a custom extension can be used to inform PHPStan about the write.