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(): void
{
$x = 0;
if ($x) {
echo 'never reached';
}
}
Why is it reported? #
The if condition is always false based on the types and values PHPStan has inferred at that point in the code. The body of the if statement will never execute, making it dead code. This usually points to a logic error or redundant check.
In the example above, $x is always 0 (falsy), so the if body is unreachable.
How to fix it #
Review the surrounding logic and either remove the dead branch or fix the condition:
<?php declare(strict_types = 1);
function doFoo(): void
{
- $x = 0;
- if ($x) {
- echo 'never reached';
- }
+ echo 'always reached';
}
Or fix the variable assignment so the condition can be true:
<?php declare(strict_types = 1);
-function doFoo(): void
+function doFoo(int $x): void
{
- $x = 0;
if ($x) {
- echo 'never reached';
+ echo 'nonzero';
}
}
How to ignore this error #
You can use the identifier if.alwaysFalse to ignore this error using a comment:
// @phpstan-ignore if.alwaysFalse
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: if.alwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\IfConstantConditionRule [1]