phauthentic/cognitive-code-analysis

1.0.0-beta3 2024-09-21 22:47 UTC

This package is auto-updated.

Last update: 2024-09-24 22:50:36 UTC


README

认知代码分析是一种通过关注人类认知与代码交互的方式来理解和改进代码的方法。它强调通过考虑编写和与代码一起工作的开发者的认知过程,使代码更加易于阅读、理解和维护。

“20世纪50年代,人类短期或工作记忆被估计为7±2个变量。一个更近的估计是4±1个结构。一旦超过四个结构的限制,决策质量通常就会下降。”

来源:人类认知限制。广泛、一致、临床应用生理原则需要决策支持

运行它

认知复杂度分析

php analyse.php metrics:cognitive <path-to-folder>

生成报告,支持的类型有 jsoncsvhtml

php analyse.php metrics:cognitive <path-to-folder> --report-type json --report-file cognitive.json

您还可以传递一个基线文件来比较结果。JSON报告用作基线。输出将显示如果有值被更改,则会显示增量。

php analyse.php metrics:cognitive <path-to-folder> --baseline cognitive.json

Halstead复杂度分析

php analyse.php metrics:halstead <path-to-folder>

文档

资源

这些页面和论文提供了有关认知限制、可读性和对业务影响的更多信息。

示例

认知指标

Wordpress WP_Debug_Data

Class: \WP_Debug_Data
┌───────────────────┬──────────────┬───────────┬─────────┬─────────────┬────────────┬────────────┬────────────┬────────────┬────────────┐
│ Method Name       │ Lines        │ Arguments │ Returns │ Variables   │ Property   │ If         │ If Nesting │ Else       │ Cognitive  │
│                   │              │           │         │             │ Accesses   │            │ Level      │            │ Complexity │
├───────────────────┼──────────────┼───────────┼─────────┼─────────────┼────────────┼────────────┼────────────┼────────────┼────────────┤
│ check_for_updates │ 6 (0)        │ 0 (0)     │ 0 (0)   │ 0 (0)       │ 0 (0)      │ 0 (0)      │ 0 (0)      │ 0 (0)      │ 0          │
│ debug_data        │ 1230 (6.373) │ 0 (0)     │ 1 (0)   │ 105 (3.073) │ 20 (0.788) │ 58 (4.025) │ 3 (1.099)  │ 33 (3.497) │ 18.855     │
│ get_wp_constants  │ 144 (3.761)  │ 0 (0)     │ 1 (0)   │ 9 (0.875)   │ 0 (0)      │ 5 (1.099)  │ 1 (0)      │ 5 (1.609)  │ 7.345      │
│ get_wp_filesystem │ 60 (0)       │ 0 (0)     │ 1 (0)   │ 9 (0.875)   │ 0 (0)      │ 1 (0)      │ 1 (0)      │ 0 (0)      │ 0.875      │
│ get_mysql_var     │ 15 (0)       │ 1 (0)     │ 2 (0)   │ 2 (0)       │ 0 (0)      │ 1 (0)      │ 1 (0)      │ 0 (0)      │ 0          │
│ format            │ 60 (0)       │ 2 (0)     │ 1 (0)   │ 11 (1.03)   │ 0 (0)      │ 5 (1.099)  │ 1 (0)      │ 5 (1.609)  │ 3.738      │
│ get_database_size │ 14 (0)       │ 0 (0)     │ 1 (0)   │ 4 (0.336)   │ 1 (0)      │ 1 (0)      │ 1 (0)      │ 0 (0)      │ 0.336      │
│ get_sizes         │ 125 (3.512)  │ 0 (0)     │ 1 (0)   │ 14 (1.224)  │ 0 (0)      │ 9 (1.946)  │ 2 (0.693)  │ 5 (1.609)  │ 8.984      │
└───────────────────┴──────────────┴───────────┴─────────┴─────────────┴────────────┴────────────┴────────────┴────────────┴────────────┘

Doctrine Paginator

