Error Identifier: generics.callSiteVarianceConflict
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
*/
interface Collection
{
/** @return T */
public function first(): mixed;
}
/**
* @param Collection<contravariant string> $collection
*/
function process(Collection $collection): void
{
}
Why is it reported? #
A call-site variance annotation (such as covariant or contravariant) on a generic type argument conflicts with the variance declared on the template type parameter of the class or interface. In this example, the template parameter T of Collection is declared as @template-covariant, but the call-site annotation uses contravariant, which is not a valid position for a covariant template type.
Call-site variance annotations (also called “use-site variance” or “type projections”) must be compatible with the declaration-site variance of the template parameter.
How to fix it #
Remove the conflicting call-site variance annotation, or use one that is compatible with the declaration-site variance:
<?php declare(strict_types = 1);
/**
- * @param Collection<contravariant string> $collection
+ * @param Collection<covariant string> $collection
*/
function process(Collection $collection): void
{
}
Or simply use the type without a call-site variance annotation:
<?php declare(strict_types = 1);
/**
- * @param Collection<contravariant string> $collection
+ * @param Collection<string> $collection
*/
function process(Collection $collection): void
{
}
How to ignore this error #
You can use the identifier generics.callSiteVarianceConflict to ignore this error using a comment:
// @phpstan-ignore generics.callSiteVarianceConflict
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: generics.callSiteVarianceConflict
Rules that report this error #
- PHPStan\Rules\Classes\LocalTypeAliasesRule [1]
- PHPStan\Rules\Classes\LocalTypeTraitAliasesRule [1]
- PHPStan\Rules\Classes\LocalTypeTraitUseAliasesRule [1]
- PHPStan\Rules\Classes\MethodTagRule [1]
- PHPStan\Rules\Classes\MethodTagTraitRule [1]
- PHPStan\Rules\Classes\MethodTagTraitUseRule [1]
- PHPStan\Rules\Classes\MixinRule [1]
- PHPStan\Rules\Classes\MixinTraitRule [1]
- PHPStan\Rules\Classes\MixinTraitUseRule [1]
- PHPStan\Rules\Classes\PropertyTagRule [1]
- PHPStan\Rules\Classes\PropertyTagTraitRule [1]
- PHPStan\Rules\Classes\PropertyTagTraitUseRule [1]
- PHPStan\Rules\Generics\ClassAncestorsRule [1]
- PHPStan\Rules\Generics\ClassTemplateTypeRule [1]
- PHPStan\Rules\Generics\EnumAncestorsRule [1]
- PHPStan\Rules\Generics\FunctionTemplateTypeRule [1]
- PHPStan\Rules\Generics\InterfaceAncestorsRule [1]
- PHPStan\Rules\Generics\InterfaceTemplateTypeRule [1]
- PHPStan\Rules\Generics\MethodTagTemplateTypeRule [1]
- PHPStan\Rules\Generics\MethodTagTemplateTypeTraitRule [1]
- PHPStan\Rules\Generics\MethodTemplateTypeRule [1]
- PHPStan\Rules\Generics\TraitTemplateTypeRule [1]
- PHPStan\Rules\Generics\UsedTraitsRule [1]
- PHPStan\Rules\PhpDoc\FunctionAssertRule [1]
- PHPStan\Rules\PhpDoc\IncompatibleClassConstantPhpDocTypeRule [1]
- PHPStan\Rules\PhpDoc\IncompatiblePhpDocTypeRule [1]
- PHPStan\Rules\PhpDoc\IncompatiblePropertyHookPhpDocTypeRule [1]
- PHPStan\Rules\PhpDoc\IncompatiblePropertyPhpDocTypeRule [1]
- PHPStan\Rules\PhpDoc\IncompatibleSelfOutTypeRule [1]
- PHPStan\Rules\PhpDoc\InvalidPhpDocVarTagTypeRule [1]
- PHPStan\Rules\PhpDoc\MethodAssertRule [1]