A condition in an if-elseif chain repeats an earlier one

tx0/php:duplicate-conditiondefaultwarningreliability/control-flowdepth fastbug

What it finds

if ($a) ... elseif ($a) ...: a condition repeated further down a chain of if, elseif and else if can never be true there, since the first one already took the case. Conditions that call something are left alone, since a call can give a different answer the second time.

From the rule's source, duplicate_condition.rs.

Example from the rule's tests
<?phpfunction classify($n, $s){    if ($n > 10) {        return 'big';    } elseif ($n > 5) {        return 'medium';    } elseif ($n > 10) {tx0 reports: the condition $n > 10 was already checked above        return 'huge';    } else if ($s === 'x') {        return 'x';    } else if ($s === 'x') {tx0 reports: the condition $s === 'x' was already checked above        return 'y';    }    if (next_token()) {        return 1;    } elseif (next_token()) {        return 2;    }    return 'small';}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/php:duplicate-condition" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/php:duplicate-condition] reason="..."

tx0 explain tx0/php:duplicate-condition shows the same in the terminal. Found a false positive? Report it.