33
44namespace DigitalRevolution \CodeCoverageInspection \Command ;
55
6+ use DigitalRevolution \CodeCoverageInspection \Lib \Config \ConfigFactory ;
7+ use DigitalRevolution \CodeCoverageInspection \Lib \Config \ConfigViolation ;
68use DigitalRevolution \CodeCoverageInspection \Lib \IO \DOMDocumentFactory ;
79use DigitalRevolution \CodeCoverageInspection \Lib \IO \InspectionConfigFactory ;
810use DigitalRevolution \CodeCoverageInspection \Lib \IO \MetricsFactory ;
2224 */
2325class InspectCommand extends Command
2426{
25- private const CONFIG_FILES = ['phpfci.xml ' , 'phpfci.xml.dist ' ];
27+ private ConfigFactory $ configFactory ;
28+ private string $ schemaPath ;
29+
30+ public function __construct (string $ name = null )
31+ {
32+ parent ::__construct ($ name );
33+ $ this ->configFactory = new ConfigFactory ();
34+ $ this ->schemaPath = dirname (__DIR__ , 2 ) . '/resources/phpfci.xsd ' ;
35+ }
2636
2737 protected function configure (): void
2838 {
2939 $ this ->setName ("inspect " )
3040 ->setDescription ("PHPUnit code coverage inspection " )
3141 ->addArgument ('coverage ' , InputOption::VALUE_REQUIRED , 'Path to phpunit \'s coverage.xml ' )
32- ->addArgument ('output ' , InputOption::VALUE_REQUIRED , 'Path to write inspections report file to ' )
3342 ->addOption ('config ' , 'c ' , InputOption::VALUE_REQUIRED , 'Path to configuration file. Optional ' )
3443 ->addOption ('baseDir ' , '' , InputOption::VALUE_REQUIRED , 'Base directory from where to determine the relative config paths ' )
35- ->addOption ('report ' , '' , InputOption::VALUE_REQUIRED , 'output format, either checkstyle or gitlab ' , 'checkstyle ' )
44+ ->addOption ('reportGitlab ' , '' , InputOption::VALUE_OPTIONAL , 'Gitlab output format. To file or if absent to stdout ' , false )
45+ ->addOption ('reportCheckstyle ' , '' , InputOption::VALUE_OPTIONAL , 'Checkstyle output format. To file or if absent to stdout ' , false )
46+ ->addOption ('reportText ' , '' , InputOption::VALUE_OPTIONAL , 'User-friendly text output format. To file or if absent to stdout ' , false )
3647 ->addOption ('exit-code-on-failure ' , '' , InputOption::VALUE_NONE , 'If failures, exit with failure exit code ' );
3748 }
3849
@@ -41,25 +52,20 @@ protected function configure(): void
4152 */
4253 protected function execute (InputInterface $ input , OutputInterface $ output ): int
4354 {
44- $ configPath = FileUtil::getExistingFile ($ input ->getOption ('config ' ) ?? FileUtil::findFilePath ((string )getcwd (), self ::CONFIG_FILES ));
45- $ baseDir = $ input ->getOption ('baseDir ' ) ?? $ configPath ->getPath ();
46- $ coverageFilePath = FileUtil::getExistingFile ($ input ->getArgument ('coverage ' ));
47- $ outputFilePath = FileUtil::getFile ($ input ->getArgument ('output ' ));
48- $ schema = dirname (__DIR__ , 2 ) . '/resources/phpfci.xsd ' ;
49-
50- if (is_string ($ baseDir ) === false ) {
51- $ output ->writeln ("--baseDir argument is not valid. Expecting string argument " );
55+ $ inputConfig = $ this ->configFactory ->createInspectConfig ($ input );
56+ if ($ inputConfig instanceof ConfigViolation) {
57+ $ output ->writeln ($ inputConfig ->getMessage ());
5258
5359 return Command::FAILURE ;
5460 }
5561
5662 // gather data
57- $ domConfig = DOMDocumentFactory::getValidatedDOMDocument ($ configPath , $ schema );
58- $ config = InspectionConfigFactory::fromDOMDocument ($ baseDir , $ domConfig );
59- $ metrics = MetricsFactory::getFileMetrics (DOMDocumentFactory::getDOMDocument ($ coverageFilePath ));
63+ $ domConfig = DOMDocumentFactory::getValidatedDOMDocument ($ inputConfig -> getConfigPath () , $ this -> schemaPath );
64+ $ config = InspectionConfigFactory::fromDOMDocument ($ inputConfig -> getBaseDir () , $ domConfig );
65+ $ metrics = MetricsFactory::getFileMetrics (DOMDocumentFactory::getDOMDocument ($ inputConfig -> getCoverageFilepath () ));
6066
6167 if (count ($ metrics ) === 0 ) {
62- $ output ->writeln ("No metrics found in coverage file: " . $ coverageFilePath -> getPathname ());
68+ $ output ->writeln ("No metrics found in coverage file: " . $ inputConfig -> getCoverageFilepath ());
6369
6470 return Command::FAILURE ;
6571 }
@@ -68,20 +74,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6874 $ failures = (new MetricsAnalyzer ($ metrics , $ config ))->analyze ();
6975
7076 // write output
71- switch ($ input ->getOption ('report ' )) {
72- case 'checkstyle ' :
73- FileUtil::writeFile ($ outputFilePath , (new CheckStyleRenderer ())->render ($ config , $ failures ));
74- break ;
75- case 'gitlab ' :
76- FileUtil::writeFile ($ outputFilePath , (new GitlabErrorRenderer ())->render ($ config , $ failures ));
77- break ;
78- default :
79- $ output ->write ((new TextRenderer ())->render ($ config , $ failures ));
80- break ;
77+ if ($ inputConfig ->getReportGitlab () !== null ) {
78+ FileUtil::writeTo ($ inputConfig ->getReportGitlab (), (new GitlabErrorRenderer ())->render ($ config , $ failures ));
79+ }
80+ if ($ inputConfig ->getReportCheckstyle () !== null ) {
81+ FileUtil::writeTo ($ inputConfig ->getReportCheckstyle (), (new CheckStyleRenderer ())->render ($ config , $ failures ));
82+ }
83+ if ($ inputConfig ->getReportText () !== null ) {
84+ FileUtil::writeTo ($ inputConfig ->getReportText (), (new TextRenderer ())->render ($ config , $ failures ));
8185 }
8286
8387 // raise exit code on failure
84- if (count ($ failures ) > 0 && $ input -> getOption ( ' exit-code-on-failure ' ) !== false ) {
88+ if (count ($ failures ) > 0 && $ inputConfig -> isExitCodeOnFailure () ) {
8589 return Command::FAILURE ;
8690 }
8791
0 commit comments