-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathReportBuilder.php
257 lines (230 loc) · 9.38 KB
/
ReportBuilder.php
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
namespace Magento\SemanticVersionChecker;
use Exception;
use Magento\SemanticVersionChecker\Analyzer\AnalyzerInterface;
use Magento\SemanticVersionChecker\Analyzer\Factory\AnalyzerFactory;
use Magento\SemanticVersionChecker\Analyzer\Factory\AnalyzerFactoryInterface;
use Magento\SemanticVersionChecker\Analyzer\Factory\DbSchemaAnalyzerFactory;
use Magento\SemanticVersionChecker\Analyzer\Factory\DiAnalyzerFactory;
use Magento\SemanticVersionChecker\Analyzer\Factory\LayoutAnalyzerFactory;
use Magento\SemanticVersionChecker\Analyzer\Factory\NonApiAnalyzerFactory;
use Magento\SemanticVersionChecker\Analyzer\Factory\SystemXmlAnalyzerFactory;
use Magento\SemanticVersionChecker\Analyzer\Factory\XsdAnalyzerFactory;
use Magento\SemanticVersionChecker\ClassHierarchy\StaticAnalyzerFactory;
use Magento\SemanticVersionChecker\Analyzer\Factory\LessAnalyzerFactory;
use Magento\SemanticVersionChecker\Filter\FilePatternFilter;
use Magento\SemanticVersionChecker\Filter\SourceWithJsonFilter;
use Magento\SemanticVersionChecker\Finder\FinderDecoratorFactory;
use Magento\SemanticVersionChecker\Scanner\ScannerRegistryFactory;
use Magento\SemanticVersionChecker\Analyzer\Factory\MftfAnalyzerFactory;
use Magento\SemanticVersionChecker\Analyzer\Factory\EtSchemaAnalyzerFactory;
use PHPSemVerChecker\Configuration\LevelMapping;
use PHPSemVerChecker\Report\Report;
use PHPSemVerChecker\SemanticVersioning\Level;
class ReportBuilder
{
/** @var string */
protected $includePatternsPath;
/** @var string */
protected $excludePatternsPath;
/** @var string */
protected $sourceBeforeDir;
/** @var string */
protected $sourceAfterDir;
/**
* Defines available analyzer factories list for the different report types.
*
* @var array
*/
protected $availableAnalyzerFactoryClasses = [
ReportTypes::API => AnalyzerFactory::class,
ReportTypes::ALL => NonApiAnalyzerFactory::class,
ReportTypes::DB_SCHEMA => DbSchemaAnalyzerFactory::class,
ReportTypes::DI_XML => DiAnalyzerFactory::class,
ReportTypes::LAYOUT_XML => LayoutAnalyzerFactory::class,
ReportTypes::SYSTEM_XML => SystemXmlAnalyzerFactory::class,
ReportTypes::XSD => XsdAnalyzerFactory::class,
ReportTypes::LESS => LessAnalyzerFactory::class,
ReportTypes::MFTF => MftfAnalyzerFactory::class,
ReportTypes::ET_SCHEMA => EtSchemaAnalyzerFactory::class,
];
/**
* Define analyzer factory list for the different report types.
* @var string[]
*/
private $analyzerFactoryClasses = [
ReportTypes::API => AnalyzerFactory::class,
ReportTypes::ALL => NonApiAnalyzerFactory::class,
ReportTypes::DB_SCHEMA => DbSchemaAnalyzerFactory::class,
ReportTypes::DI_XML => DiAnalyzerFactory::class,
ReportTypes::LAYOUT_XML => LayoutAnalyzerFactory::class,
ReportTypes::SYSTEM_XML => SystemXmlAnalyzerFactory::class,
ReportTypes::XSD => XsdAnalyzerFactory::class,
ReportTypes::LESS => LessAnalyzerFactory::class,
ReportTypes::ET_SCHEMA => EtSchemaAnalyzerFactory::class,
];
/**
* Constructor.
*
* @param string $includePatternsPath
* @param string $excludePatternsPath
* @param string $sourceBeforeDir
* @param string $sourceAfterDir
* @param array $reportType
*/
public function __construct(
$includePatternsPath,
$excludePatternsPath,
$sourceBeforeDir,
$sourceAfterDir,
array $reportType = []
) {
$this->includePatternsPath = $includePatternsPath;
$this->excludePatternsPath = $excludePatternsPath;
$this->sourceBeforeDir = $sourceBeforeDir;
$this->sourceAfterDir = $sourceAfterDir;
// If report-type(s) provided as argument via compare command it will override analyzers to specified
if (!empty($reportType)) {
$this->analyzerFactoryClasses = array_intersect_key(
$this->availableAnalyzerFactoryClasses,
array_flip($reportType)
);
}
}
/**
* @return Report
*/
public function makeCompleteVersionReport()
{
return $this->makeVersionReport();
}
/**
* Set Magento's custom severity level overrides then build a report based on type
*
* @return Report
*/
protected function makeVersionReport()
{
$originalMapping = LevelMapping::$mapping;
// Customize severity level of some @api changes
LevelMapping::setOverrides(
[
'V015' => Level::MINOR, // Add public method
'V016' => Level::MINOR, // Add protected method
'V019' => Level::MINOR, // Add public property
'V020' => Level::MINOR, // Add protected property
'V034' => Level::MAJOR, // Add public method to an interface
'V047' => Level::MINOR, // Add public method to trait
'V048' => Level::MINOR, // Add protected method to trait
'V057' => Level::MINOR, // Add private method to trait
'V059' => Level::MAJOR, // App method parameter to private method of trait
'V060' => Level::MAJOR, // Public class method parameter change
'V063' => Level::MAJOR, // Public interface method parameter change
'V064' => Level::MAJOR, // Public trait method parameter change
'V066' => Level::MAJOR, // Private trait method parameter change
]
);
// try {
$report = $this->buildReport();
// } finally {
// Restore original severity levels
LevelMapping::setOverrides($originalMapping);
// }
return $report;
}
/**
* Get the mapping of report type -> analyzer factory
*
* @return string[]
*/
protected function getAnalyzerFactoryClasses()
{
return $this->analyzerFactoryClasses;
}
/**
* Create a report based on type
*
* @return Report
* @throws Exception
*/
protected function buildReport()
{
$finderDecoratorFactory = new FinderDecoratorFactory();
$fileIterator = $finderDecoratorFactory->create();
$sourceBeforeFiles = $fileIterator->findFromString($this->sourceBeforeDir, '', '');
$sourceAfterFiles = $fileIterator->findFromString($this->sourceAfterDir, '', '');
$staticAnalyzerBefore = (new StaticAnalyzerFactory())->create();
$staticAnalyzerAfter = (new StaticAnalyzerFactory())->create();
/**
* Run dependency analysis over entire codebase. Necessary as we should parse parents and siblings of unchanged
* files.
*/
$dependencyMapBefore = $staticAnalyzerBefore->analyse($sourceBeforeFiles);
$dependencyMapAfter = $staticAnalyzerAfter->analyse($sourceAfterFiles);
//scan files
$scannerRegistryFactory = new ScannerRegistryFactory();
$scannerBefore = new ScannerRegistry($scannerRegistryFactory->create($dependencyMapBefore, $dependencyMapAfter));
$scannerAfter = new ScannerRegistry($scannerRegistryFactory->create($dependencyMapAfter, $dependencyMapBefore));
/**
* Filter unchanged files. (All json files will remain because of filter)
*/
foreach ($this->getFilters($this->sourceBeforeDir, $this->sourceAfterDir) as $filter) {
// filters modify arrays by reference
$filter->filter($sourceBeforeFiles, $sourceAfterFiles);
}
foreach ($sourceBeforeFiles as $file) {
$scannerBefore->scanFile($file);
}
foreach ($sourceAfterFiles as $file) {
$scannerAfter->scanFile($file);
}
$beforeRegistryList = $scannerBefore->getScannerRegistryList();
$afterRegistryList = $scannerAfter->getScannerRegistryList();
$report = null;
/**
* @var AnalyzerFactoryInterface $factory
*/
foreach ($this->getAnalyzerFactoryClasses() as $reportType => $factory) {
/** @var AnalyzerInterface $analyzer */
$analyzer = (new $factory())->create($dependencyMapAfter);
$tmpReport = $analyzer->analyze(
$beforeRegistryList[$reportType],
$afterRegistryList[$reportType]
);
if ($report === null) {
$report = $tmpReport;
} else {
/** @var Report $report */
$report = $report->merge($tmpReport);
}
}
return $report;
}
/**
* Get filters for source files
*
* @param string $sourceBeforeDir
* @param string $sourceAfterDir
* @return array
*/
protected function getFilters($sourceBeforeDir, $sourceAfterDir): array
{
$filters = [
// always filter out files that are identical before and after except for JSON :facepalm:
new SourceWithJsonFilter(),
// process the include and exclude patterns
new FilePatternFilter(
$this->includePatternsPath,
$this->excludePatternsPath,
$sourceBeforeDir,
$sourceAfterDir
),
];
return $filters;
}
}