55namespace Arkitect \CLI \Command ;
66
77use Arkitect \CLI \Baseline ;
8- use Arkitect \CLI \Config ;
8+ use Arkitect \CLI \ConfigBuilder ;
99use Arkitect \CLI \Printer \PrinterFactory ;
1010use Arkitect \CLI \Progress \DebugProgress ;
1111use Arkitect \CLI \Progress \ProgressBarProgress ;
1212use Arkitect \CLI \Runner ;
1313use Arkitect \CLI \TargetPhpVersion ;
14- use Arkitect \Rules \Violations ;
1514use Symfony \Component \Console \Command \Command ;
1615use Symfony \Component \Console \Input \InputInterface ;
1716use Symfony \Component \Console \Input \InputOption ;
1817use Symfony \Component \Console \Output \ConsoleOutputInterface ;
1918use Symfony \Component \Console \Output \OutputInterface ;
20- use Webmozart \Assert \Assert ;
2119
2220class Check extends Command
2321{
@@ -51,7 +49,8 @@ protected function configure(): void
5149 self ::CONFIG_FILENAME_PARAM ,
5250 'c ' ,
5351 InputOption::VALUE_OPTIONAL ,
54- 'File containing configs, such as rules to be matched '
52+ 'File containing configs, such as rules to be matched ' ,
53+ self ::DEFAULT_RULES_FILENAME
5554 )
5655 ->addOption (
5756 self ::TARGET_PHP_PARAM ,
@@ -107,6 +106,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107106
108107 try {
109108 $ verbose = (bool ) $ input ->getOption ('verbose ' );
109+ $ rulesFilename = $ input ->getOption (self ::CONFIG_FILENAME_PARAM );
110110 $ stopOnFailure = (bool ) $ input ->getOption (self ::STOP_ON_FAILURE_PARAM );
111111 $ useBaseline = (string ) $ input ->getOption (self ::USE_BASELINE_PARAM );
112112 $ skipBaseline = (bool ) $ input ->getOption (self ::SKIP_BASELINE_PARAM );
@@ -120,46 +120,44 @@ protected function execute(InputInterface $input, OutputInterface $output): int
120120 $ stdOut = $ output ;
121121 $ output = $ output instanceof ConsoleOutputInterface ? $ output ->getErrorOutput () : $ output ;
122122
123- $ targetPhpVersion = TargetPhpVersion:: create ( $ phpVersion );
123+ $ this -> printHeadingLine ( $ output );
124124
125- $ progress = $ verbose ? new DebugProgress ($ output ) : new ProgressBarProgress ($ output );
125+ $ config = ConfigBuilder::loadFromFile ($ rulesFilename )
126+ ->stopOnFailure ($ stopOnFailure )
127+ ->targetPhpVersion (TargetPhpVersion::create ($ phpVersion ))
128+ ->baselineFilePath (Baseline::resolveFilePath ($ useBaseline , self ::DEFAULT_BASELINE_FILENAME ))
129+ ->ignoreBaselineLinenumbers ($ ignoreBaselineLinenumbers )
130+ ->skipBaseline ($ skipBaseline )
131+ ->format ($ format );
126132
127- $ baseline = Baseline ::create ($ skipBaseline , $ useBaseline , self :: DEFAULT_BASELINE_FILENAME );
133+ $ printer = PrinterFactory ::create ($ config -> getFormat () );
128134
129- $ printer = ( new PrinterFactory ())-> create ( $ format );
135+ $ progress = $ verbose ? new DebugProgress ( $ output ) : new ProgressBarProgress ( $ output );
130136
131- $ this -> printHeadingLine ( $ output );
137+ $ baseline = Baseline:: create ( $ config -> isSkipBaseline (), $ config -> getBaselineFilePath () );
132138
133139 $ baseline ->getFilename () && $ output ->writeln ("Baseline file ' {$ baseline ->getFilename ()}' found " );
134-
135- $ rulesFilename = $ this ->getConfigFilename ($ input );
136-
137140 $ output ->writeln ("Config file ' $ rulesFilename' found \n" );
138141
139- $ config = new Config ();
140-
141- $ this ->readRules ($ config , $ rulesFilename );
142-
143- $ runner = new Runner ($ stopOnFailure );
144- $ result = $ runner ->run ($ config , $ progress , $ targetPhpVersion );
145-
146- $ violations = $ result ->getViolations ();
142+ $ runner = new Runner ();
147143
148144 if (false !== $ generateBaseline ) {
149- $ baselineFilePath = Baseline::save ($ generateBaseline , self ::DEFAULT_BASELINE_FILENAME , $ violations );
145+ $ result = $ runner ->baseline ($ config , $ progress );
146+
147+ $ baselineFilePath = Baseline::save ($ generateBaseline , self ::DEFAULT_BASELINE_FILENAME , $ result ->getViolations ());
150148
151149 $ output ->writeln ("ℹ️ Baseline file ' $ baselineFilePath' created! " );
152150
153151 return self ::SUCCESS_CODE ;
154152 }
155153
156- $ baseline -> applyTo ( $ violations , $ ignoreBaselineLinenumbers );
154+ $ result = $ runner -> run ( $ config , $ baseline , $ progress );
157155
158156 // we always print this so we do not have to do additional ifs later
159- $ stdOut ->writeln ($ printer ->print ($ violations ->groupedByFqcn ()));
157+ $ stdOut ->writeln ($ printer ->print ($ result -> getViolations () ->groupedByFqcn ()));
160158
161- if ($ violations -> count () > 0 ) {
162- $ output ->writeln ("⚠️ {$ violations ->count ()} violations detected! " );
159+ if ($ result -> hasViolations () ) {
160+ $ output ->writeln ("⚠️ {$ result -> getViolations () ->count ()} violations detected! " );
163161 }
164162
165163 if ($ result ->hasParsingErrors ()) {
@@ -179,18 +177,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
179177 }
180178 }
181179
182- protected function readRules (Config $ ruleChecker , string $ rulesFilename ): void
183- {
184- \Closure::fromCallable (function () use ($ ruleChecker , $ rulesFilename ): ?bool {
185- /** @psalm-suppress UnresolvableInclude $config */
186- $ config = require $ rulesFilename ;
187-
188- Assert::isCallable ($ config );
189-
190- return $ config ($ ruleChecker );
191- })();
192- }
193-
194180 protected function printHeadingLine (OutputInterface $ output ): void
195181 {
196182 $ app = $ this ->getApplication ();
@@ -207,17 +193,4 @@ protected function printExecutionTime(OutputInterface $output, float $startTime)
207193
208194 $ output ->writeln ("⏱️ Execution time: $ executionTime \n" );
209195 }
210-
211- private function getConfigFilename (InputInterface $ input ): string
212- {
213- $ filename = $ input ->getOption (self ::CONFIG_FILENAME_PARAM );
214-
215- if (null === $ filename ) {
216- $ filename = self ::DEFAULT_RULES_FILENAME ;
217- }
218-
219- Assert::file ($ filename , "Config file ' $ filename' not found " );
220-
221- return $ filename ;
222- }
223196}
0 commit comments