Skip to content

Commit 52ab7ef

Browse files
Refactoring
1 parent a722f96 commit 52ab7ef

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/Command/CognitiveMetricsCommand.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Phauthentic\CognitiveCodeAnalysis\Command;
66

77
use Exception;
8+
use InvalidArgumentException;
89
use Phauthentic\CognitiveCodeAnalysis\Business\CodeCoverage\CodeCoverageFactory;
910
use Phauthentic\CognitiveCodeAnalysis\Business\CodeCoverage\CoverageReportReaderInterface;
1011
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Baseline;
@@ -129,8 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
129130
$pathInput = $input->getArgument(self::ARGUMENT_PATH);
130131
$paths = $this->parsePaths($pathInput);
131132

132-
$configFile = $input->getOption(self::OPTION_CONFIG_FILE);
133-
if ($configFile && !$this->loadConfiguration($configFile, $output)) {
133+
if (!$this->loadConfiguration($input, $output)) {
134134
return Command::FAILURE;
135135
}
136136

@@ -147,6 +147,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
147147
if ($sortResult['status'] === Command::FAILURE) {
148148
return Command::FAILURE;
149149
}
150+
150151
$metricsCollection = $sortResult['collection'];
151152

152153
$reportType = $input->getOption(self::OPTION_REPORT_TYPE);
@@ -237,7 +238,7 @@ private function applySorting(
237238
try {
238239
$sorted = $this->sorter->sort($metricsCollection, $sortBy, $sortOrder);
239240
return ['status' => Command::SUCCESS, 'collection' => $sorted];
240-
} catch (\InvalidArgumentException $e) {
241+
} catch (InvalidArgumentException $e) {
241242
$output->writeln('<error>Sorting error: ' . $e->getMessage() . '</error>');
242243
$output->writeln('<info>Available sort fields: ' . implode(', ', $this->sorter->getSortableFields()) . '</info>');
243244
return ['status' => Command::FAILURE, 'collection' => $metricsCollection];
@@ -247,14 +248,23 @@ private function applySorting(
247248
/**
248249
* Loads configuration and handles errors.
249250
*
250-
* @param string $configFile
251251
* @param OutputInterface $output
252252
* @return bool Success or failure.
253253
*/
254-
private function loadConfiguration(string $configFile, OutputInterface $output): bool
254+
private function loadConfiguration(InputInterface $input, OutputInterface $output): bool
255255
{
256256
try {
257+
$configFile = $input->getOption(self::OPTION_CONFIG_FILE);
258+
if (!$configFile && file_exists(getcwd() . '/phpcca.yml')) {
259+
$configFile = getcwd() . '/phpcca.yml';
260+
}
261+
262+
if (!$configFile) {
263+
return true;
264+
}
265+
257266
$this->metricsFacade->loadConfig($configFile);
267+
258268
return true;
259269
} catch (Exception $e) {
260270
$output->writeln('<error>Failed to load configuration: ' . $e->getMessage() . '</error>');
@@ -279,7 +289,7 @@ private function loadCoverageReader(
279289
return null;
280290
}
281291

282-
// Auto-detect format if not specified
292+
// Auto-detect format if isn't specified
283293
if ($format === null) {
284294
$format = $this->detectCoverageFormat($coverageFile);
285295
if ($format === null) {
@@ -309,32 +319,36 @@ private function detectCoverageFormat(string $coverageFile): ?string
309319
return null;
310320
}
311321

312-
// Cobertura format has <coverage> root element with line-rate attribute
322+
// Cobertura format has <coverage> root element with a line-rate attribute
313323
if (preg_match('/<coverage[^>]*line-rate=/', $content)) {
314324
return 'cobertura';
315325
}
316326

317-
// Clover format has <coverage> with generated attribute and <project> child
327+
// Clover format has <coverage> with a generated attribute and <project> child
318328
if (preg_match('/<coverage[^>]*generated=.*<project/', $content)) {
319329
return 'clover';
320330
}
321331

322332
return null;
323333
}
324334

335+
/**
336+
* Checks
337+
* - if no coverage file is provided, validation passes (backward compatibility)
338+
* - if a coverage file is provided, check if it exists
339+
*
340+
* If coverage file was provided but doesn't exist - show error.
341+
*/
325342
private function coverageFileExists(?string $coverageFile, OutputInterface $output): bool
326343
{
327-
// If no coverage file is provided, validation passes (backward compatibility)
328344
if ($coverageFile === null) {
329345
return true;
330346
}
331347

332-
// If coverage file is provided, check if it exists
333348
if (file_exists($coverageFile)) {
334349
return true;
335350
}
336351

337-
// Coverage file was provided but doesn't exist - show error
338352
$output->writeln(sprintf(
339353
'<error>Coverage file not found: %s</error>',
340354
$coverageFile

0 commit comments

Comments
 (0)