Skip to content

Commit 51d23a5

Browse files
Adding tests
1 parent a77d89c commit 51d23a5

16 files changed

+543
-105
lines changed

src/Application.php

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Phauthentic\CognitiveCodeAnalysis\Business\Churn\ChangeCounter\ChangeCounterFactory;
88
use Phauthentic\CognitiveCodeAnalysis\Business\Churn\ChurnCalculator;
9+
use Phauthentic\CognitiveCodeAnalysis\Business\Churn\Report\ChurnReportFactory;
10+
use Phauthentic\CognitiveCodeAnalysis\Business\Churn\Report\ChurnReportFactoryInterface;
911
use Phauthentic\CognitiveCodeAnalysis\Business\CodeCoverage\CodeCoverageFactory;
1012
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Baseline;
1113
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\CognitiveMetricsCollector;
@@ -14,6 +16,8 @@
1416
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Events\ParserFailed;
1517
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Events\SourceFilesFound;
1618
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Parser;
19+
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Report\CognitiveReportFactory;
20+
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Report\CognitiveReportFactoryInterface;
1721
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\ScoreCalculator;
1822
use Phauthentic\CognitiveCodeAnalysis\Business\MetricsFacade;
1923
use Phauthentic\CognitiveCodeAnalysis\Business\Utility\DirectoryScanner;
@@ -62,6 +66,15 @@ public function __construct()
6266
}
6367

6468
private function registerServices(): void
69+
{
70+
$this->registerCoreServices();
71+
$this->registerReportFactories();
72+
$this->registerPresentationServices();
73+
$this->registerUtilityServices();
74+
$this->registerCommandHandlers();
75+
}
76+
77+
private function registerCoreServices(): void
6578
{
6679
$outputClass = getenv('APP_ENV') === 'test' ? NullOutput::class : ConsoleOutput::class;
6780

@@ -83,27 +96,48 @@ private function registerServices(): void
8396
$this->containerBuilder->register(ConfigService::class, ConfigService::class)
8497
->setPublic(true);
8598

86-
$this->containerBuilder->register(ChurnTextRenderer::class, ChurnTextRenderer::class)
87-
->setArguments([
88-
new Reference(OutputInterface::class)
89-
])
99+
$this->containerBuilder->register(Baseline::class, Baseline::class)
90100
->setPublic(true);
91101

92-
$this->containerBuilder->register(CognitiveMetricTextRendererInterface::class, CognitiveMetricTextRenderer::class)
102+
$this->containerBuilder->register(CognitiveMetricsSorter::class, CognitiveMetricsSorter::class)
103+
->setPublic(true);
104+
105+
$this->containerBuilder->register(CodeCoverageFactory::class, CodeCoverageFactory::class)
106+
->setPublic(true);
107+
}
108+
109+
private function registerReportFactories(): void
110+
{
111+
$this->containerBuilder->register(ChurnReportFactoryInterface::class, ChurnReportFactory::class)
93112
->setArguments([
94-
new Reference(ConfigService::class)
113+
new Reference(ConfigService::class),
95114
])
96115
->setPublic(true);
97116

98-
$this->containerBuilder->register(Baseline::class, Baseline::class)
117+
$this->containerBuilder->register(CognitiveReportFactoryInterface::class, CognitiveReportFactory::class)
118+
->setArguments([
119+
new Reference(ConfigService::class),
120+
])
99121
->setPublic(true);
122+
}
100123

101-
$this->containerBuilder->register(CognitiveMetricsSorter::class, CognitiveMetricsSorter::class)
124+
private function registerPresentationServices(): void
125+
{
126+
$this->containerBuilder->register(ChurnTextRenderer::class, ChurnTextRenderer::class)
127+
->setArguments([
128+
new Reference(OutputInterface::class)
129+
])
102130
->setPublic(true);
103131

104-
$this->containerBuilder->register(CodeCoverageFactory::class, CodeCoverageFactory::class)
132+
$this->containerBuilder->register(CognitiveMetricTextRendererInterface::class, CognitiveMetricTextRenderer::class)
133+
->setArguments([
134+
new Reference(ConfigService::class)
135+
])
105136
->setPublic(true);
137+
}
106138

139+
private function registerUtilityServices(): void
140+
{
107141
$this->containerBuilder->register(Processor::class, Processor::class)
108142
->setPublic(true);
109143

@@ -135,18 +169,23 @@ private function registerServices(): void
135169
new Reference(NodeTraverserInterface::class),
136170
])
137171
->setPublic(true);
172+
}
138173

