5
5
namespace Phauthentic \CognitiveCodeAnalysis \Command ;
6
6
7
7
use Exception ;
8
+ use InvalidArgumentException ;
8
9
use Phauthentic \CognitiveCodeAnalysis \Business \CodeCoverage \CodeCoverageFactory ;
9
10
use Phauthentic \CognitiveCodeAnalysis \Business \CodeCoverage \CoverageReportReaderInterface ;
10
11
use Phauthentic \CognitiveCodeAnalysis \Business \Cognitive \Baseline ;
@@ -129,8 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
129
130
$ pathInput = $ input ->getArgument (self ::ARGUMENT_PATH );
130
131
$ paths = $ this ->parsePaths ($ pathInput );
131
132
132
- $ configFile = $ input ->getOption (self ::OPTION_CONFIG_FILE );
133
- if ($ configFile && !$ this ->loadConfiguration ($ configFile , $ output )) {
133
+ if (!$ this ->loadConfiguration ($ input , $ output )) {
134
134
return Command::FAILURE ;
135
135
}
136
136
@@ -147,6 +147,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
147
147
if ($ sortResult ['status ' ] === Command::FAILURE ) {
148
148
return Command::FAILURE ;
149
149
}
150
+
150
151
$ metricsCollection = $ sortResult ['collection ' ];
151
152
152
153
$ reportType = $ input ->getOption (self ::OPTION_REPORT_TYPE );
@@ -237,7 +238,7 @@ private function applySorting(
237
238
try {
238
239
$ sorted = $ this ->sorter ->sort ($ metricsCollection , $ sortBy , $ sortOrder );
239
240
return ['status ' => Command::SUCCESS , 'collection ' => $ sorted ];
240
- } catch (\ InvalidArgumentException $ e ) {
241
+ } catch (InvalidArgumentException $ e ) {
241
242
$ output ->writeln ('<error>Sorting error: ' . $ e ->getMessage () . '</error> ' );
242
243
$ output ->writeln ('<info>Available sort fields: ' . implode (', ' , $ this ->sorter ->getSortableFields ()) . '</info> ' );
243
244
return ['status ' => Command::FAILURE , 'collection ' => $ metricsCollection ];
@@ -247,14 +248,23 @@ private function applySorting(
247
248
/**
248
249
* Loads configuration and handles errors.
249
250
*
250
- * @param string $configFile
251
251
* @param OutputInterface $output
252
252
* @return bool Success or failure.
253
253
*/
254
- private function loadConfiguration (string $ configFile , OutputInterface $ output ): bool
254
+ private function loadConfiguration (InputInterface $ input , OutputInterface $ output ): bool
255
255
{
256
256
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
+
257
266
$ this ->metricsFacade ->loadConfig ($ configFile );
267
+
258
268
return true ;
259
269
} catch (Exception $ e ) {
260
270
$ output ->writeln ('<error>Failed to load configuration: ' . $ e ->getMessage () . '</error> ' );
@@ -279,7 +289,7 @@ private function loadCoverageReader(
279
289
return null ;
280
290
}
281
291
282
- // Auto-detect format if not specified
292
+ // Auto-detect format if isn't specified
283
293
if ($ format === null ) {
284
294
$ format = $ this ->detectCoverageFormat ($ coverageFile );
285
295
if ($ format === null ) {
@@ -309,32 +319,36 @@ private function detectCoverageFormat(string $coverageFile): ?string
309
319
return null ;
310
320
}
311
321
312
- // Cobertura format has <coverage> root element with line-rate attribute
322
+ // Cobertura format has <coverage> root element with a line-rate attribute
313
323
if (preg_match ('/<coverage[^>]*line-rate=/ ' , $ content )) {
314
324
return 'cobertura ' ;
315
325
}
316
326
317
- // Clover format has <coverage> with generated attribute and <project> child
327
+ // Clover format has <coverage> with a generated attribute and <project> child
318
328
if (preg_match ('/<coverage[^>]*generated=.*<project/ ' , $ content )) {
319
329
return 'clover ' ;
320
330
}
321
331
322
332
return null ;
323
333
}
324
334
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
+ */
325
342
private function coverageFileExists (?string $ coverageFile , OutputInterface $ output ): bool
326
343
{
327
- // If no coverage file is provided, validation passes (backward compatibility)
328
344
if ($ coverageFile === null ) {
329
345
return true ;
330
346
}
331
347
332
- // If coverage file is provided, check if it exists
333
348
if (file_exists ($ coverageFile )) {
334
349
return true ;
335
350
}
336
351
337
- // Coverage file was provided but doesn't exist - show error
338
352
$ output ->writeln (sprintf (
339
353
'<error>Coverage file not found: %s</error> ' ,
340
354
$ coverageFile
0 commit comments