Error Identifier: argument.templateType
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
* @param T $value
* @return T
*/
function identity(mixed $value): mixed
{
return $value;
}
/**
* @template T
* @return T
*/
function broken(): mixed
{
return null;
}
function doFoo(): void
{
// T cannot be resolved because there is no argument
// that would allow PHPStan to infer the template type
broken();
}
Why is it reported? #
The called function or method declares a template type (generic type parameter) that appears in the return type, but PHPStan cannot determine what concrete type it should resolve to based on the provided arguments. This typically happens when the template type is used in the return type but none of the parameters allow PHPStan to infer it from the call site.
Learn more: Solving PHPStan error “Unable to resolve template type”
How to fix it #
Pass an argument that allows PHPStan to infer the template type:
<?php declare(strict_types = 1);
/**
* @template T
- * @return T
+ * @param class-string<T> $class
+ * @return T
*/
-function broken(): mixed
+function create(string $class): mixed
{
- return null;
+ return new $class();
}
Or specify the template type explicitly using PHPDoc:
-broken();
+/** @var string $result */
+$result = broken();
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]