174+
private function registerCommandHandlers(): void
175+
{
139176
$this->containerBuilder->register(ChurnReportHandler::class, ChurnReportHandler::class)
140177
->setArguments([
141178
new Reference(MetricsFacade::class),
142179
new Reference(OutputInterface::class),
180+
new Reference(ChurnReportFactoryInterface::class),
143181
])
144182
->setPublic(true);
145183

146184
$this->containerBuilder->register(CognitiveMetricsReportHandler::class, CognitiveMetricsReportHandler::class)
147185
->setArguments([
148186
new Reference(MetricsFacade::class),
149187
new Reference(OutputInterface::class),
188+
new Reference(CognitiveReportFactoryInterface::class),
150189
])
151190
->setPublic(true);
152191
}
@@ -213,6 +252,8 @@ private function registerMetricsFacade(): void
213252
new Reference(ConfigService::class),
214253
new Reference(ChurnCalculator::class),
215254
new Reference(ChangeCounterFactory::class),
255+
new Reference(ChurnReportFactoryInterface::class),
256+
new Reference(CognitiveReportFactoryInterface::class),
216257
])
217258
->setPublic(true);
218259
}

src/Business/Churn/Report/ChurnReportFactory.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,18 @@
66

77
use InvalidArgumentException;
88
use Phauthentic\CognitiveCodeAnalysis\Business\Reporter\ReporterRegistry;
9+
use Phauthentic\CognitiveCodeAnalysis\Config\ConfigService;
910

