-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathRunner.php
More file actions
172 lines (138 loc) · 5.05 KB
/
Runner.php
File metadata and controls
172 lines (138 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types=1);
namespace Arkitect\CLI;
use Arkitect\Analyzer\ClassDescription;
use Arkitect\Analyzer\FileParserFactory;
use Arkitect\Analyzer\FilesToParse;
use Arkitect\Analyzer\ParsedFiles;
use Arkitect\Analyzer\Parser;
use Arkitect\Analyzer\ParsingErrors;
use Arkitect\ClassSetRules;
use Arkitect\CLI\Progress\Progress;
use Arkitect\Exceptions\FailOnFirstViolationException;
use Arkitect\Rules\Violations;
use Symfony\Component\Finder\SplFileInfo;
class Runner
{
public function run(Config $config, Baseline $baseline, Progress $progress): AnalysisResult
{
[$violations, $parsingErrors] = $this->doRun($config, $progress);
$baseline->applyTo($violations, $config->isIgnoreBaselineLinenumbers());
return new AnalysisResult(
$violations,
$parsingErrors,
);
}
public function baseline(Config $config, Progress $progress): AnalysisResult
{
[$violations, $parsingErrors] = $this->doRun($config, $progress);
return new AnalysisResult(
$violations,
$parsingErrors,
);
}
public function check(
ClassSetRules $classSetRule,
Progress $progress,
Parser $fileParser,
Violations $violations,
ParsingErrors $parsingErrors,
bool $stopOnFailure,
): void {
// first step: collect all files to parse
$filesToParse = $this->collectFilesToParse($classSetRule);
// second step: parse all files and collect results
$parsedFiles = $this->collectParsedFiles(
$filesToParse,
$fileParser,
$progress
);
// third step: check all rules on all files
$this->checkRulesOnParsedFiles(
$classSetRule,
$parsedFiles,
$violations,
$parsingErrors,
$stopOnFailure
);
}
public function checkRulesOnParsedFiles(
ClassSetRules $classSetRule,
ParsedFiles $parsedFiles,
Violations $violations,
ParsingErrors $parsingErrors,
bool $stopOnFailure,
): void {
/** @var SplFileInfo $file */
foreach ($classSetRule->getClassSet() as $file) {
$result = $parsedFiles->get($file->getRelativePathname());
if (null === $result) {
continue; // this should not happen
}
$parsingErrors->merge($result->parsingErrors());
$fileViolations = new Violations();
/** @var ClassDescription $classDescription */
foreach ($result->classDescriptions() as $classDescription) {
foreach ($classSetRule->getRules() as $rule) {
$rule->check($classDescription, $fileViolations);
if ($stopOnFailure && $fileViolations->count() > 0) {
$violations->merge($fileViolations);
throw new FailOnFirstViolationException();
}
}
}
$violations->merge($fileViolations);
}
}
protected function collectFilesToParse(ClassSetRules $classSetRule): FilesToParse
{
$filesToParse = new FilesToParse();
/** @var SplFileInfo $file */
foreach ($classSetRule->getClassSet() as $file) {
$filesToParse->add($file);
}
return $filesToParse;
}
protected function collectParsedFiles(FilesToParse $filesToParse, Parser $fileParser, Progress $progress): ParsedFiles
{
$parsedFiles = new ParsedFiles();
/** @var SplFileInfo $file */
foreach ($filesToParse as $file) {
$progress->startParsingFile($file->getRelativePathname());
$result = $fileParser->parse($file->getContents(), $file->getRelativePathname());
$parsedFiles->add($file->getRelativePathname(), $result);
$progress->endParsingFile($file->getRelativePathname());
}
return $parsedFiles;
}
protected function doRun(Config $config, Progress $progress): array
{
$violations = new Violations();
$parsingErrors = new ParsingErrors();
$fileParser = FileParserFactory::createFileParser(
$config->getTargetPhpVersion(),
$config->isParseCustomAnnotationsEnabled(),
$config->getCacheFilePath()
);
/** @var ClassSetRules $classSetRule */
foreach ($config->getClassSetRules() as $classSetRule) {
$progress->startFileSetAnalysis($classSetRule->getClassSet());
try {
$this->check(
$classSetRule,
$progress,
$fileParser,
$violations,
$parsingErrors,
$config->isStopOnFailure()
);
} catch (FailOnFirstViolationException $e) {
break;
} finally {
$progress->endFileSetAnalysis($classSetRule->getClassSet());
}
}
$violations->sort();
return [$violations, $parsingErrors];
}
}