/
PhpFixerRunner.php
275 lines (244 loc) · 7.17 KB
/
PhpFixerRunner.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
namespace Concrete\Core\Support\CodingStyle;
use ArrayObject;
use PhpCsFixer\Cache\Directory;
use PhpCsFixer\Cache\FileCacheManager;
use PhpCsFixer\Cache\FileHandler;
use PhpCsFixer\Cache\NullCacheManager;
use PhpCsFixer\Cache\Signature;
use PhpCsFixer\Error\ErrorsManager;
use PhpCsFixer\Linter\Linter;
use PhpCsFixer\Runner\Runner;
use PhpCsFixer\ToolInfo;
use Concrete\Core\Events\EventDispatcher;
use Symfony\Component\Finder\Finder;
use Traversable;
/**
* Wrapper to PHP-CS-Fixer.
*/
class PhpFixerRunner
{
/**
* @var \Concrete\Core\Support\CodingStyle\PhpFixerRuleResolver
*/
protected $ruleResolver;
/**
* @var EventDispatcher
*/
protected $eventDispatcher;
/**
* @var array
*/
protected $steps;
/**
* @var \PhpCsFixer\Error\ErrorsManager
*/
protected $errorManager;
/**
* @var string|null
*/
private $phpCSFixerVersion;
/**
* @param \Concrete\Core\Support\CodingStyle\PhpFixerRuleResolver $ruleResolver
* @param EventDispatcher $eventDispatcher
*/
public function __construct(PhpFixerRuleResolver $ruleResolver, EventDispatcher $eventDispatcher)
{
$this->ruleResolver = $ruleResolver;
$this->eventDispatcher = $eventDispatcher;
$this->resetSteps();
$this->resetErrors();
}
/**
* @return EventDispatcher
*/
public function getEventDispatcher()
{
return $this->eventDispatcher;
}
/**
* @param \Concrete\Core\Support\CodingStyle\PhpFixerOptions $options
* @param array $paths
* @param int $flags
*
* @return $this
*/
public function addStep(PhpFixerOptions $options, array $paths, $flags)
{
$this->steps[] = [
'options' => $options,
'finder' => $this->createFinder($options, $paths),
'flags' => $flags,
];
return $this;
}
/**
* @return $this
*/
public function resetSteps()
{
$this->steps = [];
return $this;
}
/**
* @return \PhpCsFixer\Error\ErrorsManager
*/
public function getErrorManager()
{
return $this->errorManager;
}
/**
* @return int
*/
public function calculateNumberOfFiles()
{
$sum = 0;
foreach (array_keys($this->steps) as $stepIndex) {
$this->expandFinder($stepIndex);
$sum += count($this->steps[$stepIndex]['finder']);
}
return $sum;
}
/**
* @param bool $dryRun
*
* @return array
*/
public function apply($dryRun)
{
$this->resetErrors();
$changes = [];
foreach ($this->steps as $step) {
$changes += $this->applyStep($step['options'], $step['finder'], $step['flags'], $dryRun);
}
return $changes;
}
/**
* @param \Concrete\Core\Support\CodingStyle\PhpFixerOptions $options
* @param \Symfony\Component\Finder\Finder|\ArrayObject $finder
* @param int $flags
* @param bool $dryRun
*
* @return array|void[]|NULL[]|string[][]|string[][][]
*/
protected function applyStep(PhpFixerOptions $options, Traversable $finder, $flags, $dryRun)
{
$rules = $this->ruleResolver->getRules($flags, true, $options->getMinimumPhpVersion());
$fixers = $this->ruleResolver->getFixers($rules);
$linter = new Linter();
$runner = new Runner(
$finder,
$fixers,
new Differ(),
$this->eventDispatcher->getEventDispatcher(),
$this->errorManager,
$linter,
$dryRun,
$this->createCacheManager($options, $flags, $dryRun, $rules),
null,
false
);
$changes = [];
$fixResult = $runner->fix();
foreach ($fixResult as $file => &$data) {
$file = str_replace(DIRECTORY_SEPARATOR, '/', $file);
if (strpos($file, $options->getWebRoot()) === 0) {
$file = substr($file, strlen($options->getWebRoot()));
}
$changes[$file] = $data;
}
return $changes;
}
/**
* @param \Concrete\Core\Support\CodingStyle\PhpFixerOptions $options
* @param array $paths
*
* @return \Symfony\Component\Finder\Finder
*/
protected function createFinder(PhpFixerOptions $options, array $paths)
{
$pathsByType = ['dir' => [], 'file' => []];
foreach ($paths as $path) {
if (substr($path, -1) === '/') {
$pathsByType['dir'][] = $path === '/' ? DIRECTORY_SEPARATOR : str_replace('/', DIRECTORY_SEPARATOR, rtrim($path, '/'));
} else {
$pathsByType['file'][] = str_replace('/', DIRECTORY_SEPARATOR, $path);
}
}
$finder = Finder::create()
->files()
->ignoreDotFiles(true)
->ignoreVCS(true)
->exclude([])
->in($pathsByType['dir'])
->append($pathsByType['file'])
;
foreach ($options->getFilterByExtensions() as $extension) {
$finder->name('*.' . $extension);
}
foreach ($options->getIgnoredDirectoriesByName() as $notName) {
$finder->notPath('#(^|/)' . preg_quote($notName, '#') . '(/|$)#');
}
return $finder;
}
/**
* @param \Concrete\Core\Support\CodingStyle\PhpFixerOptions $options
* @param int $flags
* @param bool $dryRun
* @param array $rules
*
* @return \PhpCsFixer\Cache\CacheManagerInterface
*/
protected function createCacheManager(PhpFixerOptions $options, $flags, $dryRun, array $rules)
{
if ($options->isCacheDisabled()) {
return new NullCacheManager();
}
return new FileCacheManager(
new FileHandler($this->getCacheFilename($options, $flags)),
new Signature(
PHP_VERSION,
$this->getPhpCSFixerVersion(),
' ',
"\n",
$rules
),
$dryRun,
new Directory($options->getWebRoot())
);
}
/**
* @param \Concrete\Core\Support\CodingStyle\PhpFixerOptions $options
* @param int $flags
*
* @return string
*/
protected function getCacheFilename(PhpFixerOptions $options, $flags)
{
return $options->getWebRoot() . '/.php_cs.cache.' . $flags . '@' . trim(preg_replace('/[^\w\.\@]+/', '_', PHP_VERSION), '_');
}
protected function resetErrors()
{
$this->errorManager = new ErrorsManager();
}
/**
* @return string
*/
protected function getPhpCSFixerVersion()
{
if ($this->phpCSFixerVersion === null) {
$phpCSFixerToolInfo = new ToolInfo();
$this->phpCSFixerVersion = $phpCSFixerToolInfo->getVersion();
}
return $this->phpCSFixerVersion;
}
/**
* @param int $stepIndex
*/
private function expandFinder($stepIndex)
{
if ($this->steps[$stepIndex]['finder'] instanceof Finder) {
$this->steps[$stepIndex]['finder'] = new ArrayObject(iterator_to_array($this->steps[$stepIndex]['finder']->getIterator()));
}
}
}