Skip to content

Commit

Permalink
Fixing the output table
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankraemer committed Sep 21, 2024
1 parent d600b28 commit 292c238
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions src/Command/Presentation/CognitiveMetricTextRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,27 @@ public function render(CognitiveMetricsCollection $metricsCollection, array $bas

$table->setRows($rows);
$table->render();

$output->writeln("");
}
}

protected function renderTable(OutputInterface $output, CognitiveMetricsCollection $metricsCollection): void
/**
* @return string[]
*/
protected function getTableHeaders(): array
{
$table = new Table($output);
$table->setStyle('box');
$table->setHeaders($this->tableHeaders);

$rows = [];
foreach ($metricsCollection as $metric) {
$rows[] = $this->prepareTableRow($metric);
;
}

$table->setRows($rows);
$table->render();
return [
"Method Name",
"Lines",
"Arguments",
"Returns",
"Variables",
"Property\nAccesses",
"If",
"If Nesting\nLevel",
"Else",
"Cognitive\nComplexity"
];
}

/**
Expand All @@ -76,20 +78,21 @@ protected function prepareTableRow(CognitiveMetrics $metrics, array $baseline):
'ifCount' => $metrics->getIfCount(),
'ifNestingLevel' => $metrics->getIfNestingLevel(),
'elseCount' => $metrics->getElseCount(),
'score' => $metrics->getScore() > $this->scoreThreshold ? '<error>' . $metrics->getScore() . '</error>' : '<info>' . $metrics->getScore() . '</info>',
'score' => $metrics->getScore() > 0.5 ? '<error>' . $metrics->getScore() . '</error>' : '<info>' . $metrics->getScore() . '</info>',
];

return $this->formatValues($row, $metrics);
}
$keys = [
'lineCount',
'argCount',
'returnCount',
'variableCount',
'propertyCallCount',
'ifCount',
'ifNestingLevel',
'elseCount',
];

/**
* @param array<string, mixed> $row
* @param CognitiveMetrics $metrics
* @return array<string, mixed>
*/
protected function formatValues(array $row, CognitiveMetrics $metrics): array
{
foreach ($this->keys as $key) {
foreach ($keys as $key) {
$getMethod = 'get' . $key;
$getMethodWeight = 'get' . $key . 'Weight';
$weight = $metrics->{$getMethodWeight}();
Expand Down

0 comments on commit 292c238

Please sign in to comment.