Error Identifier: instanceof.invalidExprType
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);
function doFoo(object $obj, int $type): void
{
if ($obj instanceof $type) {
// ...
}
}
Why is it reported? #
The right-hand side of instanceof is an expression whose type is neither a string nor an object. PHP requires the right-hand side of instanceof to be either a class name (string) or an object instance. Using a value of type int, array, or other non-string/non-object types will result in a runtime error.
How to fix it #
Pass a class name string or an object:
<?php declare(strict_types = 1);
-function doFoo(object $obj, int $type): void
+function doFoo(object $obj, string $type): void
{
if ($obj instanceof $type) {
// ...
}
}
How to ignore this error #
You can use the identifier instanceof.invalidExprType to ignore this error using a comment:
// @phpstan-ignore instanceof.invalidExprType
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: instanceof.invalidExprType
Rules that report this error #
- PHPStan\Rules\Classes\ImpossibleInstanceOfRule [1]