Skip to content

Commit 7d625f2

Browse files
Made the threshold in the renderer use the configuration value. (#33)
* Made the threshold in the renderer use the configuration value. * Silencing PHPMD for the static call of the YAML parser.
1 parent 1181775 commit 7d625f2

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

src/Application.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ private function registerServices(): void
8181

8282
$this->containerBuilder->register(CognitiveMetricTextRenderer::class, CognitiveMetricTextRenderer::class)
8383
->setArguments([
84-
new Reference(OutputInterface::class)
84+
new Reference(OutputInterface::class),
85+
new Reference(ConfigService::class)
8586
])
8687
->setPublic(true);
8788

src/Command/CognitiveMetricsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
113113
return $this->reportHandler->handle($metricsCollection, $reportType, $reportFile);
114114
}
115115

116-
$this->renderer->render($metricsCollection, $this->metricsFacade->getConfig());
116+
$this->renderer->render($metricsCollection);
117117

118118
return Command::SUCCESS;
119119
}

src/Command/Presentation/CognitiveMetricTextRenderer.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\CognitiveMetricsCollection;
99
use Phauthentic\CognitiveCodeAnalysis\CognitiveAnalysisException;
1010
use Phauthentic\CognitiveCodeAnalysis\Config\CognitiveConfig;
11+
use Phauthentic\CognitiveCodeAnalysis\Config\ConfigService;
1112
use Symfony\Component\Console\Helper\Table;
1213
use Symfony\Component\Console\Output\OutputInterface;
1314

@@ -17,7 +18,8 @@
1718
class CognitiveMetricTextRenderer
1819
{
1920
public function __construct(
20-
private readonly OutputInterface $output
21+
private readonly OutputInterface $output,
22+
private readonly ConfigService $configService,
2123
) {
2224
}
2325

@@ -30,12 +32,12 @@ private function metricExceedsThreshold(CognitiveMetrics $metric, CognitiveConfi
3032

3133
/**
3234
* @param CognitiveMetricsCollection $metricsCollection
33-
* @param CognitiveConfig $config
3435
* @throws CognitiveAnalysisException
3536
*/
36-
public function render(CognitiveMetricsCollection $metricsCollection, CognitiveConfig $config): void
37+
public function render(CognitiveMetricsCollection $metricsCollection): void
3738
{
3839
$groupedByClass = $metricsCollection->groupBy('class');
40+
$config = $this->configService->getConfig();
3941

4042
foreach ($groupedByClass as $className => $metrics) {
4143
if (count($metrics) === 0) {
@@ -162,7 +164,7 @@ private function metricsToArray(CognitiveMetrics $metrics): array
162164
'ifCount' => $metrics->getIfCount(),
163165
'ifNestingLevel' => $metrics->getIfNestingLevel(),
164166
'elseCount' => $metrics->getElseCount(),
165-
'score' => $metrics->getScore() > 0.5
167+
'score' => $metrics->getScore() > $this->configService->getConfig()->scoreThreshold
166168
? '<error>' . $metrics->getScore() . '</error>'
167169
: '<info>' . $metrics->getScore() . '</info>',
168170
];

src/Config/ConfigService.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
*/
1313
class ConfigService
1414
{
15-
/**
16-
* @var array<string, mixed>
17-
*/
18-
private array $config;
15+
private CognitiveConfig $config;
1916

2017
/**
2118
* @SuppressWarnings(PHPMD)
@@ -24,24 +21,36 @@ public function __construct(
2421
private readonly Processor $processor,
2522
private readonly ConfigLoader $configuration
2623
) {
27-
$this->config = $this->processor->processConfiguration($this->configuration, [
24+
$this->loadDefaultConfig();
25+
}
26+
27+
/**
28+
* @SuppressWarnings(PHPMD)
29+
*/
30+
private function loadDefaultConfig(): void
31+
{
32+
$config = $this->processor->processConfiguration($this->configuration, [
2833
Yaml::parseFile(__DIR__ . '/../../config.yml'),
2934
]);
35+
36+
$this->config = (new ConfigFactory())->fromArray($config);
3037
}
3138

3239
/**
3340
* @SuppressWarnings(PHPMD)
3441
*/
3542
public function loadConfig(string $configFilePath): void
3643
{
37-
$this->config = $this->processor->processConfiguration($this->configuration, [
44+
$config = $this->processor->processConfiguration($this->configuration, [
3845
Yaml::parseFile(__DIR__ . '/../../config.yml'),
3946
Yaml::parseFile($configFilePath),
4047
]);
48+
49+
$this->config = (new ConfigFactory())->fromArray($config);
4150
}
4251

4352
public function getConfig(): CognitiveConfig
4453
{
45-
return (new ConfigFactory())->fromArray($this->config);
54+
return $this->config;
4655
}
4756
}

0 commit comments

Comments
 (0)