Error Identifier: staticMethod.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 TypeChecker
{
/**
* @phpstan-assert-if-true int $value
*/
public static function isInt(mixed $value): bool
{
return is_int($value);
}
}
function doFoo(string $value): void
{
if (TypeChecker::isInt($value)) {
// never reached - a string is never an int
}
}
Why is it reported? #
The call to a static method that performs a type check will always evaluate to false because the type of the argument makes it impossible for the check to succeed. PHPStan understands type-checking methods through @phpstan-assert-if-true and @phpstan-assert-if-false PHPDoc tags, as well as methods configured as type-specifying extensions.
In this example, TypeChecker::isInt() asserts that the value is int, but the argument is always string, so the check can never return true.
How to fix it #
Pass the correct variable or value to the type check:
<?php declare(strict_types = 1);
-function doFoo(string $value): void
+function doFoo(string|int $value): void
{
if (TypeChecker::isInt($value)) {
// now this branch can be reached
}
}
Or remove the impossible check if it is not needed:
<?php declare(strict_types = 1);
function doFoo(string $value): void
{
- if (TypeChecker::isInt($value)) {
- // never reached
- }
+ // handle string directly
}
How to ignore this error #
You can use the identifier staticMethod.impossibleType to ignore this error using a comment:
// @phpstan-ignore staticMethod.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: staticMethod.impossibleType
Rules that report this error #
- PHPStan\Rules\Comparison\ImpossibleCheckTypeStaticMethodCallRule [1]