55namespace Arkitect \CLI \Command ;
66
77use Arkitect \CLI \Config ;
8+ use Arkitect \CLI \Printer \Printer ;
89use Arkitect \CLI \Progress \DebugProgress ;
910use Arkitect \CLI \Progress \ProgressBarProgress ;
1011use Arkitect \CLI \Runner ;
@@ -26,6 +27,7 @@ class Check extends Command
2627 private const USE_BASELINE_PARAM = 'use-baseline ' ;
2728 private const SKIP_BASELINE_PARAM = 'skip-baseline ' ;
2829 private const IGNORE_BASELINE_LINENUMBERS_PARAM = 'ignore-baseline-linenumbers ' ;
30+ private const FORMAT_PARAM = 'format ' ;
2931
3032 private const GENERATE_BASELINE_PARAM = 'generate-baseline ' ;
3133 private const DEFAULT_RULES_FILENAME = 'phparkitect.php ' ;
@@ -87,6 +89,13 @@ protected function configure(): void
8789 'i ' ,
8890 InputOption::VALUE_NONE ,
8991 'Ignore line numbers when checking the baseline '
92+ )
93+ ->addOption (
94+ self ::FORMAT_PARAM ,
95+ 'f ' ,
96+ InputOption::VALUE_OPTIONAL ,
97+ 'Output format: json or text (default) ' ,
98+ 'text '
9099 );
91100 }
92101
@@ -102,6 +111,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
102111 $ useBaseline = (string ) $ input ->getOption (self ::USE_BASELINE_PARAM );
103112 $ skipBaseline = (bool ) $ input ->getOption (self ::SKIP_BASELINE_PARAM );
104113 $ ignoreBaselineLinenumbers = (bool ) $ input ->getOption (self ::IGNORE_BASELINE_LINENUMBERS_PARAM );
114+ $ format = $ input ->getOption (self ::FORMAT_PARAM );
115+ $ onlyErrors = Printer::FORMAT_JSON === $ format ;
105116
106117 if (true !== $ skipBaseline && !$ useBaseline && file_exists (self ::DEFAULT_BASELINE_FILENAME )) {
107118 $ useBaseline = self ::DEFAULT_BASELINE_FILENAME ;
@@ -112,7 +123,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
112123
113124 return self ::ERROR_CODE ;
114125 }
115- $ output ->writeln ('<info>Baseline found: ' .$ useBaseline .'</info> ' );
126+
127+ if (!$ onlyErrors ) {
128+ $ output ->writeln ('<info>Baseline found: ' .$ useBaseline .'</info> ' );
129+ }
116130
117131 $ generateBaseline = $ input ->getOption (self ::GENERATE_BASELINE_PARAM );
118132
@@ -122,18 +136,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
122136
123137 $ progress = $ verbose ? new DebugProgress ($ output ) : new ProgressBarProgress ($ output );
124138
125- $ this ->printHeadingLine ($ output );
139+ if (!$ onlyErrors ) {
140+ $ this ->printHeadingLine ($ output );
141+ }
126142
127143 $ rulesFilename = $ this ->getConfigFilename ($ input );
128- $ output ->writeln (sprintf ("Config file: %s \n" , $ rulesFilename ));
144+ if (!$ onlyErrors ) {
145+ $ output ->writeln (sprintf ("Config file: %s \n" , $ rulesFilename ));
146+ }
129147
130148 $ config = new Config ();
131149
132150 $ this ->readRules ($ config , $ rulesFilename );
133151
134152 $ runner = new Runner ($ stopOnFailure );
135153 try {
136- $ runner ->run ($ config , $ progress , $ targetPhpVersion );
154+ $ runner ->run ($ config , $ progress , $ targetPhpVersion, $ onlyErrors );
137155 } catch (FailOnFirstViolationException $ e ) {
138156 }
139157 $ violations = $ runner ->getViolations ();
@@ -146,7 +164,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
146164 $ this ->saveBaseline ($ generateBaseline , $ violations );
147165
148166 $ output ->writeln ('<info>Baseline file \'' .$ generateBaseline .'\'created!</info> ' );
149- $ this ->printExecutionTime ($ output , $ startTime );
167+ if (!$ onlyErrors ) {
168+ $ this ->printExecutionTime ($ output , $ startTime );
169+ }
150170
151171 return self ::SUCCESS_CODE ;
152172 }
@@ -158,15 +178,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
158178 }
159179
160180 if ($ violations ->count () > 0 ) {
161- $ this ->printViolations ($ violations , $ output );
162- $ this ->printExecutionTime ($ output , $ startTime );
181+ $ this ->printViolations ($ violations , $ output , $ format , $ onlyErrors );
182+ if (!$ onlyErrors ) {
183+ $ this ->printExecutionTime ($ output , $ startTime );
184+ }
163185
164186 return self ::ERROR_CODE ;
165187 }
166188
167189 $ parsedErrors = $ runner ->getParsingErrors ();
168190 if ($ parsedErrors ->count () > 0 ) {
169- $ this ->printParsedErrors ($ parsedErrors , $ output );
191+ $ this ->printParsedErrors ($ parsedErrors , $ output, $ onlyErrors );
170192 $ this ->printExecutionTime ($ output , $ startTime );
171193
172194 return self ::ERROR_CODE ;
@@ -178,8 +200,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
178200 return self ::ERROR_CODE ;
179201 }
180202
181- $ this ->printNoViolationsDetectedMessage ($ output );
182- $ this ->printExecutionTime ($ output , $ startTime );
203+ $ this ->printNoViolationsDetectedMessage ($ output , $ onlyErrors );
204+
205+ if (!$ onlyErrors ) {
206+ $ this ->printExecutionTime ($ output , $ startTime );
207+ }
183208
184209 return self ::SUCCESS_CODE ;
185210 }
@@ -236,21 +261,29 @@ private function getConfigFilename(InputInterface $input): string
236261 return $ filename ;
237262 }
238263
239- private function printViolations (Violations $ violations , OutputInterface $ output ): void
264+ private function printViolations (Violations $ violations , OutputInterface $ output, string $ format , bool $ onlyErrors = false ): void
240265 {
241- $ output ->writeln ('<error>ERRORS!</error> ' );
242- $ output ->writeln (sprintf ('%s ' , $ violations ->toString ()));
243- $ output ->writeln (sprintf ('<error>%s VIOLATIONS DETECTED!</error> ' , \count ($ violations )));
266+ if (!$ onlyErrors ) {
267+ $ output ->writeln ('<error>ERRORS!</error> ' );
268+ }
269+ $ output ->writeln (sprintf ('%s ' , $ violations ->toString ($ format )));
270+ if (!$ onlyErrors ) {
271+ $ output ->writeln (sprintf ('<error>%s VIOLATIONS DETECTED!</error> ' , \count ($ violations )));
272+ }
244273 }
245274
246- private function printParsedErrors (ParsingErrors $ parsingErrors , OutputInterface $ output ): void
275+ private function printParsedErrors (ParsingErrors $ parsingErrors , OutputInterface $ output, bool $ onlyErrors = false ): void
247276 {
248- $ output ->writeln ('<error>ERROR ON PARSING THESE FILES:</error> ' );
277+ if (!$ onlyErrors ) {
278+ $ output ->writeln ('<error>ERROR ON PARSING THESE FILES:</error> ' );
279+ }
249280 $ output ->writeln (sprintf ('%s ' , $ parsingErrors ->toString ()));
250281 }
251282
252- private function printNoViolationsDetectedMessage (OutputInterface $ output ): void
283+ private function printNoViolationsDetectedMessage (OutputInterface $ output, bool $ onlyErrors = false ): void
253284 {
254- $ output ->writeln ('<info>NO VIOLATIONS DETECTED!</info> ' );
285+ if (!$ onlyErrors ) {
286+ $ output ->writeln ('<info>NO VIOLATIONS DETECTED!</info> ' );
287+ }
255288 }
256289}
0 commit comments