Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/Command/CognitiveMetricsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Phauthentic\CognitiveCodeAnalysis\Command;

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

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

Expand All @@ -147,6 +147,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($sortResult['status'] === Command::FAILURE) {
return Command::FAILURE;
}

$metricsCollection = $sortResult['collection'];

$reportType = $input->getOption(self::OPTION_REPORT_TYPE);
Expand Down Expand Up @@ -237,7 +238,7 @@ private function applySorting(
try {
$sorted = $this->sorter->sort($metricsCollection, $sortBy, $sortOrder);
return ['status' => Command::SUCCESS, 'collection' => $sorted];
} catch (\InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
$output->writeln('<error>Sorting error: ' . $e->getMessage() . '</error>');
$output->writeln('<info>Available sort fields: ' . implode(', ', $this->sorter->getSortableFields()) . '</info>');
return ['status' => Command::FAILURE, 'collection' => $metricsCollection];
Expand All @@ -247,14 +248,23 @@ private function applySorting(
/**
* Loads configuration and handles errors.
*
* @param string $configFile
* @param OutputInterface $output
* @return bool Success or failure.
*/
private function loadConfiguration(string $configFile, OutputInterface $output): bool
private function loadConfiguration(InputInterface $input, OutputInterface $output): bool
{
try {
$configFile = $input->getOption(self::OPTION_CONFIG_FILE);
if (!$configFile && file_exists(getcwd() . '/phpcca.yml')) {
$configFile = getcwd() . '/phpcca.yml';
}

if (!$configFile) {
return true;
}

$this->metricsFacade->loadConfig($configFile);

return true;
} catch (Exception $e) {
$output->writeln('<error>Failed to load configuration: ' . $e->getMessage() . '</error>');
Expand All @@ -279,7 +289,7 @@ private function loadCoverageReader(
return null;
}

// Auto-detect format if not specified
// Auto-detect format if isn't specified
if ($format === null) {
$format = $this->detectCoverageFormat($coverageFile);
if ($format === null) {
Expand Down Expand Up @@ -309,32 +319,36 @@ private function detectCoverageFormat(string $coverageFile): ?string
return null;
}

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

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

return null;
}

/**
* Checks
* - if no coverage file is provided, validation passes (backward compatibility)
* - if a coverage file is provided, check if it exists
*
* If coverage file was provided but doesn't exist - show error.
*/
private function coverageFileExists(?string $coverageFile, OutputInterface $output): bool
{
// If no coverage file is provided, validation passes (backward compatibility)
if ($coverageFile === null) {
return true;
}

// If coverage file is provided, check if it exists
if (file_exists($coverageFile)) {
return true;
}

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