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 of int
* @param T $value
* @return T
*/
function identity($value)
{
return $value;
}
$result = identity('hello');
Why is it reported? #
The called function or method declares a template type (generic type parameter) with a bound, but the provided argument does not satisfy the bound. PHPStan cannot resolve the template type because the argument type falls outside the allowed range specified by the @template T of ... constraint.
In the example above, the template type T is bounded to int, but a string is passed. PHPStan cannot resolve T because string is not a subtype of int.
Learn more: Solving PHPStan error “Unable to resolve template type”
How to fix it #
Pass an argument that satisfies the template bound:
<?php declare(strict_types = 1);
-$result = identity('hello');
+$result = identity(42);
Or widen the template bound if more types should be accepted:
<?php declare(strict_types = 1);
/**
- * @template T of int
+ * @template T of int|string
* @param T $value
* @return T
*/
function identity($value)
{
return $value;
}
How to ignore this error #
You can use the identifier argument.templateType to ignore this error using a comment:
// @phpstan-ignore argument.templateType
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: argument.templateType
Rules that report this error #
- PHPStan\Rules\Classes\ClassAttributesRule [1]
- PHPStan\Rules\Classes\ClassConstantAttributesRule [1]
- PHPStan\Rules\Classes\InstantiationRule [1]
- PHPStan\Rules\Constants\ConstantAttributesRule [1]
- PHPStan\Rules\EnumCases\EnumCaseAttributesRule [1]
- PHPStan\Rules\Functions\ArrowFunctionAttributesRule [1]
- PHPStan\Rules\Functions\CallCallablesRule [1]
- PHPStan\Rules\Functions\CallToFunctionParametersRule [1]
- PHPStan\Rules\Functions\CallUserFuncRule [1]
- PHPStan\Rules\Functions\ClosureAttributesRule [1]
- PHPStan\Rules\Functions\FunctionAttributesRule [1]
- PHPStan\Rules\Functions\ParamAttributesRule [1]
- PHPStan\Rules\Methods\CallMethodsRule [1]
- PHPStan\Rules\Methods\CallStaticMethodsRule [1]
- PHPStan\Rules\Methods\MethodAttributesRule [1]
- PHPStan\Rules\Properties\PropertyAttributesRule [1]
- PHPStan\Rules\Properties\PropertyHookAttributesRule [1]
- PHPStan\Rules\Traits\TraitAttributesRule [1]