Error Identifier: function.unresolvableReturnType
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 of object
* @param class-string<T> $class
* @return T
*/
function create(string $class): object
{
return new $class();
}
function doFoo(): void
{
$result = create(identity('stdClass'));
}
Why is it reported? #
The return type of a function call contains an unresolvable type. This happens when a function’s return type depends on generic template types that PHPStan cannot fully resolve from the provided arguments. The result is that PHPStan cannot determine the precise return type, which may lead to less accurate analysis downstream.
This typically occurs when generic functions are composed in complex ways, or when the type information flowing through nested function calls is insufficient for PHPStan to resolve all template types.
How to fix it #
Break the expression into separate steps so PHPStan can resolve the types at each stage:
<?php declare(strict_types = 1);
function doFoo(): void
{
- $result = create(identity('stdClass'));
+ $className = identity('stdClass');
+ $result = create($className);
}
Or provide explicit type annotations to help PHPStan understand the types:
<?php declare(strict_types = 1);
function doFoo(): void
{
/** @var class-string<\stdClass> $className */
$className = identity('stdClass');
$result = create($className);
}
How to ignore this error #
You can use the identifier function.unresolvableReturnType to ignore this error using a comment:
// @phpstan-ignore function.unresolvableReturnType
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: function.unresolvableReturnType
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]