Error Identifier: classImplements.class
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 Logger
{
}
class FileLogger implements Logger
{
}
Why is it reported? #
In PHP, a class can only implement interfaces. The implements keyword is reserved exclusively for interface types. Using a class in the implements clause is not valid.
In the example above, FileLogger attempts to implement Logger, which is a class, not an interface.
How to fix it #
Extract an interface from the class, then implement the interface:
<?php declare(strict_types = 1);
-class Logger
+interface LoggerInterface
{
+ public function log(string $message): void;
}
-class FileLogger implements Logger
+class FileLogger implements LoggerInterface
{
+ public function log(string $message): void
+ {
+ file_put_contents('app.log', $message . PHP_EOL, FILE_APPEND);
+ }
}
Alternatively, if the intent is to reuse functionality from the parent class, use extends instead of implements:
<?php declare(strict_types = 1);
-class FileLogger implements Logger
+class FileLogger extends Logger
{
}
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\Classes\ExistingClassesInClassImplementsRule [1]