Condition Similar Block
ID |
php.condition_similar_block |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Php |
Tags |
duplicate-branch, reliability |
Description
Reports an if / elseif / else chain in which two branches have identical bodies. When
two branches run exactly the same code, the condition that selects between them has no
observable effect, which is almost always a copy-paste mistake.
Rationale
If two branches of a conditional are identical, then either the wrong block was duplicated when the chain was written, or one branch was never updated after copying. Both cases hide a real bug: the author intended the branches to differ. Collapsing or correcting the duplicate makes the true control flow explicit.
<?php
if ($kind === 'a') { // FLAW — both branches return the same thing
return handle($code);
} else {
return handle($code);
}
if ($kind === 'a') { // OK — branches differ
return handle($code);
} else {
return fallback($code);
}
Remediation
If the branches are genuinely meant to be the same, remove the redundant condition and keep a single unconditional statement. Otherwise, fix the branch that was copied incorrectly so the bodies differ as intended.