Skip to content

SortComparatorReturnsFloat ​

warning native on by default quick-fix

Group: Probable bugs

Detect a builtin sort comparator returning a subtraction whose operands are proven floats.

Example ​

php
<?php
usort($xs, fn(float $a, float $b) => $a - $b);
php
<?php
usort($xs, fn(float $a, float $b) => $a <=> $b);

Reported:

  • line 2: Compare float values without subtracting them.

Detection ​

  • Report a builtin sort comparator returning a subtraction whose operands are proven floats.
  • Resolve builtin calls and named arguments before checking the contract. Project-local callees are considered only when their bodies or immutable summaries establish the required facts. The default is enabled, with positive proof required.
  • For flow-dependent conditions, require a reachable source-to-use path with no intervening invalidation. Unknown calls, ambiguous aliases, recursion or analysis-budget exhaustion cannot establish definite facts.

Exceptions (no report) ​

  • Unknown numeric types, integer operands, and explicitly normalized integer comparison results are excluded.
  • Unresolved targets, incomplete required facts, suppressed findings and PHP versions lacking the referenced API or language feature produce no report. User declarations shadowing builtin names are not builtin contracts.

Fix ​

Replace the sole subtraction with the spaceship comparison on PHP 7.0+, preserving operand evaluation order.

The rewrite is offered only when the comparator is an anonymous function or arrow function written directly as the callback argument. A comparator reached through a function name or through a variable holding a closure can also be called elsewhere for the numeric difference, so those shapes are reported without a fix.

A fix is offered only when the concrete prerequisites above are proven. Edits use exact source byte ranges, preserve comments and argument evaluation order, and introduce syntax supported by the configured PHP version. Never invent error-handling policy.

PHP versions ​

Apply only on PHP versions supporting the referenced APIs and syntax (within PHP 5.3–8.5). Builtin behavior and argument contracts follow the configured PHP version. Extension-specific calls require resolved extension API symbols; do not treat same-named application functions as extension calls.

Options ​

None. Enable or disable this rule through normal rule configuration.

Configure ​

In custos.json:

json
{
  "rules": {
    "SortComparatorReturnsFloat": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file):

php
// @custos-ignore SortComparatorReturnsFloat

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.