1.2.1178. Switch With Too Many Default

Switch statements should only hold one default, not more. Check the code and remove the extra default.

PHP 7.0 won’t compile a script that allows for several default cases.

Multiple default happens often with large switch().

<?php

switch($a) {
    case 1 :
        break;
    default :
        break;
    case 2 :
        break;
    default :  // This default is never reached
        break;
}

?>

1.2.1178.1. Suggestions

  • Remove the useless default : it may be the first, or the last. In case of ambiguity, keep the first, as it is the one being used at the moment.

1.2.1178.2. Specs

Short name

Structures/SwitchWithMultipleDefault

Rulesets

All, CompatibilityPHP53, CompatibilityPHP54, CompatibilityPHP55, CompatibilityPHP56

Exakat since

0.8.4

PHP Version

With PHP 7.0 and older

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

switch

Available in

Entreprise Edition, Exakat Cloud