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
{
$fn = makeLoggable(...);
$fn(new PlainObject());
}
Why is it reported? #
PHPStan reports this error when the return type of a callable or first-class callable 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 the first-class callable $fn is called with new PlainObject(), T resolves to PlainObject. Since PlainObject does not implement Loggable, the intersection PlainObject&Loggable is impossible and becomes unresolvable.
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
{
$fn = makeLoggable(...);
- $fn(new PlainObject());
+ $fn(new LoggableObject());
}
Or simplify the return type to avoid intersection types:
/**
- * @template T of object
- * @param T $value
- * @return T&Loggable
+ * @param object $value
+ * @return Loggable
*/
-function makeLoggable(object $value): object
+function makeLoggable(object $value): Loggable
How to ignore this error #
You can use the identifier callable.unresolvableReturnType to ignore this error using a comment:
// @phpstan-ignore callable.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: callable.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]