Menu

← Back to method.*

Error Identifier: method.variance

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 C
{
	/**
	 * @template-covariant U
	 * @return void
	 */
	public function doFoo(): void
	{
	}
}

Why is it reported? #

Variance annotations (@template-covariant and @template-contravariant) are only allowed for type parameters of classes and interfaces, not for methods or functions. A method-level template type cannot be declared covariant or contravariant because variance only has meaning in the context of a class’s type parameters – it describes how the class relates to its subtypes.

How to fix it #

Remove the variance annotation from the method-level template type. Use a plain @template instead:

 class C
 {
 	/**
-	 * @template-covariant U
+	 * @template U
 	 * @return void
 	 */
 	public function doFoo(): void
 	{
 	}
 }

If variance is needed, move the template type to the class level:

+/**
+ * @template-covariant U
+ */
 class C
 {
-	/**
-	 * @template-covariant U
-	 * @return void
-	 */
 	public function doFoo(): void
 	{
 	}
 }

How to ignore this error #

You can use the identifier method.variance to ignore this error using a comment:

// @phpstan-ignore method.variance
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: method.variance

Rules that report this error #

  • PHPStan\Rules\Generics\ClassAncestorsRule [1]
  • PHPStan\Rules\Generics\EnumAncestorsRule [1]
  • PHPStan\Rules\Generics\FunctionSignatureVarianceRule [1]
  • PHPStan\Rules\Generics\InterfaceAncestorsRule [1]
  • PHPStan\Rules\Generics\MethodSignatureVarianceRule [1]
  • PHPStan\Rules\Generics\PropertyVarianceRule [1]
  • PHPStan\Rules\Generics\UsedTraitsRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.