1011
/**
1112
* Factory for creating churn data exporters.
1213
*/
13-
class ChurnReportFactory
14+
class ChurnReportFactory implements ChurnReportFactoryInterface
1415
{
15-
/** @var array<string, array<string, mixed>> */
16-
private array $customExporters = [];
1716
private ReporterRegistry $registry;
1817

19-
/**
20-
* @param array<string, array<string, mixed>> $customExporters
21-
*/
22-
public function __construct(array $customExporters = [])
23-
{
24-
$this->customExporters = $customExporters;
18+
public function __construct(
19+
private readonly ConfigService $configService
20+
) {
2521
$this->registry = new ReporterRegistry();
2622
}
2723

@@ -34,6 +30,9 @@ public function __construct(array $customExporters = [])
3430
*/
3531
public function create(string $type): ReportGeneratorInterface
3632
{
33+
$config = $this->configService->getConfig();
34+
$customExporters = $config->customExporters['churn'] ?? [];
35+
3736
// Check built-in exporters first
3837
$builtIn = match ($type) {
3938
'json' => new JsonReport(),
@@ -49,8 +48,8 @@ public function create(string $type): ReportGeneratorInterface
4948
}
5049

5150
// Check custom exporters
52-
if (isset($this->customExporters[$type])) {
53-
return $this->createCustomExporter($this->customExporters[$type]);
51+
if (isset($customExporters[$type])) {
52+
return $this->createCustomExporter($customExporters[$type]);
5453
}
5554

5655
throw new InvalidArgumentException("Unsupported exporter type: {$type}");
@@ -84,9 +83,12 @@ private function createCustomExporter(array $config): ReportGeneratorInterface
8483
*/
8584
public function getSupportedTypes(): array
8685
{
86+
$config = $this->configService->getConfig();
87+
$customExporters = $config->customExporters['churn'] ?? [];
88+
8789
return array_merge(
8890
['json', 'csv', 'html', 'markdown', 'svg-treemap', 'svg'],
89-
array_keys($this->customExporters)
91+
array_keys($customExporters)
9092
);
9193
}
9294

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phauthentic\CognitiveCodeAnalysis\Business\Churn\Report;
6+
7+
/**
8+
* Interface for churn report factory.
9+
*/
10+
interface ChurnReportFactoryInterface
11+
{
12+
/**
13+
* Create an exporter instance based on the report type.
14+
*
15+
* @param string $type The type of exporter to create (json, csv, html, markdown, svg-treemap)
16+
* @return ReportGeneratorInterface
17+
* @throws \InvalidArgumentException If the type is not supported
18+
*/
19+
public function create(string $type): ReportGeneratorInterface;
20+
21+
/**
22+
* Get list of supported exporter types.
23+
*
24+
* @return array<string>
25+
*/
26+
public function getSupportedTypes(): array;
27+
28+
/**
29+
* Check if a type is supported.
30+
*
31+
* @param string $type
32+
* @return bool
33+
*/
34+
public function isSupported(string $type): bool;
35+
}

src/Business/Cognitive/Report/CognitiveReportFactory.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,18 @@
66

77
use InvalidArgumentException;
88
use Phauthentic\CognitiveCodeAnalysis\Business\Reporter\ReporterRegistry;
9-
use Phauthentic\CognitiveCodeAnalysis\Config\CognitiveConfig;
9+
use Phauthentic\CognitiveCodeAnalysis\Config\ConfigService;
1010

1111
/**
1212
* Factory for creating cognitive metrics exporters.
1313
*/
14-
class CognitiveReportFactory
14+
class CognitiveReportFactory implements CognitiveReportFactoryInterface
1515
{
16-
/** @var array<string, array<string, mixed>> */
17-
private array $customExporters = [];
1816
private ReporterRegistry $registry;
1917

20-
/**
21-
* @param array<string, array<string, mixed>> $customExporters
22-
*/
2318
public function __construct(
24-
private readonly CognitiveConfig $config,
25-
array $customExporters = []
19+
private readonly ConfigService $configService
2620
) {
27-
$this->customExporters = $customExporters;
2821
$this->registry = new ReporterRegistry();
2922
}
3023

@@ -37,21 +30,24 @@ public function __construct(
3730
*/
3831
public function create(string $type): ReportGeneratorInterface
3932
{
33+
$config = $this->configService->getConfig();
34+
$customExporters = $config->customExporters['cognitive'] ?? [];
35+
4036
// Check built-in exporters first
4137
$builtIn = match ($type) {
4238
'json' => new JsonReport(),
4339
'csv' => new CsvReport(),
4440
'html' => new HtmlReport(),
45-
'markdown' => new MarkdownReport($this->config),
41+
'markdown' => new MarkdownReport($config),
4642
default => null,
4743
};
4844

4945
if ($builtIn !== null) {
5046
return $builtIn;
5147
}
5248

53-
if (isset($this->customExporters[$type])) {
54-
return $this->createCustomExporter($this->customExporters[$type]);
49+
if (isset($customExporters[$type])) {
50+
return $this->createCustomExporter($customExporters[$type]);
5551
}
5652

5753
throw new InvalidArgumentException("Unsupported exporter type: {$type}");
@@ -65,11 +61,13 @@ public function create(string $type): ReportGeneratorInterface
6561
*/
6662
private function createCustomExporter(array $config): ReportGeneratorInterface
6763
{
64+
$cognitiveConfig = $this->configService->getConfig();
65+
6866
$this->registry->loadExporter($config['class'], $config['file'] ?? null);
6967
$exporter = $this->registry->instantiate(
7068
$config['class'],
7169
$config['requiresConfig'] ?? false,
72-
$this->config
70+
$cognitiveConfig
7371
);
7472
$this->registry->validateInterface($exporter, ReportGeneratorInterface::class);
7573

@@ -85,9 +83,12 @@ private function createCustomExporter(array $config): ReportGeneratorInterface
8583
*/
8684
public function getSupportedTypes(): array
8785
{
86+
$config = $this->configService->getConfig();
87+
$customExporters = $config->customExporters['cognitive'] ?? [];
88+
8889
return array_merge(
8990
['json', 'csv', 'html', 'markdown'],
90-
array_keys($this->customExporters)
91+
array_keys($customExporters)
9192
);
9293
}
9394

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Report;
6+
7+
/**
8+
* Interface for cognitive report factory.
9+
*/
10+
interface CognitiveReportFactoryInterface
11+
{
12+
/**
13+
* Create an exporter instance based on the report type.
14+
*
15+
* @param string $type The type of exporter to create (json, csv, html, markdown)
16+
* @return ReportGeneratorInterface
17+
* @throws \InvalidArgumentException If the type is not supported
18+
*/
19+
public function create(string $type): ReportGeneratorInterface;
20+
21+
/**
22+
* Get list of supported exporter types.
23+
*
24+
* @return array<string>
25+
*/
26+
public function getSupportedTypes(): array;
27+
28+
/**
29+
* Check if a type is supported.
30+
*
31+
* @param string $type
32+
* @return bool
33+
*/
34+
public function isSupported(string $type): bool;
35+
}

0 commit comments

Comments
 (0)