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
* @phpstan-type Pair<T> array{T, T}
*/
class Collection
{
}
Why is it reported? #
A local type alias defined with @phpstan-type can declare its own type parameters, and its body can also refer to the @template types of the class, interface, or trait that defines it. When a type parameter of the alias has the same name as one of these template types, the alias’s own parameter wins inside the alias body, just like a method-level @template shadows a class-level one (see method.shadowTemplate).
In the example above, T in array{T, T} refers to the type argument passed to Pair<...>, not to the T of Collection. The class template type cannot be referred to from the alias body, and anyone reading the code can easily confuse the two.
The error is also reported when a type alias defined or imported in the PHPDoc of a function or method has the same name as a @template type of that function, method, or its class.
How to fix it #
Rename the type parameter of the type alias:
/**
* @template T
- * @phpstan-type Pair<T> array{T, T}
+ * @phpstan-type Pair<TItem> array{TItem, TItem}
*/
class Collection
{
}
If the type alias is meant to use the class template type, remove the type parameter from the alias:
/**
* @template T
- * @phpstan-type Pair<T> array{T, T}
+ * @phpstan-type Pair array{T, T}
*/
class Collection
{
}
How to ignore this error #
You can use the identifier typeAlias.shadowTemplate to ignore this error using a comment:
// @phpstan-ignore typeAlias.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: typeAlias.shadowTemplate