Error Identifier: method.impossibleType
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);
class Foo
{
public function isValid(): bool
{
return true;
}
}
function doFoo(Foo $foo): void
{
if ($foo->isValid()) {
if ($foo->isValid()) {
// ...
}
}
}
Why is it reported? #
A method call that acts as a type check will always evaluate to false based on the types PHPStan knows at that point. This means the condition can never be satisfied, so the code inside the branch is dead code.
Common examples include calling is_*() style methods on values whose type has already been narrowed, or calling type-checking methods on values that can never match the checked type.
How to fix it #
Remove the redundant check if the type has already been narrowed:
function doFoo(Foo $foo): void
{
if ($foo->isValid()) {
- if ($foo->isValid()) {
- // ...
- }
+ // ...
}
}
If the method should be able to return different values, fix the return type or the method’s logic so that the check can actually evaluate to false.
How to ignore this error #
You can use the identifier method.impossibleType to ignore this error using a comment:
// @phpstan-ignore method.impossibleType
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: method.impossibleType
Rules that report this error #
- PHPStan\Rules\Comparison\ImpossibleCheckTypeMethodCallRule [1]