1.2.1269. Unreachable Code

Code may be unreachable, because other instructions prevent its reaching.

For example, it be located after throw, return, exit(), die(), goto, break or continue : this way, it cannot be reached, as the previous instruction will divert the engine to another part of the code.

This is dead code, that may be removed.

<?php

function foo() {
    $a++;
    return $a;
    $b++;      // $b++ can't be reached;
}

function bar() {
    if ($a) {
        return $a;
    } else {
        return $b;
    }
    $b++;      // $b++ can't be reached;
}

foreach($a as $b) {
    $c += $b;
    if ($c > 10) {
        continue 1;
    } else {
        $c--;
        continue;
    }
    $d += $e;   // this can't be reached
}

$a = 1;
goto B;
class foo {}    // Definitions are accessible, but not functioncalls
B:
echo $a;

?>

See also Unreachable code.

1.2.1269.1. Suggestions

  • Remove the unreachable code

  • Remove the blocking expression, and let the rest of the code execute

1.2.1269.2. Specs

Short name

Structures/UnreachableCode

Rulesets

All, Changed Behavior, Dead code, Suggestions

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Instant (5 mins)

Precision

Very high

Features

unreachable-code

ClearPHP

no-dead-code

Available in

Entreprise Edition, Exakat Cloud