Error Identifier: generics.existingClass
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);
class Item
{
}
/**
* @template Item
*/
class Collection
{
}
Why is it reported? #
A @template tag uses a name that conflicts with an existing class. In the example above, the template type parameter is named Item, but a class with the same name already exists. This creates ambiguity because PHPStan cannot distinguish between the template type parameter and the class when the name is used in type annotations.
How to fix it #
Rename the template type parameter to a name that does not conflict with any existing class:
<?php declare(strict_types = 1);
/**
- * @template Item
+ * @template TItem
*/
class Collection
{
}
A common convention is to prefix template type names with T to avoid conflicts with existing classes.
How to ignore this error #
You can use the identifier generics.existingClass to ignore this error using a comment:
// @phpstan-ignore generics.existingClass
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.existingClass
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]