Error Identifier: methodTag.shadowTemplate
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);
/**
* @template T
* @method void process<T>(T $item)
*/
class Container
{
}
Why is it reported? #
A @method PHPDoc tag defines a template type parameter that has the same name as a template type parameter already defined on the class via @template. This shadows the class-level template type, making it ambiguous which T is being referred to. The method-level template hides the class-level one within the scope of that method tag.
How to fix it #
Rename the template type parameter in the @method tag to avoid the name collision:
<?php declare(strict_types = 1);
/**
* @template T
- * @method void process<T>(T $item)
+ * @method void process<U>(U $item)
*/
class Container
{
}
Alternatively, if the method should use the class-level template type, remove the template parameter from the @method tag:
<?php declare(strict_types = 1);
/**
* @template T
- * @method void process<T>(T $item)
+ * @method void process(T $item)
*/
class Container
{
}
How to ignore this error #
You can use the identifier methodTag.shadowTemplate to ignore this error using a comment:
// @phpstan-ignore methodTag.shadowTemplate
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.shadowTemplate