NanCheckedWithEquality
warning native on by default quick-fixGroup: Probable bugs
NaN does not equal itself, so equality comparisons cannot recognize it. Use is_nan when testing whether a numeric value is NaN.
Example
<?php
function f(float $value) { if($value===NAN) { echo "bad"; } }<?php
function f(float $value) { if(\is_nan($value)) { echo "bad"; } }Reported:
- line 2: Use is_nan to test for NaN.
Detection
- An equality or inequality comparison has one operand resolved NAN and the other an arbitrary expression. == and === are always false; != and !== always true for NaN. Highlight the comparison.
- Resolve builtin functions, classes, constants and methods; user-defined lookalikes are excluded. Follow only bounded local proof in one lexical scope.
- Emit one finding per violating operation, not per supporting evidence statement. When the proof is inconclusive, nothing is reported.
Exceptions (no report)
- Shadowed NAN constants and already-correct is_nan calls are excluded.
- Both standard suppression forms suppress this native ID.
Fix
F1. Replace equality with \is_nan(otherOperand), and inequality with !\is_nan(otherOperand). Preserve the operand bytes and evaluate it once. Do not fix operands with unknown nonnumeric type: is_nan can reject/coerce them. Offer only when type evidence proves int or float, or leave the finding without a fix. If replacing the comparison would discard comments outside the retained operand, suppress the fix.
PHP versions
Available throughout supported PHP 5.3–8.5. The scalar-type declaration in the example needs PHP 7.0; numeric expression inference can establish a safe fix on earlier versions.
Configure
In custos.json:
{
"rules": {
"NanCheckedWithEquality": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
// @custos-ignore NanCheckedWithEquality