SortComparatorReturnsBoolean
warning native on by default quick-fixGroup: Probable bugs
Detect usort, uasort, or uksort with a local comparator whose every returned value is boolean and at least one return is an ordering comparison.
Example
<?php
usort($xs, fn($a, $b) => $a > $b);<?php
usort($xs, fn($a, $b) => $a <=> $b);Reported:
- line 2: Return a three-way integer comparison result.
Detection
- Report usort, uasort, or uksort with a local comparator whose every returned value is boolean and at least one return is an ordering comparison.
- 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 callback bodies, mixed return types, and explicit 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 a sole a > b expression with a <=> b, or a < b with b <=> a, on PHP 7.0+. Do not fix compound predicates or comparisons with extra side effects.
The rewrite changes what the comparator returns, so it is offered only when the sort call provably owns the comparator and an integer result is acceptable:
- F1. The callback argument is itself a closure or arrow function literal (parentheses allowed). A comparator reached through a function name string or a variable can have other callers that rely on its boolean result; report it without a fix.
- F2. The literal declares no return type, or declares
int. Under any other declared type (bool, a nullable or union type,mixed, ...) the integer would be coerced, so that both orderings become true, or rejected under strict types; report it 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:
{
"rules": {
"SortComparatorReturnsBoolean": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
// @custos-ignore SortComparatorReturnsBoolean