Class: Doctrine\ORM\Tools\Pagination\Paginator
┌───────────────────────────────────────────┬────────┬───────────┬─────────┬───────────┬──────────┬───────┬────────────┬───────────┬────────────┐
│ Method Name                               │ Lines  │ Arguments │ Returns │ Variables │ Property │ If    │ If Nesting │ Else      │ Cognitive  │
│                                           │        │           │         │           │ Accesses │       │ Level      │           │ Complexity │
├───────────────────────────────────────────┼────────┼───────────┼─────────┼───────────┼──────────┼───────┼────────────┼───────────┼────────────┤
│ __construct                               │ 10 (0) │ 2 (0)     │ 0 (0)   │ 1 (0)     │ 1 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ getQuery                                  │ 4 (0)  │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ getFetchJoinCollection                    │ 4 (0)  │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ getUseOutputWalkers                       │ 4 (0)  │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ setUseOutputWalkers                       │ 6 (0)  │ 1 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ count                                     │ 12 (0) │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ getIterator                               │ 46 (0) │ 0 (0)     │ 2 (0)   │ 9 (0.875) │ 2 (0)    │ 3 (0) │ 2 (0.693)  │ 2 (0.693) │ 2.262      │
│ cloneQuery                                │ 13 (0) │ 1 (0)     │ 1 (0)   │ 3 (0.182) │ 0 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0.182      │
│ useOutputWalker                           │ 8 (0)  │ 1 (0)     │ 2 (0)   │ 1 (0)     │ 1 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ appendTreeWalker                          │ 11 (0) │ 2 (0)     │ 0 (0)   │ 1 (0)     │ 0 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ getCountQuery                             │ 25 (0) │ 0 (0)     │ 1 (0)   │ 4 (0.336) │ 1 (0)    │ 2 (0) │ 1 (0)      │ 1 (0)     │ 0.336      │
│ unbindUnusedQueryParams                   │ 17 (0) │ 1 (0)     │ 0 (0)   │ 6 (0.588) │ 0 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0.588      │
│ convertWhereInIdentifiersToDatabaseValues │ 11 (0) │ 1 (0)     │ 1 (0)   │ 5 (0.47)  │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0.47       │
└───────────────────────────────────────────┴────────┴───────────┴─────────┴───────────┴──────────┴───────┴────────────┴───────────┴────────────┘

Halstead指标

Class: WP_Debug_Data
File: /home/florian/projects/cognitive-code-checker/tests/TestCode/FileWithTwoClasses.php
┌─────────────┬─────────────┬───────────┬──────────┬─────────┬────────────┬──────────┬────────────┬───────────┬──────────┐
│ n1 Distinct │ n2 Distinct │ Total     │ Total    │ Program │ Program    │ Volume   │ Difficulty │ Effort    │ Possible │
│ Operators   │ Operands    │ Operators │ Operands │ Length  │ Vocabulary │          │            │           │ Bugs     │
├─────────────┼─────────────┼───────────┼──────────┼─────────┼────────────┼──────────┼────────────┼───────────┼──────────┤
│ 13          │ 563         │ 869       │ 2123     │ 2992    │ 576        │ 27436.42 │ 24.51      │ 672484.58 │ 9.15     │
└─────────────┴─────────────┴───────────┴──────────┴─────────┴────────────┴──────────┴────────────┴───────────┴──────────┘

Class: Doctrine\ORM\Tools\Pagination\Paginator
File: /home/florian/projects/cognitive-code-checker/tests/TestCode/FileWithTwoClasses.php
┌─────────────┬─────────────┬───────────┬──────────┬─────────┬────────────┬─────────┬────────────┬──────────┬──────────┐
│ n1 Distinct │ n2 Distinct │ Total     │ Total    │ Program │ Program    │ Volume  │ Difficulty │ Effort   │ Possible │
│ Operators   │ Operands    │ Operators │ Operands │ Length  │ Vocabulary │         │            │          │ Bugs     │
├─────────────┼─────────────┼───────────┼──────────┼─────────┼────────────┼─────────┼────────────┼──────────┼──────────┤
│ 7           │ 36          │ 108       │ 161      │ 269     │ 43         │ 1459.67 │ 15.65      │ 22847.82 │ 0.49     │
└─────────────┴─────────────┴───────────┴──────────┴─────────┴────────────┴─────────┴────────────┴──────────┴──────────┘

许可

版权所有 Florian Krämer

GPL3许可 下许可。