55namespace Arkitect \CLI \Command ;
66
77use Arkitect \CLI \Config ;
8+ use Arkitect \CLI \Printer \PrinterFactory ;
89use Arkitect \CLI \Progress \DebugProgress ;
910use Arkitect \CLI \Progress \ProgressBarProgress ;
1011use Arkitect \CLI \Runner ;
@@ -109,6 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
109110 $ useBaseline = (string ) $ input ->getOption (self ::USE_BASELINE_PARAM );
110111 $ skipBaseline = (bool ) $ input ->getOption (self ::SKIP_BASELINE_PARAM );
111112 $ ignoreBaselineLinenumbers = (bool ) $ input ->getOption (self ::IGNORE_BASELINE_LINENUMBERS_PARAM );
113+ $ generateBaseline = $ input ->getOption (self ::GENERATE_BASELINE_PARAM );
112114 $ phpVersion = $ input ->getOption ('target-php-version ' );
113115 $ format = $ input ->getOption (self ::FORMAT_PARAM );
114116
@@ -117,49 +119,44 @@ protected function execute(InputInterface $input, OutputInterface $output): int
117119 $ stdOut = $ output ;
118120 $ output = $ output instanceof ConsoleOutputInterface ? $ output ->getErrorOutput () : $ output ;
119121
120- /** @var string|null $phpVersion */
121122 $ targetPhpVersion = TargetPhpVersion::create ($ phpVersion );
122123
123124 $ progress = $ verbose ? new DebugProgress ($ output ) : new ProgressBarProgress ($ output );
124125
126+ $ this ->printHeadingLine ($ output );
127+
125128 if (true !== $ skipBaseline && !$ useBaseline && file_exists (self ::DEFAULT_BASELINE_FILENAME )) {
126129 $ useBaseline = self ::DEFAULT_BASELINE_FILENAME ;
127130 }
128131
129132 if ($ useBaseline && !file_exists ($ useBaseline )) {
130- $ output ->writeln (' <error> Baseline file not found.</error> ' );
133+ $ output ->writeln (" ❌ Baseline file ' $ useBaseline ' not found." );
131134
132135 return self ::ERROR_CODE ;
133136 }
134137
135- $ output ->writeln ('<info>Baseline found: ' .$ useBaseline .'</info> ' );
136-
137- $ generateBaseline = $ input ->getOption (self ::GENERATE_BASELINE_PARAM );
138-
139- $ this ->printHeadingLine ($ output );
138+ $ output ->writeln ("Baseline file ' $ useBaseline' found " );
140139
141140 $ rulesFilename = $ this ->getConfigFilename ($ input );
142141
143- $ output ->writeln (\sprintf ( "Config file: %s \n" , $ rulesFilename ) );
142+ $ output ->writeln ("Config file ' $ rulesFilename ' found \n" );
144143
145144 $ config = new Config ();
146145
147146 $ this ->readRules ($ config , $ rulesFilename );
148147
149148 $ runner = new Runner ($ stopOnFailure );
150- $ runner ->run ($ config , $ progress , $ targetPhpVersion );
149+ $ result = $ runner ->run ($ config , $ progress , $ targetPhpVersion );
151150
152- $ violations = $ runner ->getViolations ();
153- $ violations ->sort ();
151+ $ violations = $ result ->getViolations ();
154152
155153 if (false !== $ generateBaseline ) {
156154 if (null === $ generateBaseline ) {
157155 $ generateBaseline = self ::DEFAULT_BASELINE_FILENAME ;
158156 }
159157 $ this ->saveBaseline ($ generateBaseline , $ violations );
160158
161- $ output ->writeln ('<info>Baseline file \'' .$ generateBaseline .'\'created!</info> ' );
162- $ this ->printExecutionTime ($ output , $ startTime );
159+ $ output ->writeln ("ℹ️ Baseline file ' $ generateBaseline' created! " );
163160
164161 return self ::SUCCESS_CODE ;
165162 }
@@ -170,37 +167,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
170167 $ violations ->remove ($ baseline , $ ignoreBaselineLinenumbers );
171168 }
172169
170+ $ printer = (new PrinterFactory ())->create ($ format );
171+
173172 // we always print this so we do not have to do additional ifs later
174- $ stdOut ->writeln ($ violations -> toString ( $ format ));
173+ $ stdOut ->writeln ($ printer -> print ( $ violations -> groupedByFqcn () ));
175174
176175 if ($ violations ->count () > 0 ) {
177- $ output ->writeln (\sprintf ('<error>⚠️ %s violations detected!</error> ' , \count ($ violations )));
178- $ this ->printExecutionTime ($ output , $ startTime );
179-
180- return self ::ERROR_CODE ;
176+ $ output ->writeln (\sprintf ('⚠️ %s violations detected! ' , \count ($ violations )));
181177 }
182178
183- $ parsedErrors = $ runner ->getParsingErrors ();
179+ if ($ result ->hasParsingErrors ()) {
180+ $ output ->writeln ('❌ could not parse these files: ' );
181+ $ output ->writeln ($ result ->getParsingErrors ()->toString ());
182+ }
184183
185- if ($ parsedErrors ->count () > 0 ) {
186- $ output ->writeln ('<error>❌ could not parse these files:</error> ' );
187- $ output ->writeln ($ parsedErrors ->toString ());
188- $ this ->printExecutionTime ($ output , $ startTime );
184+ !$ result ->hasErrors () && $ output ->writeln ('✅ No violations detected ' );
189185
190- return self ::ERROR_CODE ;
191- }
186+ return $ result ->hasErrors () ? self ::ERROR_CODE : self ::SUCCESS_CODE ;
192187 } catch (\Throwable $ e ) {
193- $ output ->writeln ($ e ->getMessage ());
194- $ this ->printExecutionTime ($ output , $ startTime );
188+ $ output ->writeln ("❌ {$ e ->getMessage ()}" );
195189
196190 return self ::ERROR_CODE ;
191+ } finally {
192+ $ this ->printExecutionTime ($ output , $ startTime );
197193 }
198-
199- $ output ->writeln ('<info>✅ No violations detected</info> ' );
200-
201- $ this ->printExecutionTime ($ output , $ startTime );
202-
203- return self ::SUCCESS_CODE ;
204194 }
205195
206196 protected function readRules (Config $ ruleChecker , string $ rulesFilename ): void
@@ -229,7 +219,7 @@ protected function printExecutionTime(OutputInterface $output, float $startTime)
229219 $ endTime = microtime (true );
230220 $ executionTime = number_format ($ endTime - $ startTime , 2 );
231221
232- $ output ->writeln (' <info> Execution time: ' . $ executionTime. " s</info> \n" );
222+ $ output ->writeln (" ⏱️ Execution time: $ executionTime \n" );
233223 }
234224
235225 private function loadBaseline (string $ filename ): Violations
@@ -250,7 +240,7 @@ private function getConfigFilename(InputInterface $input): string
250240 $ filename = self ::DEFAULT_RULES_FILENAME ;
251241 }
252242
253- Assert::file ($ filename , ' Config file not found ' );
243+ Assert::file ($ filename , " Config file ' $ filename ' not found" );
254244
255245 return $ filename ;
256246 }
0 commit comments