Error Identifier: classConstant.finalPrivate
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 private const BAR = 'baz';
}
Why is it reported? #
A class constant is declared as both final and private. The final modifier prevents child classes from overriding the constant, but private constants are not visible to child classes and therefore cannot be overridden anyway. Combining final with private is redundant and misleading.
How to fix it #
Remove the final modifier from the private constant:
<?php declare(strict_types = 1);
class Foo
{
- final private const BAR = 'baz';
+ private const BAR = 'baz';
}
Alternatively, if the constant should be accessible to child classes but not overridable, change the visibility to protected or public:
<?php declare(strict_types = 1);
class Foo
{
- final private const BAR = 'baz';
+ final protected const BAR = 'baz';
}
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.
Rules that report this error #
- PHPStan\Rules\Constants\FinalPrivateConstantRule [1]