From 10c3a47a26d103d124855800a3f904f216eec481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Tue, 24 Sep 2024 21:16:34 +0200 Subject: [PATCH] Refactoring the baseline handling. --- src/Business/Cognitive/BaselineService.php | 54 ++++++++++++ src/Business/Cognitive/CognitiveMetrics.php | 84 ++++++++++++------- src/Business/Cognitive/Delta.php | 9 +- .../Cognitive/Exporter/JsonExporter.php | 48 +++++------ src/Command/CognitiveMetricsCommand.php | 42 +++------- .../CognitiveMetricTextRenderer.php | 54 +++--------- .../Cognitive/CognitiveMetricsTest.php | 70 ++++++++-------- .../Cognitive/Exporter/JsonExporterTest.php | 20 ++--- 8 files changed, 208 insertions(+), 173 deletions(-) create mode 100644 src/Business/Cognitive/BaselineService.php diff --git a/src/Business/Cognitive/BaselineService.php b/src/Business/Cognitive/BaselineService.php new file mode 100644 index 0000000..b82a5fe --- /dev/null +++ b/src/Business/Cognitive/BaselineService.php @@ -0,0 +1,54 @@ +> $baseline + */ + public function calculateDeltas(CognitiveMetricsCollection $metricsCollection, array $baseline): void + { + foreach ($baseline as $class => $data) { + foreach ($data['methods'] as $methodName => $methodData) { + $metrics = $metricsCollection->getClassWithMethod($class, $methodName); + if (!$metrics) { + continue; + } + + $previousMetrics = CognitiveMetrics::fromArray($methodData); + $metrics->calculateDeltas($previousMetrics); + } + } + } + + + /** + * Loads the baseline file and returns the data as an array. + * + * @param string $baselineFile + * @return array> $baseline + * @throws \JsonException + */ + public function loadBaseline(string $baselineFile): array + { + if (!file_exists($baselineFile)) { + throw new RuntimeException('Baseline file does not exist.'); + } + + $baseline = file_get_contents($baselineFile); + if ($baseline === false) { + throw new RuntimeException('Failed to read baseline file.'); + } + + return json_decode($baseline, true, 512, JSON_THROW_ON_ERROR); + } +} diff --git a/src/Business/Cognitive/CognitiveMetrics.php b/src/Business/Cognitive/CognitiveMetrics.php index 9e50f75..ccdc545 100644 --- a/src/Business/Cognitive/CognitiveMetrics.php +++ b/src/Business/Cognitive/CognitiveMetrics.php @@ -12,8 +12,23 @@ */ class CognitiveMetrics implements JsonSerializable { - private string $class = ''; - private string $method = ''; + /** + * @var array + */ + private array $metrics = [ + 'lineCount', + 'argCount', + 'returnCount', + 'variableCount', + 'propertyCallCount', + 'ifCount', + 'ifNestingLevel', + 'elseCount' + ]; + + private string $class; + private string $method; + private int $lineCount = 0; private int $argCount = 0; private int $returnCount = 0; @@ -49,25 +64,37 @@ public function __construct(array $metrics) { $this->assertArrayKeyIsPresent($metrics, 'class'); $this->assertArrayKeyIsPresent($metrics, 'method'); - $this->assertArrayKeyIsPresent($metrics, 'lineCount'); - $this->assertArrayKeyIsPresent($metrics, 'argCount'); - $this->assertArrayKeyIsPresent($metrics, 'returnCount'); - $this->assertArrayKeyIsPresent($metrics, 'variableCount'); - $this->assertArrayKeyIsPresent($metrics, 'propertyCallCount'); - $this->assertArrayKeyIsPresent($metrics, 'ifCount'); - $this->assertArrayKeyIsPresent($metrics, 'ifNestingLevel'); - $this->assertArrayKeyIsPresent($metrics, 'elseCount'); - - $this->class = $metrics['class']; $this->method = $metrics['method']; - $this->lineCount = $metrics['lineCount']; - $this->argCount = $metrics['argCount']; - $this->returnCount = $metrics['returnCount']; - $this->variableCount = $metrics['variableCount']; - $this->propertyCallCount = $metrics['propertyCallCount']; - $this->ifCount = $metrics['ifCount']; - $this->ifNestingLevel = $metrics['ifNestingLevel']; - $this->elseCount = $metrics['elseCount']; + $this->class = $metrics['class']; + + $this->setRequiredMetricProperties($metrics); + $this->setOptionalMetricProperties($metrics); + } + + /** + * @param array $metrics + * @return void + */ + private function setRequiredMetricProperties(array $metrics): void + { + foreach ($this->metrics as $metricName) { + $this->assertArrayKeyIsPresent($metrics, $metricName); + $this->$metricName = $metrics[$metricName]; + } + } + + /** + * @param array $metrics + * @return void + */ + private function setOptionalMetricProperties(array $metrics): void + { + foreach ($this->metrics as $metricName) { + $property = $metricName . 'Weight'; + if (array_key_exists($property, $metrics)) { + $this->$property = $metrics[$property]; + } + } } private function assertSame(self $other): void @@ -93,17 +120,16 @@ public function calculateDeltas(self $other): void { $this->assertSame($other); - $this->lineCountWeightDelta = new Delta($this->lineCountWeight, $other->getLineCountWeight()); - $this->argCountWeightDelta = new Delta($this->argCountWeight, $other->getArgCountWeight()); - $this->returnCountWeightDelta = new Delta($this->returnCountWeight, $other->getReturnCountWeight()); - $this->variableCountWeightDelta = new Delta($this->variableCountWeight, $other->getVariableCountWeight()); - $this->propertyCallCountWeightDelta = new Delta($this->propertyCallCountWeight, $other->getPropertyCallCountWeight()); - $this->ifCountWeightDelta = new Delta($this->ifCountWeight, $other->getIfCountWeight()); - $this->ifNestingLevelWeightDelta = new Delta($this->ifNestingLevelWeight, $other->getIfNestingLevelWeight()); - $this->elseCountWeightDelta = new Delta($this->elseCountWeight, $other->getElseCountWeight()); + $this->lineCountWeightDelta = new Delta($other->getLineCountWeight(), $this->lineCountWeight); + $this->argCountWeightDelta = new Delta($other->getArgCountWeight(), $this->argCountWeight); + $this->returnCountWeightDelta = new Delta($other->getReturnCountWeight(), $this->returnCountWeight); + $this->variableCountWeightDelta = new Delta($other->getVariableCountWeight(), $this->variableCountWeight); + $this->propertyCallCountWeightDelta = new Delta($other->getPropertyCallCountWeight(), $this->propertyCallCountWeight); + $this->ifCountWeightDelta = new Delta($other->getIfCountWeight(), $this->ifCountWeight); + $this->ifNestingLevelWeightDelta = new Delta($other->getIfNestingLevelWeight(), $this->ifNestingLevelWeight); + $this->elseCountWeightDelta = new Delta($other->getElseCountWeight(), $this->elseCountWeight); } - /** * @param array $metrics * @return self diff --git a/src/Business/Cognitive/Delta.php b/src/Business/Cognitive/Delta.php index 534e948..a0f0d1b 100644 --- a/src/Business/Cognitive/Delta.php +++ b/src/Business/Cognitive/Delta.php @@ -16,12 +16,12 @@ public function __construct(float $before, float $after) { if ($before < $after) { $this->hasIncreased = true; - $this->difference = $after - $before; // Positive difference for increase + $this->difference = $after - $before; return; } $this->hasIncreased = false; - $this->difference = $after - $before; // Negative difference for decrease or zero + $this->difference = $after - $before; } public function getValue(): float @@ -38,4 +38,9 @@ public function __toString(): string { return (string)$this->difference; } + + public function hasNotChanged(): bool + { + return $this->difference === 0.0; + } } diff --git a/src/Business/Cognitive/Exporter/JsonExporter.php b/src/Business/Cognitive/Exporter/JsonExporter.php index 0cc24b1..193c802 100644 --- a/src/Business/Cognitive/Exporter/JsonExporter.php +++ b/src/Business/Cognitive/Exporter/JsonExporter.php @@ -5,12 +5,16 @@ namespace Phauthentic\CodeQualityMetrics\Business\Cognitive\Exporter; use Phauthentic\CodeQualityMetrics\Business\Cognitive\CognitiveMetricsCollection; +use RuntimeException; /** * */ class JsonExporter implements DataExporterInterface { + /** + * @throws \JsonException + */ public function export(CognitiveMetricsCollection $metricsCollection, string $filename): void { $jsonData = []; @@ -19,30 +23,26 @@ public function export(CognitiveMetricsCollection $metricsCollection, string $fi foreach ($groupedByClass as $class => $methods) { foreach ($methods as $metrics) { - $jsonData[] = [ + $jsonData[$class]['methods'][$metrics->getMethod()] = [ 'class' => $metrics->getClass(), - 'methods' => [ - $metrics->getMethod() => [ - 'name' => $metrics->getMethod(), - 'lineCount' => $metrics->getLineCount(), - 'lineCountWeight' => $metrics->getLineCountWeight(), - 'argCount' => $metrics->getArgCount(), - 'argCountWeight' => $metrics->getArgCountWeight(), - 'returnCount' => $metrics->getReturnCount(), - 'returnCountWeight' => $metrics->getReturnCountWeight(), - 'variableCount' => $metrics->getVariableCount(), - 'variableCountWeight' => $metrics->getVariableCountWeight(), - 'propertyCallCount' => $metrics->getPropertyCallCount(), - 'propertyCallCountWeight' => $metrics->getPropertyCallCountWeight(), - 'ifCount' => $metrics->getIfCount(), - 'ifCountWeight' => $metrics->getIfCountWeight(), - 'ifNestingLevel' => $metrics->getIfNestingLevel(), - 'ifNestingLevelWeight' => $metrics->getIfNestingLevelWeight(), - 'elseCount' => $metrics->getElseCount(), - 'elseCountWeight' => $metrics->getElseCountWeight(), - 'score' => $metrics->getScore() - ] - ] + 'method' => $metrics->getMethod(), + 'lineCount' => $metrics->getLineCount(), + 'lineCountWeight' => $metrics->getLineCountWeight(), + 'argCount' => $metrics->getArgCount(), + 'argCountWeight' => $metrics->getArgCountWeight(), + 'returnCount' => $metrics->getReturnCount(), + 'returnCountWeight' => $metrics->getReturnCountWeight(), + 'variableCount' => $metrics->getVariableCount(), + 'variableCountWeight' => $metrics->getVariableCountWeight(), + 'propertyCallCount' => $metrics->getPropertyCallCount(), + 'propertyCallCountWeight' => $metrics->getPropertyCallCountWeight(), + 'ifCount' => $metrics->getIfCount(), + 'ifCountWeight' => $metrics->getIfCountWeight(), + 'ifNestingLevel' => $metrics->getIfNestingLevel(), + 'ifNestingLevelWeight' => $metrics->getIfNestingLevelWeight(), + 'elseCount' => $metrics->getElseCount(), + 'elseCountWeight' => $metrics->getElseCountWeight(), + 'score' => $metrics->getScore() ]; } } @@ -50,7 +50,7 @@ public function export(CognitiveMetricsCollection $metricsCollection, string $fi $jsonData = json_encode($jsonData, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); if (file_put_contents($filename, $jsonData) === false) { - throw new \RuntimeException("Unable to write to file: $filename"); + throw new RuntimeException("Unable to write to file: $filename"); } } } diff --git a/src/Command/CognitiveMetricsCommand.php b/src/Command/CognitiveMetricsCommand.php index 05f9d35..a9e4bb7 100644 --- a/src/Command/CognitiveMetricsCommand.php +++ b/src/Command/CognitiveMetricsCommand.php @@ -5,6 +5,7 @@ namespace Phauthentic\CodeQualityMetrics\Command; use Exception; +use Phauthentic\CodeQualityMetrics\Business\Cognitive\BaselineService; use Phauthentic\CodeQualityMetrics\Business\Cognitive\CognitiveMetricsCollection; use Phauthentic\CodeQualityMetrics\Business\MetricsFacade; use Phauthentic\CodeQualityMetrics\Command\Presentation\CognitiveMetricTextRenderer; @@ -34,6 +35,7 @@ class CognitiveMetricsCommand extends Command private MetricsFacade $metricsFacade; private CognitiveMetricTextRenderer $metricTextRenderer; + private BaselineService $baselineService; /** * Constructor to initialize dependencies. @@ -43,6 +45,7 @@ public function __construct() parent::__construct(); $this->metricsFacade = new MetricsFacade(); $this->metricTextRenderer = new CognitiveMetricTextRenderer(); + $this->baselineService = new BaselineService(); } /** @@ -76,19 +79,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int return Command::FAILURE; } - // Load baseline if the option is provided. - $baseline = $this->handleBaseLine($input); - // Generate metrics for the provided path. $metricsCollection = $this->metricsFacade->getCognitiveMetrics($path); + // Load baseline if the option is provided. + $this->handleBaseLine($input, $metricsCollection); + // Handle different export options. if (!$this->handleExportOptions($input, $output, $metricsCollection)) { return Command::FAILURE; } // Render the metrics to the console. - $this->metricTextRenderer->render($metricsCollection, $baseline, $output); + $this->metricTextRenderer->render($metricsCollection, [], $output); return Command::SUCCESS; } @@ -97,39 +100,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int * Handles the baseline option and loads the baseline file if provided. * * @param InputInterface $input - * @return array> + * @param CognitiveMetricsCollection $metricsCollection * @throws Exception */ - private function handleBaseLine(InputInterface $input): array + private function handleBaseLine(InputInterface $input, CognitiveMetricsCollection $metricsCollection): void { - $baseline = []; $baselineFile = $input->getOption(self::OPTION_BASELINE); if ($baselineFile) { - $baseline = $this->loadBaseline($baselineFile); - } - - return $baseline; - } - - /** - * Loads the baseline file and returns the data as an array. - * - * @param string $baselineFile - * @return array> $baseline - * @throws \JsonException - */ - private function loadBaseline(string $baselineFile): array - { - if (!file_exists($baselineFile)) { - throw new Exception('Baseline file does not exist.'); - } - - $baseline = file_get_contents($baselineFile); - if ($baseline === false) { - throw new Exception('Failed to read baseline file.'); + $baseline = $this->baselineService->loadBaseline($baselineFile); + $this->baselineService->calculateDeltas($metricsCollection, $baseline); } - - return json_decode($baseline, true, 512, JSON_THROW_ON_ERROR); } /** diff --git a/src/Command/Presentation/CognitiveMetricTextRenderer.php b/src/Command/Presentation/CognitiveMetricTextRenderer.php index cd2737d..98104df 100644 --- a/src/Command/Presentation/CognitiveMetricTextRenderer.php +++ b/src/Command/Presentation/CognitiveMetricTextRenderer.php @@ -6,6 +6,7 @@ use Phauthentic\CodeQualityMetrics\Business\Cognitive\CognitiveMetrics; use Phauthentic\CodeQualityMetrics\Business\Cognitive\CognitiveMetricsCollection; +use RuntimeException; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Output\OutputInterface; @@ -16,7 +17,7 @@ class CognitiveMetricTextRenderer { /** * @param CognitiveMetricsCollection $metricsCollection - * @param array> $baseline + * @param array> $baseline * @param OutputInterface $output */ public function render(CognitiveMetricsCollection $metricsCollection, array $baseline, OutputInterface $output): void @@ -32,7 +33,7 @@ public function render(CognitiveMetricsCollection $metricsCollection, array $bas $rows = []; foreach ($metrics as $metric) { - $row = $this->prepareTableRow($metric, $baseline); + $row = $this->prepareTableRow($metric); $rows[] = $row; } @@ -63,10 +64,9 @@ protected function getTableHeaders(): array /** * @param CognitiveMetrics $metrics - * @param array> $baseline * @return array */ - protected function prepareTableRow(CognitiveMetrics $metrics, array $baseline): array + protected function prepareTableRow(CognitiveMetrics $metrics): array { $row = [ 'methodName' => $metrics->getMethod(), @@ -95,55 +95,25 @@ protected function prepareTableRow(CognitiveMetrics $metrics, array $baseline): foreach ($keys as $key) { $getMethod = 'get' . $key; $getMethodWeight = 'get' . $key . 'Weight'; + $getDeltaMethod = 'get' . $key . 'WeightDelta'; $weight = $metrics->{$getMethodWeight}(); $row[$key] = $metrics->{$getMethod}() . ' (' . round($weight, 3) . ')'; - $row = $this->addDelta($row, $metrics, $baseline, $key, $weight); - } - - return $row; - } - /** - * @param array $row - * @param CognitiveMetrics $metrics - * @param array> $baseline - * @param string $key - * @param float $weight - * @return array - */ - private function addDelta( - array $row, - CognitiveMetrics $metrics, - array $baseline, - string $key, - float $weight - ): array { - foreach ($baseline as $classMetrics) { - if (!isset($classMetrics['class']) || $classMetrics['class'] !== $metrics->getClass()) { - continue; + if (!method_exists($metrics, $getDeltaMethod)) { + throw new RuntimeException('Method not found: ' . $getDeltaMethod); } - if (!isset($classMetrics['methods'][$metrics->getMethod()])) { + $delta = $metrics->{$getDeltaMethod}(); + if ($delta === null || $delta->hasNotChanged()) { continue; } - $method = $key . 'Weight'; - if (!isset($classMetrics['methods'][$metrics->getMethod()][$method])) { + if ($delta->hasIncreased()) { + $row[$key] .= PHP_EOL . 'Δ +' . round($delta->getValue(), 3) . ''; continue; } - $baselineWeight = (float)$classMetrics['methods'][$metrics->getMethod()][$method]; - if ($baselineWeight === $weight) { - return $row; - } - - if ($baselineWeight > $weight) { - $row[$key] .= PHP_EOL . 'Δ -' . round($baselineWeight - $weight, 3) . ''; - } - - if ($baselineWeight < $weight) { - $row[$key] .= PHP_EOL . 'Δ +' . round($weight - $baselineWeight, 3) . ''; - } + $row[$key] .= PHP_EOL . 'Δ ' . $delta->getValue() . ''; } return $row; diff --git a/tests/Unit/Business/Cognitive/CognitiveMetricsTest.php b/tests/Unit/Business/Cognitive/CognitiveMetricsTest.php index f0bf8d6..e854d0d 100644 --- a/tests/Unit/Business/Cognitive/CognitiveMetricsTest.php +++ b/tests/Unit/Business/Cognitive/CognitiveMetricsTest.php @@ -156,7 +156,7 @@ public function testJsonSerialize(): void public function testCalculateDeltas(): void { // Create first set of metrics - $metrics1 = new CognitiveMetrics([ + $presentMetrics = new CognitiveMetrics([ 'class' => 'TestClass', 'method' => 'testMethod', 'lineCount' => 10, @@ -170,17 +170,17 @@ public function testCalculateDeltas(): void ]); // Set weights for metrics1 - $metrics1->setLineCountWeight(1.5); - $metrics1->setArgCountWeight(0.5); - $metrics1->setReturnCountWeight(2.0); - $metrics1->setVariableCountWeight(1.0); - $metrics1->setPropertyCallCountWeight(1.5); - $metrics1->setIfCountWeight(0.0); - $metrics1->setIfNestingLevelWeight(1.0); - $metrics1->setElseCountWeight(0.5); + $presentMetrics->setLineCountWeight(1.5); + $presentMetrics->setArgCountWeight(0.5); + $presentMetrics->setReturnCountWeight(2.0); + $presentMetrics->setVariableCountWeight(1.0); + $presentMetrics->setPropertyCallCountWeight(1.5); + $presentMetrics->setIfCountWeight(0.0); + $presentMetrics->setIfNestingLevelWeight(1.0); + $presentMetrics->setElseCountWeight(0.5); // Create second set of metrics with different weights - $metrics2 = new CognitiveMetrics([ + $beforeMetrics = new CognitiveMetrics([ 'class' => 'TestClass', 'method' => 'testMethod', 'lineCount' => 10, @@ -194,41 +194,41 @@ public function testCalculateDeltas(): void ]); // Set weights for metrics2 - $metrics2->setLineCountWeight(1.0); - $metrics2->setArgCountWeight(1.0); - $metrics2->setReturnCountWeight(2.5); - $metrics2->setVariableCountWeight(1.0); - $metrics2->setPropertyCallCountWeight(2.0); - $metrics2->setIfCountWeight(0.0); - $metrics2->setIfNestingLevelWeight(2.0); - $metrics2->setElseCountWeight(0.5); + $beforeMetrics->setLineCountWeight(1.0); + $beforeMetrics->setArgCountWeight(1.0); + $beforeMetrics->setReturnCountWeight(2.5); + $beforeMetrics->setVariableCountWeight(1.0); + $beforeMetrics->setPropertyCallCountWeight(2.0); + $beforeMetrics->setIfCountWeight(0.0); + $beforeMetrics->setIfNestingLevelWeight(2.0); + $beforeMetrics->setElseCountWeight(0.5); // Calculate deltas - $metrics1->calculateDeltas($metrics2); + $presentMetrics->calculateDeltas($beforeMetrics); // Check deltas for each weight - $this->assertInstanceOf(Delta::class, $metrics1->getLineCountWeightDelta()); - $this->assertEquals(-0.5, $metrics1->getLineCountWeightDelta()->getValue()); + $this->assertInstanceOf(Delta::class, $presentMetrics->getLineCountWeightDelta()); + $this->assertEquals(0.5, $presentMetrics->getLineCountWeightDelta()->getValue()); - $this->assertInstanceOf(Delta::class, $metrics1->getArgCountWeightDelta()); - $this->assertEquals(0.5, $metrics1->getArgCountWeightDelta()->getValue()); + $this->assertInstanceOf(Delta::class, $presentMetrics->getArgCountWeightDelta()); + $this->assertEquals(-0.5, $presentMetrics->getArgCountWeightDelta()->getValue()); - $this->assertInstanceOf(Delta::class, $metrics1->getReturnCountWeightDelta()); - $this->assertEquals(0.5, $metrics1->getReturnCountWeightDelta()->getValue()); + $this->assertInstanceOf(Delta::class, $presentMetrics->getReturnCountWeightDelta()); + $this->assertEquals(-0.5, $presentMetrics->getReturnCountWeightDelta()->getValue()); - $this->assertInstanceOf(Delta::class, $metrics1->getVariableCountWeightDelta()); - $this->assertEquals(0.0, $metrics1->getVariableCountWeightDelta()->getValue()); + $this->assertInstanceOf(Delta::class, $presentMetrics->getVariableCountWeightDelta()); + $this->assertEquals(0.0, $presentMetrics->getVariableCountWeightDelta()->getValue()); - $this->assertInstanceOf(Delta::class, $metrics1->getPropertyCallCountWeightDelta()); - $this->assertEquals(0.5, $metrics1->getPropertyCallCountWeightDelta()->getValue()); + $this->assertInstanceOf(Delta::class, $presentMetrics->getPropertyCallCountWeightDelta()); + $this->assertEquals(-0.5, $presentMetrics->getPropertyCallCountWeightDelta()->getValue()); - $this->assertInstanceOf(Delta::class, $metrics1->getIfCountWeightDelta()); - $this->assertEquals(0.0, $metrics1->getIfCountWeightDelta()->getValue()); + $this->assertInstanceOf(Delta::class, $presentMetrics->getIfCountWeightDelta()); + $this->assertEquals(0.0, $presentMetrics->getIfCountWeightDelta()->getValue()); - $this->assertInstanceOf(Delta::class, $metrics1->getIfNestingLevelWeightDelta()); - $this->assertEquals(1.0, $metrics1->getIfNestingLevelWeightDelta()->getValue()); + $this->assertInstanceOf(Delta::class, $presentMetrics->getIfNestingLevelWeightDelta()); + $this->assertEquals(-1.0, $presentMetrics->getIfNestingLevelWeightDelta()->getValue()); - $this->assertInstanceOf(Delta::class, $metrics1->getElseCountWeightDelta()); - $this->assertEquals(0.0, $metrics1->getElseCountWeightDelta()->getValue()); + $this->assertInstanceOf(Delta::class, $presentMetrics->getElseCountWeightDelta()); + $this->assertEquals(0.0, $presentMetrics->getElseCountWeightDelta()->getValue()); } } diff --git a/tests/Unit/Business/Cognitive/Exporter/JsonExporterTest.php b/tests/Unit/Business/Cognitive/Exporter/JsonExporterTest.php index 634c833..98582af 100644 --- a/tests/Unit/Business/Cognitive/Exporter/JsonExporterTest.php +++ b/tests/Unit/Business/Cognitive/Exporter/JsonExporterTest.php @@ -63,11 +63,11 @@ public function testExport(): void $decodedData = json_decode($jsonData, true); $expected = [ - [ - 'class' => 'TestClass1', + 'TestClass1' => [ 'methods' => [ 'testMethod1' => [ - 'name' => 'testMethod1', + 'class' => 'TestClass1', + 'method' => 'testMethod1', 'lineCount' => 10, 'lineCountWeight' => 0, 'argCount' => 2, @@ -84,15 +84,15 @@ public function testExport(): void 'ifNestingLevelWeight' => 0, 'elseCount' => 1, 'elseCountWeight' => 0, - 'score' => 0 + 'score' => 0, ] ] - ], - [ - 'class' => 'TestClass2', + ], + 'TestClass2' => [ 'methods' => [ 'testMethod2' => [ - 'name' => 'testMethod2', + 'class' => 'TestClass2', + 'method' => 'testMethod2', 'lineCount' => 15, 'lineCountWeight' => 0, 'argCount' => 3, @@ -109,10 +109,10 @@ public function testExport(): void 'ifNestingLevelWeight' => 0, 'elseCount' => 1, 'elseCountWeight' => 0, - 'score' => 0 + 'score' => 0, ] ] - ] + ] ]; $this->assertSame($expected, $decodedData);