Error Identifier: requireExtends.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 Logging
{
}
/**
* @phpstan-require-extends Logging
*/
interface HasLogging
{
}
Why is it reported? #
The @phpstan-require-extends PHPDoc tag references a trait, but it expects a class. A class can only extend another class, not a trait. Traits are used via the use keyword, not through inheritance.
How to fix it #
If you want to require that a class uses a specific trait, there is no built-in PHPDoc tag for that. Instead, reference a class in @phpstan-require-extends:
<?php declare(strict_types = 1);
+class BaseWithLogging
+{
+ use Logging;
+}
+
/**
- * @phpstan-require-extends Logging
+ * @phpstan-require-extends BaseWithLogging
*/
interface HasLogging
{
}
Alternatively, if the trait should be an interface, use @phpstan-require-implements instead:
<?php declare(strict_types = 1);
-trait Logging
+interface Logging
{
}
/**
- * @phpstan-require-extends Logging
+ * @phpstan-require-implements Logging
*/
interface HasLogging
{
}
How to ignore this error #
You can use the identifier requireExtends.trait to ignore this error using a comment:
// @phpstan-ignore requireExtends.trait
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: requireExtends.trait