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 T = U
* @template U = T
*/
class Pair
{
}
Why is it reported? #
A template type can have a default (@template T = Default) that is used when the template is not specified, for example in @param Pair $pair. A default may refer to other templates, including ones declared later. In the example above, the default of T is U and the default of U is T, so neither default can be resolved to an actual type. The cycle is shown in the error message, for example T → U → T.
The same check applies to templates of functions, methods, interfaces, traits, @method tags, and generic callable and Closure types in PHPDocs.
How to fix it #
Break the cycle by giving at least one of the templates a default that does not depend on the others:
/**
* @template T = U
- * @template U = T
+ * @template U = int
*/
class Pair
{
}
How to ignore this error #
You can use the identifier generics.circularDefault to ignore this error using a comment:
// @phpstan-ignore generics.circularDefault
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.circularDefault
Rules that report this error #
- PHPStan\Rules\Classes\MethodTagRule [1]
- PHPStan\Rules\Classes\MethodTagTraitRule [1]
- PHPStan\Rules\Classes\MethodTagTraitUseRule [1]
- PHPStan\Rules\Classes\PropertyTagRule [1]
- PHPStan\Rules\Classes\PropertyTagTraitRule [1]
- PHPStan\Rules\Classes\PropertyTagTraitUseRule [1]
- PHPStan\Rules\Generics\ClassTemplateTypeRule [1]
- PHPStan\Rules\Generics\FunctionTemplateTypeRule [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\PhpDoc\IncompatibleClassConstantPhpDocTypeRule [1]
- PHPStan\Rules\PhpDoc\IncompatiblePhpDocTypeRule [1]
- PHPStan\Rules\PhpDoc\IncompatiblePropertyHookPhpDocTypeRule [1]
- PHPStan\Rules\PhpDoc\IncompatiblePropertyPhpDocTypeRule [1] [2]
- PHPStan\Rules\PhpDoc\InvalidPhpDocVarTagTypeRule [1]