Error Identifier: classConstant.nonFinal
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);
trait HasVersion
{
final public const VERSION = '1.0';
}
class App
{
use HasVersion;
public const VERSION = '1.0';
}
Why is it reported? #
The class declares a constant that overrides a final constant from a used trait, but does not declare it as final as well. When a trait defines a constant as final, any class that uses the trait and redeclares that constant must also declare it as final. This is a PHP language constraint for trait constant inheritance.
How to fix it #
Add the final keyword to the overriding constant:
<?php declare(strict_types = 1);
class App
{
use HasVersion;
- public const VERSION = '1.0';
+ final public const VERSION = '1.0';
}
Or remove the redeclaration and let the trait constant be inherited:
<?php declare(strict_types = 1);
class App
{
use HasVersion;
-
- public const VERSION = '1.0';
}
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\Traits\ConflictingTraitConstantsRule [1]