Error Identifier: assert.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 Loggable
{
public function log(string $message): void
{
// ...
}
}
class Checker
{
/**
* @phpstan-assert Loggable $value
*/
public function assertLoggable(mixed $value): void
{
// ...
}
}
Why is it reported? #
A @phpstan-assert PHPDoc tag references a trait, which is not a valid type for assertions. Traits cannot be used as standalone types in PHP – they cannot be instantiated, and instanceof checks against traits are not supported. Using a trait in a @phpstan-assert tag is meaningless because it does not represent a type that can be checked at runtime.
In the example above, Loggable is a trait, so @phpstan-assert Loggable $value is invalid.
How to fix it #
Replace the trait with an interface that declares the same contract:
<?php declare(strict_types = 1);
-trait Loggable
+interface Loggable
{
- public function log(string $message): void
- {
- // ...
- }
+ public function log(string $message): void;
}
class Checker
{
/**
* @phpstan-assert Loggable $value
*/
public function assertLoggable(mixed $value): void
{
// ...
}
}
How to ignore this error #
You can use the identifier assert.trait to ignore this error using a comment:
// @phpstan-ignore assert.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: assert.trait