1.2.700. Missing Cases In Switch

It seems that some cases are missing in this switch structure.

When comparing two different switch() structures, it appears that some cases are missing in one of them. The set of cases are almost identical, but one of the values are missing.

Switch() structures using strings as literals are compared in this analysis. When the discrepancy between two lists is below 25%, both switches are reported. In the example, one may argue that the ‘c’ case is actually handled by the ‘default’ case. Otherwise, business logic may request that omission.

<?php

// This switch operates on a, b, c, d and default
switch($a) {
    case 'a': doSomethingA(); break 1;
    case 'b': doSomethingB(); break 1;
    case 'c': doSomethingC(); break 1;
    case 'd': doSomethingD(); break 1;
    default: doNothing();
}

// This switch operates on a, b, d and default
switch($o->p) {
    case 'a': doSomethingA(); break 1;
    case 'b': doSomethingB(); break 1;

    case 'd': doSomethingD(); break 1;
    default: doNothing();
}

?>

1.2.700.1. Suggestions

  • Add the missing cases

  • Add comments to mention that missing cases are processed in the default case

1.2.700.2. Specs

Short name

Structures/MissingCases

Rulesets

All, Analyze

Exakat since

0.10.7

PHP Version

All

Severity

Minor

Time To Fix

Slow (1 hour)

Precision

High

Examples

Tikiwiki

Available in

Entreprise Edition, Exakat Cloud