Compare Value To Itself
ID |
php.compare_value_to_itself |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Php |
Tags |
CWE:570, CWE:571, reliability, suspicious-comparison |
Description
Reports a comparison whose two operands are the same expression, for example $x == $x or
$a > $a. The result is constant regardless of the value, so the comparison cannot do what
it appears to and almost always signals a copy-paste error or a missing qualifier on one side.
Rationale
A guard such as if ($a == $a) is always true and if ($a > $a) is always false, so the
branch it controls runs unconditionally or never. The usual real cause is a duplicated
operand where a sibling field or variable was meant — for instance writing $this→min ==
$this→min instead of $this→min == $this→max. Unlike floating-point comparisons in some
languages, PHP has no legitimate self-comparison idiom (use is_nan() for not-a-number), so
every self comparison is suspect.
<?php
if ($count == $count) { // FLAW — always true
reset();
}
if ($a > $a) { // FLAW — always false
grow();
}
if ($min == $max) { // OK — distinct operands
collapse();
}