Skip to content

PHPStan rules to measure cognitive complexity of your classes and methods

License

Notifications You must be signed in to change notification settings

TomasVotruba/cognitive-complexity

Repository files navigation

Keep Cognitive Complexity Down


Cognitive complexity tells us, how difficult code is to understand by a reader.

How is cognitive compleixty measured?

function sumOfPrimes($max) {
    $total = 0;
    for ($i = 1; $i < $max; ++$i) {     // +1
        for ($j = 2; $j < $i; ++$j) {   // +2
            if ($i % $j === 0) {        // +3
                continue 2;             // +1
            }
        }

        $total += $i;
    }

    return $total;
}

This function uses nesting, conditions and continue back and forth. It's hard to read and has complexity of 7.

How to keep it down and what else is included in measurements? Check Is Your Code Readable By Humans? post to learn it.


Install

composer require tomasvotruba/cognitive-complexity --dev

The package is available on PHP 7.2-8.1 versions in tagged releases.


Usage

With PHPStan extension installer, everything is ready to run.

Enable each item on their own with simple configuration:

# phpstan.neon
parameters:
    cognitive_complexity:
        class: 50
        function: 8

About

PHPStan rules to measure cognitive complexity of your classes and methods

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Contributors 3

  •  
  •  
  •  

Languages