Skip to content

[CodeQuality] Invert ordering comparisons only on int operands - #9059

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-nan-unsafe-comparison-inversion
Nov 24, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-nan-unsafe-comparison-inversion

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

SimplifyDeMorganBinaryRector, NegatedAndsToPositiveOrsRector, SimplifyConditionsRector and UnnecessaryTernaryExpressionRector inverted ordering comparisons regardless of operand types:

!($a > $b)                  // -> $a <= $b
$a >= $b ? false : true     // -> $a < $b
!($a > 20 || $b <= 50)      // -> $a <= 20 && $b > 50

That is not equivalent when an operand can be NAN (every comparison is false, so !(NAN > 1) is true but NAN <= 1 is false), or uncomparable arrays/objects ([1] < ['a' => 1] and [1] >= ['a' => 1] are both false).

Fix: the inversion now lives in BinaryOpManipulator::resolveSafeInversedClass(), which inverts <, <=, >, >= only when both sides are native int. ===, !==, ==, != invert as before.

  • SimplifyDeMorganBinaryRector, NegatedAndsToPositiveOrsRector - unsafe operand is wrapped: !($a > 20) && !($b < $c)
  • SimplifyConditionsRector, UnnecessaryTernaryExpressionRector - skipped

Existing fixtures now use int params where they demonstrate the inversion, new fixtures cover float, mixed and array operands.

@TomasVotruba
TomasVotruba merged commit 48448b9 into main Nov 24, 2026
51 checks passed
@TomasVotruba
TomasVotruba deleted the fix-nan-unsafe-comparison-inversion branch November 24, 2026 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant