55namespace Arkitect \CLI \Command ;
66
77use Arkitect \CLI \Config ;
8- use Arkitect \CLI \Printer \Printer ;
98use Arkitect \CLI \Progress \DebugProgress ;
109use Arkitect \CLI \Progress \ProgressBarProgress ;
1110use Arkitect \CLI \Runner ;
1211use Arkitect \CLI \TargetPhpVersion ;
13- use Arkitect \Rules \ParsingErrors ;
1412use Arkitect \Rules \Violations ;
1513use Symfony \Component \Console \Command \Command ;
1614use Symfony \Component \Console \Input \InputInterface ;
1715use Symfony \Component \Console \Input \InputOption ;
16+ use Symfony \Component \Console \Output \ConsoleOutputInterface ;
1817use Symfony \Component \Console \Output \OutputInterface ;
1918use Webmozart \Assert \Assert ;
2019
@@ -110,8 +109,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
110109 $ useBaseline = (string ) $ input ->getOption (self ::USE_BASELINE_PARAM );
111110 $ skipBaseline = (bool ) $ input ->getOption (self ::SKIP_BASELINE_PARAM );
112111 $ ignoreBaselineLinenumbers = (bool ) $ input ->getOption (self ::IGNORE_BASELINE_LINENUMBERS_PARAM );
112+ $ phpVersion = $ input ->getOption ('target-php-version ' );
113113 $ format = $ input ->getOption (self ::FORMAT_PARAM );
114- $ onlyErrors = Printer::FORMAT_JSON === $ format || Printer::FORMAT_GITLAB === $ format ;
114+
115+ // we write everything on STDERR apart from the list of violations which goes on STDOUT
116+ // this allows to pipe the output of this command to a file while showing output on the terminal
117+ $ stdOut = $ output ;
118+ $ output = $ output instanceof ConsoleOutputInterface ? $ output ->getErrorOutput () : $ output ;
119+
120+ /** @var string|null $phpVersion */
121+ $ targetPhpVersion = TargetPhpVersion::create ($ phpVersion );
122+
123+ $ progress = $ verbose ? new DebugProgress ($ output ) : new ProgressBarProgress ($ output );
115124
116125 if (true !== $ skipBaseline && !$ useBaseline && file_exists (self ::DEFAULT_BASELINE_FILENAME )) {
117126 $ useBaseline = self ::DEFAULT_BASELINE_FILENAME ;
@@ -123,33 +132,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
123132 return self ::ERROR_CODE ;
124133 }
125134
126- if (!$ onlyErrors ) {
127- $ output ->writeln ('<info>Baseline found: ' .$ useBaseline .'</info> ' );
128- }
135+ $ output ->writeln ('<info>Baseline found: ' .$ useBaseline .'</info> ' );
129136
130137 $ generateBaseline = $ input ->getOption (self ::GENERATE_BASELINE_PARAM );
131138
132- /** @var string|null $phpVersion */
133- $ phpVersion = $ input ->getOption ('target-php-version ' );
134- $ targetPhpVersion = TargetPhpVersion::create ($ phpVersion );
135-
136- $ progress = $ verbose ? new DebugProgress ($ output ) : new ProgressBarProgress ($ output );
137-
138- if (!$ onlyErrors ) {
139- $ this ->printHeadingLine ($ output );
140- }
139+ $ this ->printHeadingLine ($ output );
141140
142141 $ rulesFilename = $ this ->getConfigFilename ($ input );
143- if (!$ onlyErrors ) {
144- $ output ->writeln (\sprintf ("Config file: %s \n" , $ rulesFilename ));
145- }
142+
143+ $ output ->writeln (\sprintf ("Config file: %s \n" , $ rulesFilename ));
146144
147145 $ config = new Config ();
148146
149147 $ this ->readRules ($ config , $ rulesFilename );
150148
151149 $ runner = new Runner ($ stopOnFailure );
152- $ runner ->run ($ config , $ progress , $ targetPhpVersion, $ onlyErrors );
150+ $ runner ->run ($ config , $ progress , $ targetPhpVersion );
153151
154152 $ violations = $ runner ->getViolations ();
155153 $ violations ->sort ();
@@ -161,9 +159,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
161159 $ this ->saveBaseline ($ generateBaseline , $ violations );
162160
163161 $ output ->writeln ('<info>Baseline file \'' .$ generateBaseline .'\'created!</info> ' );
164- if (!$ onlyErrors ) {
165- $ this ->printExecutionTime ($ output , $ startTime );
166- }
162+ $ this ->printExecutionTime ($ output , $ startTime );
167163
168164 return self ::SUCCESS_CODE ;
169165 }
@@ -174,18 +170,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
174170 $ violations ->remove ($ baseline , $ ignoreBaselineLinenumbers );
175171 }
176172
173+ // we always print this so we do not have to do additional ifs later
174+ $ stdOut ->writeln ($ violations ->toString ($ format ));
175+
177176 if ($ violations ->count () > 0 ) {
178- $ this ->printViolations ($ violations , $ output , $ format , $ onlyErrors );
179- if (!$ onlyErrors ) {
180- $ this ->printExecutionTime ($ output , $ startTime );
181- }
177+ $ output ->writeln (\sprintf ('<error>⚠️ %s violations detected!</error> ' , \count ($ violations )));
178+ $ this ->printExecutionTime ($ output , $ startTime );
182179
183180 return self ::ERROR_CODE ;
184181 }
185182
186183 $ parsedErrors = $ runner ->getParsingErrors ();
184+
187185 if ($ parsedErrors ->count () > 0 ) {
188- $ this ->printParsedErrors ($ parsedErrors , $ output , $ onlyErrors );
186+ $ output ->writeln ('<error>❌ could not parse these files:</error> ' );
187+ $ output ->writeln ($ parsedErrors ->toString ());
189188 $ this ->printExecutionTime ($ output , $ startTime );
190189
191190 return self ::ERROR_CODE ;
@@ -197,11 +196,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
197196 return self ::ERROR_CODE ;
198197 }
199198
200- $ this -> printNoViolationsDetectedMessage ( $ output , $ onlyErrors , $ format );
199+ $ output -> writeln ( ' <info>✅ No violations detected</info> ' );
201200
202- if (!$ onlyErrors ) {
203- $ this ->printExecutionTime ($ output , $ startTime );
204- }
201+ $ this ->printExecutionTime ($ output , $ startTime );
205202
206203 return self ::SUCCESS_CODE ;
207204 }
@@ -257,33 +254,4 @@ private function getConfigFilename(InputInterface $input): string
257254
258255 return $ filename ;
259256 }
260-
261- private function printViolations (Violations $ violations , OutputInterface $ output , string $ format , bool $ onlyErrors = false ): void
262- {
263- if (!$ onlyErrors ) {
264- $ output ->writeln ('<error>ERRORS!</error> ' );
265- }
266-
267- $ output ->writeln (\sprintf ('%s ' , $ violations ->toString ($ format )));
268- if (!$ onlyErrors ) {
269- $ output ->writeln (\sprintf ('<error>%s VIOLATIONS DETECTED!</error> ' , \count ($ violations )));
270- }
271- }
272-
273- private function printParsedErrors (ParsingErrors $ parsingErrors , OutputInterface $ output , bool $ onlyErrors = false ): void
274- {
275- if (!$ onlyErrors ) {
276- $ output ->writeln ('<error>ERROR ON PARSING THESE FILES:</error> ' );
277- }
278- $ output ->writeln (\sprintf ('%s ' , $ parsingErrors ->toString ()));
279- }
280-
281- private function printNoViolationsDetectedMessage (OutputInterface $ output , bool $ onlyErrors = false , string $ format = Printer::FORMAT_TEXT ): void
282- {
283- if (!$ onlyErrors ) {
284- $ output ->writeln ('<info>NO VIOLATIONS DETECTED!</info> ' );
285- } elseif (Printer::FORMAT_JSON === $ format || Printer::FORMAT_GITLAB === $ format ) {
286- $ output ->writeln ('<info>[]</info> ' );
287- }
288- }
289257}
0 commit comments