Error Identifier: trait.unused
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 Loggable
{
public function log(string $message): void
{
echo $message;
}
}
Why is it reported? #
The trait is declared but never used by any class in the analysed codebase. PHPStan analyses traits in the context of the classes that use them, so a trait that is not used anywhere is never analysed for errors. This means bugs inside the trait would go undetected.
See: How PHPStan analyses traits
How to fix it #
Use the trait in at least one class so that PHPStan can analyse its code, or remove the trait if it is no longer needed.
<?php declare(strict_types = 1);
trait Loggable
{
public function log(string $message): void
{
echo $message;
}
}
+
+class UserService
+{
+ use Loggable;
+}
How to ignore this error #
You can use the identifier trait.unused to ignore this error using a comment:
// @phpstan-ignore trait.unused
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: trait.unused
Rules that report this error #
- PHPStan\Rules\Traits\NotAnalysedTraitRule [1]