php-coding-standards
php-coding-standards copied to clipboard
[Feature Request]: Replace cyclomatic complexity with Cognitive Complexity
Is your feature request related to a problem?
Curently we have a rule that enforces a reasonable amount of cyclomatic complexity.
Cyclomatic complexity is a purely aritmetic value, which duplicates the value at every logic bifurcation in code. Usually this is also referred as "how many unit tests you need to write to have 100% coverage".
Cognitive complexity, on the other hand, is an indication of how complex a piece of code is to read and understand.
For example, a match expression with 3 branches has the same cyclomatic complexity of a if/elseif/else structure, but it is easier to read.
if (condition_one()) {
do_one();
} elseif (condition_two()) {
do_two();
} else {
do_else();
}
// VS
match (true) {
condition_one() => do_one(),
condition_two() => do_two(),
default => do_else(),
}
There's a document published by Sonar that can be downloaded here that explains cognitive complexity in detail.
I believe that we should check our codebase for cognitive complexity instead of cyclomatic complexity.
Describe the desired solution
The Slevomat coding standard has a sniff that checks for cognitive complexity, implementing the rules in the Sonar's document mentioned above.
I propose that we use that Slevomat sniff, replacing the current sniff for cyclomatic complexity.
Describe the alternatives that you have considered
Keep cyclomatic complexity sniff.
Implementing both does not make much sense because any code with that makes cyclomatic complexity sniff pass will also make the cognitive complexity pass, unless the cognitive complexity threshold is much higher, but at that point cyclomatic complexity sniff will make no sense.
Additional context
While this still suggest a Slevomat rule, I'm proposing this issue separately from #71 because it is not just about embracing a Slevomat sniff, but also use it to replace another rule we have.
Code of Conduct
- [X] I agree to follow this project's Code of Conduct