Menu
Error Identifier: match.alwaysTrue
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 = 1;
return match (true) {
$value === 1 => 'one',
$value === 2 => 'two',
default => 'other',
};
}
Why is it reported? #
A match arm comparison always evaluates to true, which means all subsequent arms are unreachable. In the example above, since $value is always 1, the first arm ($value === 1) always matches, making the remaining arms dead code.
How to fix it #
Remove the unreachable arms:
<?php declare(strict_types = 1);
function doFoo(): string
{
$value = 1;
- return match (true) {
- $value === 1 => 'one',
- $value === 2 => 'two',
- default => 'other',
- };
+ return 'one';
}
Or fix the logic so the match subject varies.
How to ignore this error #
You can use the identifier match.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore match.alwaysTrue
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: match.alwaysTrue
Rules that report this error #
- PHPStan\Rules\Comparison\MatchExpressionRule [1]