diff --git a/src/Command/Presentation/CognitiveMetricTextRenderer.php b/src/Command/Presentation/CognitiveMetricTextRenderer.php index cd2737d..cad9d80 100644 --- a/src/Command/Presentation/CognitiveMetricTextRenderer.php +++ b/src/Command/Presentation/CognitiveMetricTextRenderer.php @@ -38,27 +38,25 @@ public function render(CognitiveMetricsCollection $metricsCollection, array $bas $table->setRows($rows); $table->render(); + $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(); } /** @@ -78,21 +76,20 @@ protected function prepareTableRow(CognitiveMetrics $metrics, array $baseline): '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 = ''; } }