Cognitive complexity tells how difficult code is for a reader to understand. This package adds PHPStan rules that report classes and methods that got too complex.
How is cognitive complexity measured?
function get_words_from_number(int $number): string
{
$amountInWords = '';
if ($number === 1) { // + 1
$amountInWords = 'one';
} elseif ($number === 2) { // + 1
$amountInWords = 'couple';
} elseif ($number === 3) { // + 1
$amountInWords = 'a few';
} else { // + 1
$amountInWords = 'a lot';
}
return $amountInWords;
}Every branch makes the reader keep one more path in their head. This function results in cognitive complexity of 4.
What adds complexity:
- +1 for each
if,elseif,else,switch,match, ternary,for,foreach,while,do-whileandcatch - +1 for each sequence of boolean operators, e.g.
$a && $b && $cis +1,$a && $b || $cis +2 - +1 for
gotoandbreak/continuewith a level - +1 per nesting level for structures nested in loops, conditions, closures or
catch
How to keep cognitive complexity on 1? Read Cognitive load is what matters or Sonar paper about cognitive complexity metrics that inspired this repository.
composer require tomasvotruba/cognitive-complexity --devThe package runs on PHP 7.4+.
With PHPStan extension installer, everything is ready to run. The class and function rules are enabled by default.
Adjust the limits in your config, these are the defaults:
# phpstan.neon
parameters:
cognitive_complexity:
class: 40
function: 9Each rule has its own error identifier, so you can ignore it on a specific place:
complexity.classLikecomplexity.functionLike
# phpstan.neon
parameters:
ignoreErrors:
-
identifier: complexity.functionLike
path: src/SomeComplexClass.phpHappy coding!
