PHP

PHP

Made by DeepSource

Use of nested switch statements found PHP-W1091

Anti-pattern
Major

Nested switch statements are difficult to understand and affect code readability. In addition, it is considered a code smell since they are harder to maintain and can easily be confused with the outer switch statement. Therefore it is recommended to avoid using nested switch statements or consider moving the inner switch to another function/method.

Bad practice

switch ($i) {
    case 0:
        switch ($j) {
            case 1:
                // some implementation here..
                break;
        }
        break;
    case 1:
        echo "i equals 1";
        break;
    default:
        echo "default";
}

Recommended

switch ($i) {
    case 0:
        check_val($j);
        break;
    case 1:
        echo "i equals 1";
        break;
    default:
        echo "default";
}

function check_val($j) {
    switch ($j) {
        case 1:
            // some implementation here..
            break;
    }
}