Error Identifier: requireImplements.interface
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);
interface Loggable
{
}
/**
* @phpstan-require-implements Loggable
*/
interface AnotherInterface // ERROR: PHPDoc tag @phpstan-require-implements is only valid on trait.
{
}
Why is it reported? #
The @phpstan-require-implements PHPDoc tag is only valid on traits. It allows a trait to declare that any class using it must implement a specific interface. When this tag is placed on an interface (or a class, or an enum), PHPStan reports an error because it is not a valid location for this tag.
Interfaces already have their own mechanism for requiring implementations – they can extend other interfaces directly using the extends keyword.
How to fix it #
If the goal is to have one interface require another, use extends instead:
<?php declare(strict_types = 1);
interface Loggable
{
}
-/**
- * @phpstan-require-implements Loggable
- */
-interface AnotherInterface
+interface AnotherInterface extends Loggable
{
}
If the tag is intended for a trait, move it to the trait:
<?php declare(strict_types = 1);
interface Loggable
{
}
/**
* @phpstan-require-implements Loggable
*/
-interface AnotherInterface
-{
-}
+trait LoggableTrait
+{
+}
How to ignore this error #
You can use the identifier requireImplements.interface to ignore this error using a comment:
// @phpstan-ignore requireImplements.interface
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: requireImplements.interface
Rules that report this error #
- PHPStan\Rules\PhpDoc\RequireImplementsDefinitionTraitRule [1]