Back to Phpstan

trait.unused

website/errors/trait.unused.md

2.2.1856 B
Original Source

Code example

php
<?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.

diff-php
 <?php declare(strict_types = 1);

 trait Loggable
 {
 	public function log(string $message): void
 	{
 		echo $message;
 	}
 }
+
+class UserService
+{
+	use Loggable;
+}