Menu
Error Identifier: instanceof.alwaysTrue
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(\stdClass $obj): void
{
if ($obj instanceof \stdClass) {
// always entered
}
}
Why is it reported? #
The instanceof check always evaluates to true because the expression is already known to be of the checked type. In the example above, $obj is typed as \stdClass, so $obj instanceof \stdClass is always true. This makes the check redundant.
How to fix it #
Remove the redundant instanceof check:
<?php declare(strict_types = 1);
function doFoo(\stdClass $obj): void
{
- if ($obj instanceof \stdClass) {
- // always entered
- }
+ // Execute the code unconditionally
}
How to ignore this error #
You can use the identifier instanceof.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore instanceof.alwaysTrue
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.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Classes\ImpossibleInstanceOfRule [1]