Error Identifier: mixin.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 MyTrait
{
public function doFoo(): void
{
}
}
/**
* @mixin MyTrait
*/
class Foo
{
}
Why is it reported? #
A @mixin PHPDoc tag references a trait. Traits cannot be used as types in PHP – they cannot be instantiated or used as a mixin type. The @mixin tag expects a class or object type so that PHPStan knows which methods and properties to forward.
To use a trait’s methods in a class, use the use statement directly in the class body instead of @mixin.
How to fix it #
Use the trait directly with a use statement:
-/**
- * @mixin MyTrait
- */
class Foo
{
+ use MyTrait;
}
Alternatively, replace the trait reference with a class or interface:
-trait MyTrait
+class MyHelper
{
public function doFoo(): void
{
}
}
/**
- * @mixin MyTrait
+ * @mixin MyHelper
*/
class Foo
{
}
How to ignore this error #
You can use the identifier mixin.trait to ignore this error using a comment:
// @phpstan-ignore mixin.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: mixin.trait