Error Identifier: property.parentPropertyFinalByPhpDoc
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
{
/** @final */
public int $value = 0;
}
class Bar extends Foo
{
public int $value = 1; // ERROR: Property Bar::$value overrides @final property Foo::$value.
}
Why is it reported? #
The parent class has marked the property as @final in its PHPDoc, indicating that it should not be overridden in subclasses. The child class is overriding this property, which violates the parent’s intent.
The @final PHPDoc annotation is a softer version of the PHP final keyword – it signals the author’s intent that the property should not be overridden, even though PHP does not enforce this at runtime. PHPStan enforces this restriction at the static analysis level.
How to fix it #
Remove the property override from the child class:
<?php declare(strict_types = 1);
class Bar extends Foo
{
- public int $value = 1;
}
If the child class needs a different default value, set it in the constructor instead:
<?php declare(strict_types = 1);
class Bar extends Foo
{
- public int $value = 1;
+ public function __construct()
+ {
+ $this->value = 1;
+ }
}
How to ignore this error #
You can use the identifier property.parentPropertyFinalByPhpDoc to ignore this error using a comment:
// @phpstan-ignore property.parentPropertyFinalByPhpDoc
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.parentPropertyFinalByPhpDoc
Rules that report this error #
- PHPStan\Rules\Properties\OverridingPropertyRule [1]