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
*/
#[\Attribute]
class MyAttribute
{
/**
* @param T $value
* @phpstan-self-out self<T&Loggable>
*/
public function __construct(public object $value) {}
}
class PlainObject {}
#[MyAttribute(new PlainObject())]
class Foo {}
Why is it reported? #
PHPStan reports this error when the return type of an attribute constructor call contains an unresolvable type after generic template substitution. This can happen when the constructor’s resolved return type includes an intersection or conditional type that simplifies to an impossible type.
For example, if a generic attribute’s constructor resolves a template type T to PlainObject, and the return type contains T&Loggable, the intersection PlainObject&Loggable becomes unresolvable if PlainObject does not implement Loggable.
How to fix it #
Pass an argument whose type satisfies all constraints in the resolved return type:
-#[MyAttribute(new PlainObject())]
+#[MyAttribute(new LoggableObject())]
class Foo {}
Or simplify the attribute class to avoid intersection return types in the constructor:
/**
- * @template T of object
+ * @template T of Loggable
*/
#[\Attribute]
class MyAttribute
{
- /**
- * @param T $value
- * @phpstan-self-out self<T&Loggable>
- */
- public function __construct(public object $value) {}
+ /** @param T $value */
+ public function __construct(public object $value) {}
}
How to ignore this error #
You can use the identifier attribute.unresolvableReturnType to ignore this error using a comment:
// @phpstan-ignore attribute.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: attribute.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]