Error Identifier: phpstanApi.instanceofType
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\StringType;
use PHPStan\Type\Type;
function processType(Type $type): void
{
if ($type instanceof StringType) {
// ...
}
}
Why is it reported? #
Using instanceof checks against specific PHPStan Type classes is error-prone and deprecated. PHPStan’s type system uses composed types (unions, intersections, constant types), so checking for a specific class often misses valid cases. For example, a ConstantStringType is a string type, but $type instanceof StringType would be false.
The PHPStan Type interface provides dedicated methods that correctly handle all type compositions.
Learn more: Why Is instanceof *Type Wrong and Getting Deprecated
How to fix it #
Use the recommended Type interface method instead of instanceof:
use PHPStan\Type\Type;
function processType(Type $type): void
{
- if ($type instanceof StringType) {
+ if ($type->isString()->yes()) {
// ...
}
}
Consult the error message for which method to use. Common replacements include:
instanceof StringType– useType::isString()instanceof IntegerType– useType::isInteger()instanceof ArrayType– useType::isArray()orType::getArrays()instanceof NullType– useType::isNull()instanceof ObjectType– useType::isObject()orType::getObjectClassNames()instanceof ConstantStringType– useType::getConstantStrings()instanceof BooleanType– useType::isBoolean()
How to ignore this error #
You can use the identifier phpstanApi.instanceofType to ignore this error using a comment:
// @phpstan-ignore phpstanApi.instanceofType
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.instanceofType