Error Identifier: generics.templateDefaultOutOfBounds
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 of int
* @template U of string = int
*/
class Container
{
}
Why is it reported? #
The default type specified for a @template tag is not a subtype of the template’s bound. In the example above, U is bounded by string (via of string), but the default is int, which is not a subtype of string.
How to fix it #
Change the default type so that it satisfies the bound constraint:
<?php declare(strict_types = 1);
/**
* @template T of int
- * @template U of string = int
+ * @template U of string = string
*/
class Container
{
}
Alternatively, change the bound to accommodate the desired default:
<?php declare(strict_types = 1);
/**
* @template T of int
- * @template U of string = int
+ * @template U of string|int = int
*/
class Container
{
}
How to ignore this error #
You can use the identifier generics.templateDefaultOutOfBounds to ignore this error using a comment:
// @phpstan-ignore generics.templateDefaultOutOfBounds
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.templateDefaultOutOfBounds
Rules that report this error #
- 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\IncompatiblePhpDocTypeRule [1]
- PHPStan\Rules\PhpDoc\IncompatiblePropertyHookPhpDocTypeRule [1]
- PHPStan\Rules\PhpDoc\IncompatiblePropertyPhpDocTypeRule [1]