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 A of B
* @template B of A
*/
class Pair
{
}
Why is it reported? #
The bound of a template type (@template T of Bound) restricts which types the template can be replaced with. In the example above, the bound of A is B and the bound of B is A, so neither bound can be resolved to an actual type. The cycle is shown in the error message, for example A → B → A. A template bounded by itself, like @template T of T, is circular too.
The same check applies to templates of functions, methods, interfaces, traits, @method tags, and generic callable and Closure types in PHPDocs, for example callable<A of B, B of A>(A): B.
How to fix it #
Break the cycle by giving at least one of the templates a bound that does not depend on the others:
/**
- * @template A of B
+ * @template A of object
* @template B of A
*/
class Pair
{
}
If the bound is not needed, remove it:
/**
- * @template A of B
- * @template B of A
+ * @template A
+ * @template B of A
*/
class Pair
{
}
Learn more about template types in Generics in PHP using PHPDocs.
How to ignore this error #
You can use the identifier generics.circularBound to ignore this error using a comment:
// @phpstan-ignore generics.circularBound
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.circularBound
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]