Menu

← Back to staticMethod.*

Error Identifier: staticMethod.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);

class Factory
{
	/**
	 * @template T of object
	 * @param class-string<T> $class
	 * @return T
	 */
	public static function create(string $class): object
	{
		return new $class();
	}
}

function doFoo(): void
{
	$result = Factory::create('stdClass' | 'DateTime');
}

Why is it reported? #

The return type of a static method call contains a template type that PHPStan cannot resolve based on the provided arguments. This typically happens when the arguments passed to the method do not provide enough type information for PHPStan to determine the concrete type that the template parameter should resolve to.

When PHPStan cannot resolve the template type, the return type becomes imprecise, which can lead to missed type errors downstream.

How to fix it #

Pass more specific types as arguments so that PHPStan can resolve the template type:

 <?php declare(strict_types = 1);
 
 function doFoo(): void
 {
-	$result = Factory::create('stdClass' | 'DateTime');
+	$result = Factory::create(\stdClass::class);
 }

If the template type on the method is not needed, simplify the method signature:

 <?php declare(strict_types = 1);
 
 class Factory
 {
-	/**
-	 * @template T of object
-	 * @param class-string<T> $class
-	 * @return T
-	 */
-	public static function create(string $class): object
+	/**
+	 * @param class-string $class
+	 */
+	public static function create(string $class): object
 	{
 		return new $class();
 	}
 }

How to ignore this error #

You can use the identifier staticMethod.unresolvableReturnType to ignore this error using a comment:

// @phpstan-ignore staticMethod.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: staticMethod.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]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.