Error Identifier: phpstanApi.getTemplateType
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);
use PHPStan\Type\ObjectType;
class MyTypeExtension
{
public function getType(): \PHPStan\Type\Type
{
$objectType = new ObjectType(\ArrayObject::class);
return $objectType->getTemplateType(\ArrayObject::class, 'TItem');
}
}
Why is it reported? #
The call to getTemplateType() on a PHPStan Type object references a template type name that does not exist on the specified class. The class either has no template types at all, or the template type name is misspelled.
In the example above, ArrayObject has template types TKey and TValue, but not TItem.
This rule helps PHPStan extension developers catch mistakes when working with generic types in the PHPStan API.
How to fix it #
Use the correct template type name that is defined on the class:
-return $objectType->getTemplateType(\ArrayObject::class, 'TItem');
+return $objectType->getTemplateType(\ArrayObject::class, 'TValue');
Check the class’s @template PHPDoc tags to find the available template type names.
How to ignore this error #
You can use the identifier phpstanApi.getTemplateType to ignore this error using a comment:
// @phpstan-ignore phpstanApi.getTemplateType
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: phpstanApi.getTemplateType
Rules that report this error #
- PHPStan\Rules\Api\GetTemplateTypeRule [1]