44
55namespace Arkitect \CLI \Command ;
66
7+ use Arkitect \CLI \Baseline ;
78use Arkitect \CLI \Config ;
89use Arkitect \CLI \Printer \PrinterFactory ;
910use Arkitect \CLI \Progress \DebugProgress ;
@@ -123,19 +124,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
123124
124125 $ progress = $ verbose ? new DebugProgress ($ output ) : new ProgressBarProgress ($ output );
125126
126- $ this ->printHeadingLine ($ output );
127-
128- if (true !== $ skipBaseline && !$ useBaseline && file_exists (self ::DEFAULT_BASELINE_FILENAME )) {
129- $ useBaseline = self ::DEFAULT_BASELINE_FILENAME ;
130- }
127+ $ baseline = Baseline::create ($ skipBaseline , $ useBaseline , self ::DEFAULT_BASELINE_FILENAME );
131128
132- if ($ useBaseline && !file_exists ($ useBaseline )) {
133- $ output ->writeln ("❌ Baseline file ' $ useBaseline' not found. " );
129+ $ printer = (new PrinterFactory ())->create ($ format );
134130
135- return self ::ERROR_CODE ;
136- }
131+ $ this ->printHeadingLine ($ output );
137132
138- $ output ->writeln ("Baseline file ' $ useBaseline ' found " );
133+ $ baseline -> getFilename () && $ output ->writeln ("Baseline file ' { $ baseline -> getFilename ()} ' found " );
139134
140135 $ rulesFilename = $ this ->getConfigFilename ($ input );
141136
@@ -151,29 +146,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
151146 $ violations = $ result ->getViolations ();
152147
153148 if (false !== $ generateBaseline ) {
154- if (null === $ generateBaseline ) {
155- $ generateBaseline = self ::DEFAULT_BASELINE_FILENAME ;
156- }
157- $ this ->saveBaseline ($ generateBaseline , $ violations );
149+ $ baselineFilePath = Baseline::save ($ generateBaseline , self ::DEFAULT_BASELINE_FILENAME , $ violations );
158150
159- $ output ->writeln ("ℹ️ Baseline file ' $ generateBaseline ' created! " );
151+ $ output ->writeln ("ℹ️ Baseline file ' $ baselineFilePath ' created! " );
160152
161153 return self ::SUCCESS_CODE ;
162154 }
163155
164- if ($ useBaseline ) {
165- $ baseline = $ this ->loadBaseline ($ useBaseline );
166-
167- $ violations ->remove ($ baseline , $ ignoreBaselineLinenumbers );
168- }
169-
170- $ printer = (new PrinterFactory ())->create ($ format );
156+ $ baseline ->applyTo ($ violations , $ ignoreBaselineLinenumbers );
171157
172158 // we always print this so we do not have to do additional ifs later
173159 $ stdOut ->writeln ($ printer ->print ($ violations ->groupedByFqcn ()));
174160
175161 if ($ violations ->count () > 0 ) {
176- $ output ->writeln (\sprintf ( ' ⚠️ %s violations detected! ' , \count ( $ violations )) );
162+ $ output ->writeln (" ⚠️ { $ violations -> count ()} violations detected! " );
177163 }
178164
179165 if ($ result ->hasParsingErrors ()) {
@@ -222,16 +208,6 @@ protected function printExecutionTime(OutputInterface $output, float $startTime)
222208 $ output ->writeln ("⏱️ Execution time: $ executionTime \n" );
223209 }
224210
225- private function loadBaseline (string $ filename ): Violations
226- {
227- return Violations::fromJson (file_get_contents ($ filename ));
228- }
229-
230- private function saveBaseline (string $ filename , Violations $ violations ): void
231- {
232- file_put_contents ($ filename , json_encode ($ violations , \JSON_PRETTY_PRINT ));
233- }
234-
235211 private function getConfigFilename (InputInterface $ input ): string
236212 {
237213 $ filename = $ input ->getOption (self ::CONFIG_FILENAME_PARAM );
0 commit comments