Error Identifier: interfaceExtends.trait
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 MyTrait
{
}
interface MyInterface extends MyTrait
{
}
Why is it reported? #
An interface declares that it extends a trait, which is not allowed in PHP. Interfaces can only extend other interfaces. Traits are a different kind of language construct used for horizontal code reuse via use statements in classes, not for interface inheritance.
How to fix it #
If the target should be an interface, change the trait to an interface:
-trait MyTrait
+interface MyTrait
{
}
interface MyInterface extends MyTrait
{
}
If the trait is correct, the interface should not extend it. Instead, a class can use the trait and implement the interface separately:
trait MyTrait
{
}
-interface MyInterface extends MyTrait
+interface MyInterface
{
}
+
+class MyClass implements MyInterface
+{
+ use MyTrait;
+}
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\ExistingClassesInInterfaceExtendsRule [1]