14
14
*/
15
15
class CognitiveMetricTextRenderer
16
16
{
17
+ protected float $ scoreThreshold = 0.5 ;
18
+
19
+ /**
20
+ * @var array<string>
21
+ */
22
+ protected array $ keys = [
23
+ 'lineCount ' ,
24
+ 'argCount ' ,
25
+ 'returnCount ' ,
26
+ 'variableCount ' ,
27
+ 'propertyCallCount ' ,
28
+ 'ifCount ' ,
29
+ 'ifNestingLevel ' ,
30
+ 'elseCount ' ,
31
+ ];
32
+
33
+ /**
34
+ * @var array<string>
35
+ */
36
+ protected array $ tableHeaders = [
37
+ "Method Name " ,
38
+ "Lines " ,
39
+ "Arguments " ,
40
+ "Returns " ,
41
+ "Variables " ,
42
+ "Property \nAccesses " ,
43
+ "If " ,
44
+ "If Nesting \nLevel " ,
45
+ "Else " ,
46
+ "Cognitive \nComplexity "
47
+ ];
48
+
17
49
public function render (CognitiveMetricsCollection $ metricsCollection , OutputInterface $ output ): void
18
50
{
19
51
$ groupedByClass = $ metricsCollection ->groupBy ('class ' );
20
52
21
53
foreach ($ groupedByClass as $ className => $ metrics ) {
22
54
$ output ->writeln ("<info>Class: $ className</info> " );
23
-
24
- $ table = new Table ($ output );
25
- $ table ->setStyle ('box ' );
26
- $ table ->setHeaders ($ this ->getTableHeaders ());
27
-
28
- $ rows = [];
29
- foreach ($ metrics as $ metric ) {
30
- $ row = $ this ->prepareTableRow ($ metric );
31
- $ rows [] = $ row ;
32
- }
33
-
34
- $ table ->setRows ($ rows );
35
- $ table ->render ();
55
+ $ this ->renderTable ($ output , $ metrics );
36
56
$ output ->writeln ("" );
37
57
}
38
58
}
39
59
40
- /**
41
- * @return string[]
42
- */
43
- protected function getTableHeaders (): array
60
+ protected function renderTable (OutputInterface $ output , CognitiveMetricsCollection $ metricsCollection ): void
44
61
{
45
- return [
46
- " Method Name " ,
47
- " Lines " ,
48
- " Arguments " ,
49
- " Returns " ,
50
- " Variables " ,
51
- " Property \n Accesses " ,
52
- " If " ,
53
- " If Nesting \n Level " ,
54
- " Else " ,
55
- " Cognitive \n Complexity "
56
- ] ;
62
+ $ table = new Table ( $ output );
63
+ $ table -> setStyle ( ' box ' );
64
+ $ table -> setHeaders ( $ this -> tableHeaders );
65
+
66
+ $ rows = [];
67
+ foreach ( $ metricsCollection as $ metric ) {
68
+ $ rows [] = $ this -> prepareTableRow ( $ metric );
69
+ ;
70
+ }
71
+
72
+ $ table -> setRows ( $ rows );
73
+ $ table -> render () ;
57
74
}
58
75
59
76
/**
60
77
* @param CognitiveMetrics $metrics
61
- * @return array<string, mixed>
78
+ * @return array<string, mixed>se
62
79
*/
63
80
protected function prepareTableRow (CognitiveMetrics $ metrics ): array
64
81
{
@@ -72,21 +89,20 @@ protected function prepareTableRow(CognitiveMetrics $metrics): array
72
89
'ifCount ' => $ metrics ->getIfCount (),
73
90
'ifNestingLevel ' => $ metrics ->getIfNestingLevel (),
74
91
'elseCount ' => $ metrics ->getElseCount (),
75
- 'score ' => $ metrics ->getScore () > 0.5 ? '<error> ' . $ metrics ->getScore () . '</error> ' : '<info> ' . $ metrics ->getScore () . '</info> ' ,
92
+ 'score ' => $ metrics ->getScore () > $ this -> scoreThreshold ? '<error> ' . $ metrics ->getScore () . '</error> ' : '<info> ' . $ metrics ->getScore () . '</info> ' ,
76
93
];
77
94
78
- $ keys = [
79
- 'lineCount ' ,
80
- 'argCount ' ,
81
- 'returnCount ' ,
82
- 'variableCount ' ,
83
- 'propertyCallCount ' ,
84
- 'ifCount ' ,
85
- 'ifNestingLevel ' ,
86
- 'elseCount ' ,
87
- ];
95
+ return $ this ->formatValues ($ row , $ metrics );
96
+ }
88
97
89
- foreach ($ keys as $ key ) {
98
+ /**
99
+ * @param array<string, mixed> $row
100
+ * @param CognitiveMetrics $metrics
101
+ * @return array<string, mixed>
102
+ */
103
+ protected function formatValues (array $ row , CognitiveMetrics $ metrics ): array
104
+ {
105
+ foreach ($ this ->keys as $ key ) {
90
106
$ getMethod = 'get ' . $ key ;
91
107
$ getMethodWeight = 'get ' . $ key . 'Weight ' ;
92
108
$ weight = $ metrics ->{$ getMethodWeight }();
0 commit comments