phpmetrics / PhpMetrics

Beautiful and understandable static analysis tool for PHP
https://phpmetrics.github.io/website/
MIT License
2.48k stars 259 forks source link

Cognitive Complexity #368

Open Isinlor opened 5 years ago

Isinlor commented 5 years ago

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 )

niconoe- commented 4 years ago

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