Error Identifier: phpstanApi.instanceofAssumption
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\Type;
use PHPStan\Type\StringType;
function doFoo(Type $type): void
{
if ($type instanceof StringType) { // ERROR: Although StringType is covered by backward compatibility promise, this instanceof assumption might break because it's not guaranteed to always stay the same.
// ...
}
}
Why is it reported? #
This error is reported when an instanceof check is used against a PHPStan class that is covered by the backward compatibility promise, but the specific instanceof assumption might break in a future minor version. While the class itself is part of the public API, PHPStan does not guarantee that a value currently represented by a specific class will always be represented by that same class.
For example, a type that is currently a StringType instance might be represented differently in a future version, even though the StringType class itself still exists.
How to fix it #
Instead of checking for a specific class with instanceof, use the type’s API methods to query its properties:
<?php declare(strict_types = 1);
use PHPStan\Type\Type;
-use PHPStan\Type\StringType;
function doFoo(Type $type): void
{
- if ($type instanceof StringType) {
+ if ($type->isString()->yes()) {
// ...
}
}
If you believe the instanceof check is the correct approach and should be supported, open a discussion at github.com/phpstan/phpstan/discussions.
See also: Backward Compatibility Promise
How to ignore this error #
You can use the identifier phpstanApi.instanceofAssumption to ignore this error using a comment:
// @phpstan-ignore phpstanApi.instanceofAssumption
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.instanceofAssumption
Rules that report this error #
- PHPStan\Rules\Api\ApiInstanceofRule [1]