Skip to content

DegradedSwitch ​

info on by default

Group: Control flow · PhpStorm name: DegradedSwitchInspection

A switch with only a default branch, or with a single case (optionally plus default), is just an if / if-else (or plain code) in disguise and reads better rewritten that way.

Example ​

php
<?php
function route($verb) {
    switch ($verb) {
        case 'GET':
            serve();
    }
    switch (strtoupper($verb)) {
        default:
            reject();
            break;
        case 'POST':
            store();
            break;
    }
    switch ($verb):
        default:
            fallback();
    endswitch;

    switch ($verb) {}
    switch ($verb) {
        case 'PUT':
        case 'PATCH':
            update();
    }
}

Reported:

  • line 3: This switch has a single case; an 'if' is clearer.
  • line 7: This switch has a single case and a default; an 'if'/'else' is clearer.
  • line 15: This switch only has a default branch; keep just its body.

Configure ​

In custos.json:

json
{
  "rules": {
    "DegradedSwitch": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore DegradedSwitch

/**
 * @noinspection DegradedSwitchInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.