5
5
namespace Arkitect \CLI \Command ;
6
6
7
7
use Arkitect \CLI \Baseline ;
8
- use Arkitect \CLI \Config ;
8
+ use Arkitect \CLI \ConfigBuilder ;
9
9
use Arkitect \CLI \Printer \PrinterFactory ;
10
10
use Arkitect \CLI \Progress \DebugProgress ;
11
11
use Arkitect \CLI \Progress \ProgressBarProgress ;
12
12
use Arkitect \CLI \Runner ;
13
13
use Arkitect \CLI \TargetPhpVersion ;
14
- use Arkitect \Rules \Violations ;
15
14
use Symfony \Component \Console \Command \Command ;
16
15
use Symfony \Component \Console \Input \InputInterface ;
17
16
use Symfony \Component \Console \Input \InputOption ;
18
17
use Symfony \Component \Console \Output \ConsoleOutputInterface ;
19
18
use Symfony \Component \Console \Output \OutputInterface ;
20
- use Webmozart \Assert \Assert ;
21
19
22
20
class Check extends Command
23
21
{
@@ -51,7 +49,8 @@ protected function configure(): void
51
49
self ::CONFIG_FILENAME_PARAM ,
52
50
'c ' ,
53
51
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
55
54
)
56
55
->addOption (
57
56
self ::TARGET_PHP_PARAM ,
@@ -107,6 +106,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107
106
108
107
try {
109
108
$ verbose = (bool ) $ input ->getOption ('verbose ' );
109
+ $ rulesFilename = $ input ->getOption (self ::CONFIG_FILENAME_PARAM );
110
110
$ stopOnFailure = (bool ) $ input ->getOption (self ::STOP_ON_FAILURE_PARAM );
111
111
$ useBaseline = (string ) $ input ->getOption (self ::USE_BASELINE_PARAM );
112
112
$ skipBaseline = (bool ) $ input ->getOption (self ::SKIP_BASELINE_PARAM );
@@ -120,46 +120,44 @@ protected function execute(InputInterface $input, OutputInterface $output): int
120
120
$ stdOut = $ output ;
121
121
$ output = $ output instanceof ConsoleOutputInterface ? $ output ->getErrorOutput () : $ output ;
122
122
123
- $ targetPhpVersion = TargetPhpVersion:: create ( $ phpVersion );
123
+ $ this -> printHeadingLine ( $ output );
124
124
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 );
126
132
127
- $ baseline = Baseline ::create ($ skipBaseline , $ useBaseline , self :: DEFAULT_BASELINE_FILENAME );
133
+ $ printer = PrinterFactory ::create ($ config -> getFormat () );
128
134
129
- $ printer = ( new PrinterFactory ())-> create ( $ format );
135
+ $ progress = $ verbose ? new DebugProgress ( $ output ) : new ProgressBarProgress ( $ output );
130
136
131
- $ this -> printHeadingLine ( $ output );
137
+ $ baseline = Baseline:: create ( $ config -> isSkipBaseline (), $ config -> getBaselineFilePath () );
132
138
133
139
$ baseline ->getFilename () && $ output ->writeln ("Baseline file ' {$ baseline ->getFilename ()}' found " );
134
-
135
- $ rulesFilename = $ this ->getConfigFilename ($ input );
136
-
137
140
$ output ->writeln ("Config file ' $ rulesFilename' found \n" );
138
141
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 ();
147
143
148
144
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 ());
150
148
151
149
$ output ->writeln ("ℹ️ Baseline file ' $ baselineFilePath' created! " );
152
150
153
151
return self ::SUCCESS_CODE ;
154
152
}
155
153
156
- $ baseline -> applyTo ( $ violations , $ ignoreBaselineLinenumbers );
154
+ $ result = $ runner -> run ( $ config , $ baseline , $ progress );
157
155
158
156
// 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 ()));
160
158
161
- if ($ violations -> count () > 0 ) {
162
- $ output ->writeln ("⚠️ {$ violations ->count ()} violations detected! " );
159
+ if ($ result -> hasViolations () ) {
160
+ $ output ->writeln ("⚠️ {$ result -> getViolations () ->count ()} violations detected! " );
163
161
}
164
162
165
163
if ($ result ->hasParsingErrors ()) {
@@ -179,18 +177,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
179
177
}
180
178
}
181
179
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
-
194
180
protected function printHeadingLine (OutputInterface $ output ): void
195
181
{
196
182
$ app = $ this ->getApplication ();
@@ -207,17 +193,4 @@ protected function printExecutionTime(OutputInterface $output, float $startTime)
207
193
208
194
$ output ->writeln ("⏱️ Execution time: $ executionTime \n" );
209
195
}
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
- }
223
196
}
0 commit comments