Menu
Error Identifier: methodTag.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
{
}
}
/**
* @method MyTrait getHelper()
*/
class Foo
{
}
Why is it reported? #
A @method PHPDoc tag references a trait as a type. Traits cannot be used as types in PHP – they cannot be instantiated, passed as parameters, or returned from methods. A trait is a mechanism for code reuse and does not represent a type on its own.
How to fix it #
Replace the trait with a class or interface that represents the intended type:
-trait MyTrait
+interface MyInterface
{
- public function doFoo(): void
- {
- }
+ public function doFoo(): void;
}
/**
- * @method MyTrait getHelper()
+ * @method MyInterface getHelper()
*/
class Foo
{
}
How to ignore this error #
You can use the identifier methodTag.trait to ignore this error using a comment:
// @phpstan-ignore methodTag.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: methodTag.trait