Condition Always True
ID |
php.condition_always_true |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Php |
Tags |
reliability, suspicious-comparison |
Description
Reports conditions whose result is fixed at parse time, so the guarded branch either always runs or never runs. The analysis is purely structural and covers three shapes:
-
a literal
true/falseused as anif(orelseif) guard; -
a comparison of two literals, such as
5 > 3,'a' == 'b'or1 === 2, in anif/elseif/while/ ternary condition; -
a single condition that
&&-joins two comparisons of the same expression in a mutually-exclusive way — equality to two different values ($x == 1 && $x == 2) or an impossible numeric range ($x > 5 && $x < 3).
The idiomatic infinite loop while (true) is not flagged, and comparing an expression with
itself ($x == $x) is handled by a separate rule and deliberately excluded here.
Rationale
A condition that cannot vary is a paradox: the author clearly expected it to decide
something, yet it always resolves the same way. Such conditions hide bugs — a misplaced
constant, a typo (== where a variable was meant), or contradictory bounds that silently
disable a branch. Surfacing them exposes dead branches and logic errors that tests rarely
catch because the branch is taken (or skipped) unconditionally.
<?php
if (true) { // FLAW — literal guard, branch always runs
doSomething();
}
if (5 > 3) { // FLAW — two constants, always true
doSomething();
}
if ($x == 1 && $x == 2) { // FLAW — $x cannot equal both 1 and 2
impossible();
}
if ($x > 5 && $x < 10) { // OK — a valid range that depends on $x
inRange();
}
Remediation
Decide what the condition was meant to test. Replace a stray literal with the real predicate, fix the operand that was meant to be a variable, or correct the contradictory bounds. If the branch is genuinely unconditional, remove the condition (or the dead branch) outright.