Skip to content

Commit 9bc428c

Browse files
Refactoring the table renderer (#3)
* Refactoring the metrics table renderer * Adding missing data in the visitor
1 parent 4786e74 commit 9bc428c

File tree

2 files changed

+61
-42
lines changed

2 files changed

+61
-42
lines changed

src/Command/Presentation/CognitiveMetricTextRenderer.php

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,68 @@
1414
*/
1515
class CognitiveMetricTextRenderer
1616
{
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+
1749
public function render(CognitiveMetricsCollection $metricsCollection, OutputInterface $output): void
1850
{
1951
$groupedByClass = $metricsCollection->groupBy('class');
2052

2153
foreach ($groupedByClass as $className => $metrics) {
2254
$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);
3656
$output->writeln("");
3757
}
3858
}
3959

40-
/**
41-
* @return string[]
42-
*/
43-
protected function getTableHeaders(): array
60+
protected function renderTable(OutputInterface $output, CognitiveMetricsCollection $metricsCollection): void
4461
{
45-
return [
46-
"Method Name",
47-
"Lines",
48-
"Arguments",
49-
"Returns",
50-
"Variables",
51-
"Property\nAccesses",
52-
"If",
53-
"If Nesting\nLevel",
54-
"Else",
55-
"Cognitive\nComplexity"
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();
5774
}
5875

5976
/**
6077
* @param CognitiveMetrics $metrics
61-
* @return array<string, mixed>
78+
* @return array<string, mixed>se
6279
*/
6380
protected function prepareTableRow(CognitiveMetrics $metrics): array
6481
{
@@ -72,21 +89,20 @@ protected function prepareTableRow(CognitiveMetrics $metrics): array
7289
'ifCount' => $metrics->getIfCount(),
7390
'ifNestingLevel' => $metrics->getIfNestingLevel(),
7491
'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>',
7693
];
7794

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+
}
8897

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) {
90106
$getMethod = 'get' . $key;
91107
$getMethodWeight = 'get' . $key . 'Weight';
92108
$weight = $metrics->{$getMethodWeight}();

src/PhpParser/CognitiveMetricsVisitor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ private function setCurrentClassOnEnterNode(Node $node): bool
169169
if ($node->name === null) {
170170
return false;
171171
}
172+
172173
$this->currentClassName = $this->currentNamespace . '\\' . $node->name->toString();
173174
}
174175

@@ -263,6 +264,8 @@ private function writeMetricsOnLeaveNode(Node $node): void
263264
$this->methodMetrics[$method]['if_count'] = $this->ifCount;
264265
$this->methodMetrics[$method]['if_nesting_level'] = $this->maxIfNestingLevel;
265266
$this->methodMetrics[$method]['else_count'] = $this->elseCount;
267+
$this->methodMetrics[$method]['line_count'] = $node->getEndLine() - $node->getStartLine() + 1;
268+
$this->methodMetrics[$method]['arg_count'] = count($node->getParams());
266269
$this->currentMethod = '';
267270
}
268271
}

0 commit comments

Comments
 (0)