Menu

← Back to phpstanApi.*

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 – use Type::isString()
  • instanceof IntegerType – use Type::isInteger()
  • instanceof ArrayType – use Type::isArray() or Type::getArrays()
  • instanceof NullType – use Type::isNull()
  • instanceof ObjectType – use Type::isObject() or Type::getObjectClassNames()
  • instanceof ConstantStringType – use Type::getConstantStrings()
  • instanceof BooleanType – use Type::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

Rules that report this error #

  • PHPStan\Rules\Api\ApiInstanceofTypeRule [1] [2]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.