Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cognitive complexity calculation error #87

Open
bkdotcom opened this issue Oct 27, 2021 · 1 comment
Open

Cognitive complexity calculation error #87

bkdotcom opened this issue Oct 27, 2021 · 1 comment
Assignees

Comments

@bkdotcom
Copy link

I apologize if this isn't the correct repo to report this

CodeClimate reports the Cognitive Complexity of the following function as 14
I believe the value should be 12

It seems that CodeClimate is incorrectly incrementing the continue statements

function testFunction($array)
{
    if (false) {
        return;
    }
    $file = '';
    $line = 0;
    if (false) {
        throw new \RuntimeException('false was true! ');
    }
    foreach ($array as $key => $val) {
        if (\is_int($key)) {
            continue;
        }
        if (\is_string($val)) {
            continue;
        }
        if (\is_array($val)) {
            foreach ($val as $val2) {
                // empty
            }
        }
    }
}

continue; should not increment

continue LABEL; should increment

@bkdotcom
Copy link
Author

bkdotcom commented Jun 12, 2022

The cognitive complexity whitepaper is a bit vague/ambiguous

https://www.sonarsource.com/docs/CognitiveComplexity.pdf

Jumps to labels

goto adds a fundamental increment to Cognitive Complexity, as do break or continue to
a label
and other multi-level jumps such as the break or continue to a number found in
some languages. But because an early return can often make code much clearer, no other
jumps or early exits cause an increment.

B1. Increments

  • goto LABEL, break LABEL, continue LABEL, break NUMBER, continue
    NUMBER

Appendix C has a code examples containing a break; (break without a label)
No increment in assigned/given

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants