PHP

PHP

Made by DeepSource

Function with cyclomatic complexity higher than threshold found PHP-R1006

Anti-pattern
Minor

A function with high cyclomatic complexity can be hard to understand and maintain. Cyclomatic complexity is a software metric that measures the number of independent paths through a function. A higher cyclomatic complexity indicates that the function has more decision points and is more complex.

Functions with high cyclomatic complexity are more likely to have bugs and be harder to test. They may lead to reduced code maintainability and increased development time.

To reduce the cyclomatic complexity of a function, you can:

  • Break the function into smaller, more manageable functions.
  • Refactor complex logic into separate functions or classes.
  • Avoid multiple return paths and deeply nested control expressions.

Bad practice

function fizzbuzz(string $file) { // Complexity 1
    if ($file === null) return; // +1
    $max = 0;
    try {
        $input = file_get_contents($file);
        $max = intval($input);
    } catch (e) { // +1
        return;
    }

    if ($max < 0 || $max === 0 || $max === null) return; // +3

    $i = 0;
    while ($i < $max) { // +1
        switch ($i % 15) {
            case 0: // +1
                print "fizzbuzz";
                break;
            case 3:
            case 6:
            case 9:
            case 12: // +4
                print "fizz";
                break;
            case 5:
            case 10: // +2
                print "buzz";
                break;
            default: print $i;
        };
        $i += 1;
    }
}

Recommended

function get_max_from_file(string $file): ?int {
    if ($file === null) return null; // +1
    $max = 0;
    try {
        $input = file_get_contents($file);
        $max = intval($input);
    } catch (e) { // +1
        return null;
    }

    if ($max <== 0) return null; // +1
    return $max;
}

function fizzbuzz(string $file) { // Complexity 1
    $max = get_max_from_file($file) ?? return;
    $i = 0;
    while ($i < $max) { // +1
        $divby3 = $i % 3 == 0;
        $divby5 = $i % 5 == 0;

        if ($divby3 && $divby5) print "fizzbuzz"; // +2
        else if ($divby3) print "fizz"; // +1
        else if ($divby5) print "buzz"; // +1
        else print $i;

        $i += 1;
    }
}

Issue configuration

Cyclomatic complexity threshold can be configured using the cyclomatic_complexity_threshold meta field in your repository's .deepsource.toml config file.

Configuring this issue is optional. If you don't provide a value, the PHP analyzer will raise issues for functions with complexity higher than the default threshold, which is 15 (Medium) for PHP.

Here's a mapping of risk category to cyclomatic complexity score to help you configure this better:

Risk category Cyclomatic complexity range Recommended action
low 1-5 No action needed.
medium 6-15 Review and monitor.
high 16-25 Review and refactor. It is recommended to add explanatory comments if the function absolutely cannot be changed.
very-high 26-50 Refactor to reduce the complexity.
critical >50 The function must be refactored. Such high complexity can harm testability and readability.