Skip to content

SwitchContinuationInLoop ​

error on by default quick-fix

Group: Probable bugs · PhpStorm name: SwitchContinuationInLoopInspection

PHP treats switch as a loop structure for continue, so a bare continue inside a switch only leaves the switch (like break) instead of starting the next iteration of the surrounding loop. continue 2 is what was meant.

Example ​

php
<?php
function dispatch(array $events) {
    while ($e = array_shift($events)) {
        switch ($e->type) {
            case 'skip':
                continue;
            case 'retry':
                if ($e->tries > 3) {
                    continue;
                }
                continue 2;
            case 'batch':
                for ($i = 0; $i < 2; $i++) {
                    continue;
                }
                break;
            case 'later':
                $cb = function () { continue; };
                break;
        }
    }
}
php
<?php
function dispatch(array $events) {
    while ($e = array_shift($events)) {
        switch ($e->type) {
            case 'skip':
                continue 2;
            case 'retry':
                if ($e->tries > 3) {
                    continue 2;
                }
                continue 2;
            case 'batch':
                for ($i = 0; $i < 2; $i++) {
                    continue;
                }
                break;
            case 'later':
                $cb = function () { continue; };
                break;
        }
    }
}

Reported:

  • line 6: Inside 'switch', 'continue' acts like 'break'; use 'continue 2' to reach the loop.
  • line 9: Inside 'switch', 'continue' acts like 'break'; use 'continue 2' to reach the loop.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore SwitchContinuationInLoop

/**
 * @noinspection SwitchContinuationInLoopInspection
 */

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