1.2.255. Could Be Else

Merge opposite conditions into one if/then structure.

When two if/then structures follow each other, using a condition and its opposite, they may be merged into one.

<?php

// Short version
if ($a == 1) {
    $b = 2;
} else {
    $b = 1;
}

// Long version
if ($a == 1) {
    $b = 2;
}

if ($a != 1) {
    $b = 3;
}

?>

1.2.255.1. Suggestions

  • Merge the two conditions into one structure

  • Check if the second condition is still applicable

1.2.255.2. Specs

Short name

Structures/CouldBeElse

Rulesets

All, Analyze, Changed Behavior

Exakat since

1.0.1

PHP Version

All

Severity

Minor

Time To Fix

Instant (5 mins)

Precision

Very high

Features

if-then

Examples

SugarCrm, OpenEMR

Available in

Entreprise Edition, Exakat Cloud