Menu
Error Identifier: unset.readOnlyPropertyByPhpDoc
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;
public function __construct(int $value)
{
$this->value = $value;
}
public function reset(): void
{
unset($this->value);
}
}
Why is it reported? #
The code attempts to unset() a property that is marked as @readonly in PHPDoc. Readonly properties should not be unset because they are intended to be immutable after initialization.
How to fix it #
Do not unset the readonly property. If you need to reset it, consider restructuring your code:
<?php declare(strict_types = 1);
class Foo
{
/** @readonly */
public int $value;
public function __construct(int $value)
{
$this->value = $value;
}
- public function reset(): void
- {
- unset($this->value);
- }
}
How to ignore this error #
You can use the identifier unset.readOnlyPropertyByPhpDoc to ignore this error using a comment:
// @phpstan-ignore unset.readOnlyPropertyByPhpDoc
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: unset.readOnlyPropertyByPhpDoc
Rules that report this error #
- PHPStan\Rules\Variables\UnsetRule [1]