PhpMetrics icon indicating copy to clipboard operation
PhpMetrics copied to clipboard

Cognitive Complexity

Open Isinlor opened this issue 6 years ago • 1 comments

Hello,

Recently I have read about an interesting upgrade of Cyclomatic Complexity called Cognitive Complexity.

Cyclomatic Complexity was initially formulated as a measurement of the “testability and maintainability” of the control flow of a module. While it excels at measuring the former, its underlying mathematical model is unsatisfactory at producing a value that measures the latter. This white paper describes a new metric that breaks from the use of mathematical models to evaluate code in order to remedy Cyclomatic Complexity’s shortcomings and produce a measurement that more accurately reflects the relative difficulty of understanding, and therefore of maintaining methods, classes, and applications.

Here is the paper: https://www.sonarsource.com/docs/CognitiveComplexity.pdf

I have also seen two implementations for PHP:

https://github.com/Symplify/Symplify/blob/2a449a660d48658088e35be8bd565111c230fa78/packages/TokenRunner/src/Analyzer/SnifferAnalyzer/CognitiveComplexityAnalyzer.php

https://github.com/NielsdeBlaauw/php-doc-check/blob/master/src/Metrics/CognitiveComplexity.php

I guess it could be an interesting addition to PHP Metrics.

( Side note: I have submitted similar issue to PHP Mess Detector: https://github.com/phpmd/phpmd/issues/594 )

Isinlor avatar Feb 03 '19 15:02 Isinlor

Issue #409 is giving link of another example of implementation.

niconoe- avatar Dec 27 '19 19:12 niconoe-