Menu

← Back to function.*

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

/**
 * @template-covariant T
 */
function doFoo(): void
{
}

Why is it reported? #

A variance annotation (@template-covariant or @template-contravariant) was used on a template type parameter of a function or method. Variance annotations are only meaningful on class and interface type parameters, where they describe how subtyping of the container relates to subtyping of the type parameter. Functions and methods do not have this relationship.

How to fix it #

Remove the variance annotation from the function’s template type:

 <?php declare(strict_types = 1);
 
 /**
- * @template-covariant T
+ * @template T
  */
 function doFoo(): void
 {
 }

How to ignore this error #

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

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