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.
RuleErrorBuilder::identifier().
Code example #
<?php declare(strict_types = 1);
interface Loggable {}
/**
* @template T of object
*/
class Wrapper
{
/**
* @param T $value
* @phpstan-self-out self<T&Loggable>
*/
public function __construct(private object $value) {}
}
class PlainObject {}
function doFoo(): void
{
new Wrapper(new PlainObject());
}
Why is it reported? #
PHPStan reports this error when the return type of a new expression contains an unresolvable type after generic template substitution. The return type of a constructor call is the class itself with its template parameters resolved. If the resolved template types make the class type contain an impossible intersection or an otherwise unresolvable type, PHPStan reports this error.
In the example above, if the constructor’s @phpstan-self-out resolves to self<PlainObject&Loggable>, and PlainObject does not implement Loggable, the intersection is impossible and the return type becomes unresolvable.
How to fix it #
Pass an argument whose type satisfies all constraints:
+class LoggableObject implements Loggable
+{
+ public function log(): void {}
+}
+
function doFoo(): void
{
- new Wrapper(new PlainObject());
+ $w = new Wrapper(new LoggableObject());
}
Or constrain the template type so the intersection is always valid:
/**
- * @template T of object
+ * @template T of Loggable
*/
class Wrapper
{
- /**
- * @param T $value
- * @phpstan-self-out self<T&Loggable>
- */
- public function __construct(private object $value) {}
+ /** @param T $value */
+ public function __construct(private object $value) {}
}
How to ignore this error #
You can use the identifier new.unresolvableReturnType to ignore this error using a comment:
// @phpstan-ignore new.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: new.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\FunctionCallableRule [1]
- PHPStan\Rules\Functions\ParamAttributesRule [1]
- PHPStan\Rules\Methods\CallMethodsRule [1]
- PHPStan\Rules\Methods\CallStaticMethodsRule [1]
- PHPStan\Rules\Methods\MethodAttributesRule [1]
- PHPStan\Rules\Methods\MethodCallableRule [1]
- PHPStan\Rules\Methods\StaticMethodCallableRule [1]
- PHPStan\Rules\Properties\PropertyAttributesRule [1]
- PHPStan\Rules\Properties\PropertyHookAttributesRule [1]
- PHPStan\Rules\Traits\TraitAttributesRule [1]