website/errors/property.onlyWritten.md
<?php declare(strict_types = 1);
class UserProcessor
{
private string $lastProcessed;
public function process(string $name): void
{
$this->lastProcessed = $name;
}
}
A private property is being written to but is never read anywhere in the class. This indicates dead code -- the property stores a value that is never used. Writing to a property without ever reading it has no observable effect and is likely a leftover from a refactoring or a mistake.
If the property is not needed, remove it along with its assignments:
<?php declare(strict_types = 1);
class UserProcessor
{
- private string $lastProcessed;
-
public function process(string $name): void
{
- $this->lastProcessed = $name;
+ // process the name
}
}
If the property is meant to be read, add the code that reads it:
<?php declare(strict_types = 1);
class UserProcessor
{
private string $lastProcessed;
public function process(string $name): void
{
$this->lastProcessed = $name;
}
+ public function getLastProcessed(): string
+ {
+ return $this->lastProcessed;
+ }
}
If the property is read by a framework or library through reflection, you can use the @phpstan-use annotation or configure PHPStan to always mark certain properties as read.