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);
interface Loggable
{
public function log(): void;
}
/**
* @template T of object
* @param T $value
* @return T&Loggable
*/
function makeLoggable(object $value): object
{
return $value;
}
class PlainObject {}
function doFoo(): void
{
$result = makeLoggable(new PlainObject());
}
Why is it reported? #
PHPStan reports this error when the return type of a function call contains an unresolvable type after generic template substitution. This happens when the template type resolves to a value that makes the return type impossible.
In the example above, makeLoggable returns T&Loggable. When called with new PlainObject(), T resolves to PlainObject. Since PlainObject does not implement Loggable, the intersection PlainObject&Loggable is impossible and PHPStan cannot determine a valid return type.
How to fix it #
Pass an argument whose type satisfies all constraints in the return type:
+class LoggableObject implements Loggable
+{
+ public function log(): void {}
+}
+
function doFoo(): void
{
- $result = makeLoggable(new PlainObject());
+ $result = makeLoggable(new LoggableObject());
}
Or constrain the template type to require the interface upfront:
/**
- * @template T of object
+ * @template T of Loggable
* @param T $value
- * @return T&Loggable
+ * @return T
*/
function makeLoggable(object $value): object
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]