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(int $i): void
{
switch ($i) {
case 'foo':
break;
}
}
Why is it reported? #
The comparison between the switch subject and this case value is always false, so the case body can never execute. This happens when the case value can never be loosely equal (==) to any possible value of the subject — for example comparing an int subject against the string 'foo', or listing a case whose value falls outside the subject’s known set of values:
<?php declare(strict_types = 1);
/** @param 1|2|3 $i */
function doFoo(int $i): void
{
switch ($i) {
case 4: // int is never 4, always false
break;
case 1:
break;
}
}
It can also appear when previous cases have already exhausted every possible value of the subject, leaving a later case impossible to reach.
How to fix it #
Fix the case value so it can actually match the subject:
switch ($i) {
- case 'foo':
+ case 1:
break;
}
Remove the case entirely if it is dead code:
switch ($i) {
- case 'foo':
- break;
}
If the subject can legitimately hold more types or values than PHPStan infers, widen its declared type so the case becomes reachable. This rule respects the treatPhpDocTypesAsCertain configuration parameter — when the type mismatch comes only from PHPDoc types, setting it to false relaxes the check.
How to ignore this error #
You can use the identifier switch.alwaysFalse to ignore this error using a comment:
// @phpstan-ignore switch.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: switch.alwaysFalse
Rules that report this error #
- PHPStan\Rules\Comparison\SwitchConditionRule [1]