diff --git a/src/Command/Presentation/CognitiveMetricTextRenderer.php b/src/Command/Presentation/CognitiveMetricTextRenderer.php index da6ec19..22a09a0 100644 --- a/src/Command/Presentation/CognitiveMetricTextRenderer.php +++ b/src/Command/Presentation/CognitiveMetricTextRenderer.php @@ -14,51 +14,68 @@ */ class CognitiveMetricTextRenderer { + protected float $scoreThreshold = 0.5; + + /** + * @var array + */ + protected array $keys = [ + 'lineCount', + 'argCount', + 'returnCount', + 'variableCount', + 'propertyCallCount', + 'ifCount', + 'ifNestingLevel', + 'elseCount', + ]; + + /** + * @var array + */ + 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("Class: $className"); - - $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 + * @return arrayse */ protected function prepareTableRow(CognitiveMetrics $metrics): array { @@ -72,21 +89,20 @@ protected function prepareTableRow(CognitiveMetrics $metrics): array 'ifCount' => $metrics->getIfCount(), 'ifNestingLevel' => $metrics->getIfNestingLevel(), 'elseCount' => $metrics->getElseCount(), - 'score' => $metrics->getScore() > 0.5 ? '' . $metrics->getScore() . '' : '' . $metrics->getScore() . '', + 'score' => $metrics->getScore() > $this->scoreThreshold ? '' . $metrics->getScore() . '' : '' . $metrics->getScore() . '', ]; - $keys = [ - 'lineCount', - 'argCount', - 'returnCount', - 'variableCount', - 'propertyCallCount', - 'ifCount', - 'ifNestingLevel', - 'elseCount', - ]; + return $this->formatValues($row, $metrics); + } - foreach ($keys as $key) { + /** + * @param array $row + * @param CognitiveMetrics $metrics + * @return array + */ + protected function formatValues(array $row, CognitiveMetrics $metrics): array + { + foreach ($this->keys as $key) { $getMethod = 'get' . $key; $getMethodWeight = 'get' . $key . 'Weight'; $weight = $metrics->{$getMethodWeight}(); diff --git a/src/PhpParser/CognitiveMetricsVisitor.php b/src/PhpParser/CognitiveMetricsVisitor.php index 6dfbc07..ed12532 100644 --- a/src/PhpParser/CognitiveMetricsVisitor.php +++ b/src/PhpParser/CognitiveMetricsVisitor.php @@ -169,6 +169,7 @@ private function setCurrentClassOnEnterNode(Node $node): bool if ($node->name === null) { return false; } + $this->currentClassName = $this->currentNamespace . '\\' . $node->name->toString(); } @@ -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 = ''; } }