BETA

Cyclomatic complexity

Cyclomatic complexity is calculated by counting the amount of decision paths. After an if statement, your code can go do 2 different things. Every conditional statement increases the complexity of your program. It's harder to test, likely contains more bugs & is harder to understand when you look at it again.

The cyclomatic complexity is calculated by counting the amount of these conditional statements in the code: ?, &&, ||, and, or, xor, if, elseif, for, foreach, case, while & catch.

if ($user->isLoggedIn()) {
    echo 'Welcome back, ' . $user->getName();
} else {
    echo 'Hi there, stranger!';
}

In the above code example, there are 2 possible code paths: either $user is logged in (in which case a personalized text is displayed), or isn't (and a generic message is shown). It has a cyclomatic complexity of 2.

The more decision paths there are, the harder it becomes to reason about the logic & test it.