Skip to content

Commit

Permalink
Refactoring the table renderer (#3)
Browse files Browse the repository at this point in the history
* Refactoring the metrics table renderer
* Adding missing data in the visitor
  • Loading branch information
floriankraemer authored Sep 7, 2024
1 parent 4786e74 commit 9bc428c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 42 deletions.
100 changes: 58 additions & 42 deletions src/Command/Presentation/CognitiveMetricTextRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,68 @@
*/
class CognitiveMetricTextRenderer
{
protected float $scoreThreshold = 0.5;

/**
* @var array<string>
*/
protected array $keys = [
'lineCount',
'argCount',
'returnCount',
'variableCount',
'propertyCallCount',
'ifCount',
'ifNestingLevel',
'elseCount',
];

/**
* @var array<string>
*/
protected array $tableHeaders = [
"Method Name",
"Lines",
"Arguments",
"Returns",
"Variables",
"Property\nAccesses",
"If",
"If Nesting\nLevel",
"Else",
"Cognitive\nComplexity"
];

public function render(CognitiveMetricsCollection $metricsCollection, OutputInterface $output): void
{
$groupedByClass = $metricsCollection->groupBy('class');

foreach ($groupedByClass as $className => $metrics) {
$output->writeln("<info>Class: $className</info>");

$table = new Table($output);
$table->setStyle('box');
$table->setHeaders($this->getTableHeaders());

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

$table->setRows($rows);
$table->render();
$this->renderTable($output, $metrics);
$output->writeln("");
}
}

/**
* @return string[]
*/
protected function getTableHeaders(): array
protected function renderTable(OutputInterface $output, CognitiveMetricsCollection $metricsCollection): void
{
return [
"Method Name",
"Lines",
"Arguments",
"Returns",
"Variables",
"Property\nAccesses",
"If",
"If Nesting\nLevel",
"Else",
"Cognitive\nComplexity"
];
$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();
}

/**
* @param CognitiveMetrics $metrics
* @return array<string, mixed>
* @return array<string, mixed>se
*/
protected function prepareTableRow(CognitiveMetrics $metrics): array
{
Expand All @@ -72,21 +89,20 @@ protected function prepareTableRow(CognitiveMetrics $metrics): array
'ifCount' => $metrics->getIfCount(),
'ifNestingLevel' => $metrics->getIfNestingLevel(),
'elseCount' => $metrics->getElseCount(),
'score' => $metrics->getScore() > 0.5 ? '<error>' . $metrics->getScore() . '</error>' : '<info>' . $metrics->getScore() . '</info>',
'score' => $metrics->getScore() > $this->scoreThreshold ? '<error>' . $metrics->getScore() . '</error>' : '<info>' . $metrics->getScore() . '</info>',
];

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

foreach ($keys as $key) {
/**
* @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) {
$getMethod = 'get' . $key;
$getMethodWeight = 'get' . $key . 'Weight';
$weight = $metrics->{$getMethodWeight}();
Expand Down
3 changes: 3 additions & 0 deletions src/PhpParser/CognitiveMetricsVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ private function setCurrentClassOnEnterNode(Node $node): bool
if ($node->name === null) {
return false;
}

$this->currentClassName = $this->currentNamespace . '\\' . $node->name->toString();
}

Expand Down Expand Up @@ -263,6 +264,8 @@ private function writeMetricsOnLeaveNode(Node $node): void
$this->methodMetrics[$method]['if_count'] = $this->ifCount;
$this->methodMetrics[$method]['if_nesting_level'] = $this->maxIfNestingLevel;
$this->methodMetrics[$method]['else_count'] = $this->elseCount;
$this->methodMetrics[$method]['line_count'] = $node->getEndLine() - $node->getStartLine() + 1;
$this->methodMetrics[$method]['arg_count'] = count($node->getParams());
$this->currentMethod = '';
}
}
Expand Down

0 comments on commit 9bc428c

Please sign in to comment.