Error Identifier: function.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);
function doFoo(string $value): void
{
if (is_int($value)) {
// ...
}
}
Why is it reported? #
A type-checking function call will always evaluate to false because the checked type is incompatible with the variable’s type. In the example above, $value is typed as string, so is_int($value) can never be true. This usually indicates a logic error or an incorrect type declaration.
How to fix it #
Fix the type declaration to match the actual range of values, or fix the type check:
-function doFoo(string $value): void
+function doFoo(string|int $value): void
{
if (is_int($value)) {
// ...
}
}
Or fix the condition to check for the correct type:
function doFoo(string $value): void
{
- if (is_int($value)) {
+ if (is_numeric($value)) {
// ...
}
}
How to ignore this error #
You can use the identifier function.impossibleType to ignore this error using a comment:
// @phpstan-ignore function.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: function.impossibleType
Rules that report this error #
- PHPStan\Rules\Comparison\ImpossibleCheckTypeFunctionCallRule [1]