1.2.268. Could Be Spaceship

The spaceship operator compares values and returns 0 for equality, 1 for superior and -1 for inferior.

It is the same as below, and prevents lots of code.

<?php

if ($a) {
    return 1;
} elseif ($b) {
    return 0;
} else {
    return -1;
}
?>

See also spaceship operator and Remembering what spaceship operator do on comparison in PHP.

1.2.268.1. Suggestions

  • Adopt the spaceship operator

1.2.268.2. Specs

Short name

Structures/CouldBeSpaceship

Rulesets

All, Analyze, Suggestions

Exakat since

2.4.0

PHP Version

With PHP 7.0 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

spaceship

Available in

Entreprise Edition, Exakat Cloud