5
5
namespace Arkitect \CLI \Command ;
6
6
7
7
use Arkitect \CLI \Config ;
8
+ use Arkitect \CLI \Printer \PrinterFactory ;
8
9
use Arkitect \CLI \Progress \DebugProgress ;
9
10
use Arkitect \CLI \Progress \ProgressBarProgress ;
10
11
use Arkitect \CLI \Runner ;
@@ -109,6 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
109
110
$ useBaseline = (string ) $ input ->getOption (self ::USE_BASELINE_PARAM );
110
111
$ skipBaseline = (bool ) $ input ->getOption (self ::SKIP_BASELINE_PARAM );
111
112
$ ignoreBaselineLinenumbers = (bool ) $ input ->getOption (self ::IGNORE_BASELINE_LINENUMBERS_PARAM );
113
+ $ generateBaseline = $ input ->getOption (self ::GENERATE_BASELINE_PARAM );
112
114
$ phpVersion = $ input ->getOption ('target-php-version ' );
113
115
$ format = $ input ->getOption (self ::FORMAT_PARAM );
114
116
@@ -117,49 +119,44 @@ protected function execute(InputInterface $input, OutputInterface $output): int
117
119
$ stdOut = $ output ;
118
120
$ output = $ output instanceof ConsoleOutputInterface ? $ output ->getErrorOutput () : $ output ;
119
121
120
- /** @var string|null $phpVersion */
121
122
$ targetPhpVersion = TargetPhpVersion::create ($ phpVersion );
122
123
123
124
$ progress = $ verbose ? new DebugProgress ($ output ) : new ProgressBarProgress ($ output );
124
125
126
+ $ this ->printHeadingLine ($ output );
127
+
125
128
if (true !== $ skipBaseline && !$ useBaseline && file_exists (self ::DEFAULT_BASELINE_FILENAME )) {
126
129
$ useBaseline = self ::DEFAULT_BASELINE_FILENAME ;
127
130
}
128
131
129
132
if ($ useBaseline && !file_exists ($ useBaseline )) {
130
- $ output ->writeln (' <error> Baseline file not found.</error> ' );
133
+ $ output ->writeln (" ❌ Baseline file ' $ useBaseline ' not found." );
131
134
132
135
return self ::ERROR_CODE ;
133
136
}
134
137
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 " );
140
139
141
140
$ rulesFilename = $ this ->getConfigFilename ($ input );
142
141
143
- $ output ->writeln (\sprintf ( "Config file: %s \n" , $ rulesFilename ) );
142
+ $ output ->writeln ("Config file ' $ rulesFilename ' found \n" );
144
143
145
144
$ config = new Config ();
146
145
147
146
$ this ->readRules ($ config , $ rulesFilename );
148
147
149
148
$ runner = new Runner ($ stopOnFailure );
150
- $ runner ->run ($ config , $ progress , $ targetPhpVersion );
149
+ $ result = $ runner ->run ($ config , $ progress , $ targetPhpVersion );
151
150
152
- $ violations = $ runner ->getViolations ();
153
- $ violations ->sort ();
151
+ $ violations = $ result ->getViolations ();
154
152
155
153
if (false !== $ generateBaseline ) {
156
154
if (null === $ generateBaseline ) {
157
155
$ generateBaseline = self ::DEFAULT_BASELINE_FILENAME ;
158
156
}
159
157
$ this ->saveBaseline ($ generateBaseline , $ violations );
160
158
161
- $ output ->writeln ('<info>Baseline file \'' .$ generateBaseline .'\'created!</info> ' );
162
- $ this ->printExecutionTime ($ output , $ startTime );
159
+ $ output ->writeln ("ℹ️ Baseline file ' $ generateBaseline' created! " );
163
160
164
161
return self ::SUCCESS_CODE ;
165
162
}
@@ -170,37 +167,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
170
167
$ violations ->remove ($ baseline , $ ignoreBaselineLinenumbers );
171
168
}
172
169
170
+ $ printer = (new PrinterFactory ())->create ($ format );
171
+
173
172
// 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 () ));
175
174
176
175
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 )));
181
177
}
182
178
183
- $ parsedErrors = $ runner ->getParsingErrors ();
179
+ if ($ result ->hasParsingErrors ()) {
180
+ $ output ->writeln ('❌ could not parse these files: ' );
181
+ $ output ->writeln ($ result ->getParsingErrors ()->toString ());
182
+ }
184
183
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 ' );
189
185
190
- return self ::ERROR_CODE ;
191
- }
186
+ return $ result ->hasErrors () ? self ::ERROR_CODE : self ::SUCCESS_CODE ;
192
187
} catch (\Throwable $ e ) {
193
- $ output ->writeln ($ e ->getMessage ());
194
- $ this ->printExecutionTime ($ output , $ startTime );
188
+ $ output ->writeln ("❌ {$ e ->getMessage ()}" );
195
189
196
190
return self ::ERROR_CODE ;
191
+ } finally {
192
+ $ this ->printExecutionTime ($ output , $ startTime );
197
193
}
198
-
199
- $ output ->writeln ('<info>✅ No violations detected</info> ' );
200
-
201
- $ this ->printExecutionTime ($ output , $ startTime );
202
-
203
- return self ::SUCCESS_CODE ;
204
194
}
205
195
206
196
protected function readRules (Config $ ruleChecker , string $ rulesFilename ): void
@@ -229,7 +219,7 @@ protected function printExecutionTime(OutputInterface $output, float $startTime)
229
219
$ endTime = microtime (true );
230
220
$ executionTime = number_format ($ endTime - $ startTime , 2 );
231
221
232
- $ output ->writeln (' <info> Execution time: ' . $ executionTime. " s</info> \n" );
222
+ $ output ->writeln (" ⏱️ Execution time: $ executionTime \n" );
233
223
}
234
224
235
225
private function loadBaseline (string $ filename ): Violations
@@ -250,7 +240,7 @@ private function getConfigFilename(InputInterface $input): string
250
240
$ filename = self ::DEFAULT_RULES_FILENAME ;
251
241
}
252
242
253
- Assert::file ($ filename , ' Config file not found ' );
243
+ Assert::file ($ filename , " Config file ' $ filename ' not found" );
254
244
255
245
return $ filename ;
256
246
}
0 commit comments