/
PhpFixer.php
266 lines (245 loc) · 9.01 KB
/
PhpFixer.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
<?php
namespace Concrete\Core\Support\CodingStyle;
use DirectoryIterator;
use PhpCsFixer\Console\Output\ProcessOutput;
use PhpCsFixer\FixerFileProcessedEvent;
use RuntimeException;
use SplFileInfo;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class PhpFixer
{
/**
* Coding style rules flag: compatible with PHP versions prior to 5.5.9 (the minimum PHP version required by concrete5).
*
* @var int
*/
const FLAG_OLDPHP = 0x01;
/**
* Coding style rules flag: PHP-only files.
*
* @var int
*/
const FLAG_PHPONLY = 0x02;
/**
* Coding style rules flag: bootstrap files.
*
* @var int
*/
const FLAG_BOOTSTRAP = 0x03; // FLAG_OLDPHP | FLAG_PHPONLY
/**
* Coding style rules flag: files that implement classes following the PSR-4 rules.
*
* @var int
*/
const FLAG_PSR4CLASS = 0x06; // Includes FLAG_PHPONLY
/**
* @var \Concrete\Core\Support\CodingStyle\PhpFixerOptions
*/
protected $options;
/**
* @var \Concrete\Core\Support\CodingStyle\PhpFixerRuleResolver
*/
protected $ruleResolver;
/**
* @var \Concrete\Core\Support\CodingStyle\PhpFixerRunner
*/
protected $runner;
/**
* Initialize the instance.
*
* @param \Concrete\Core\Support\CodingStyle\PhpFixerOptions $options
* @param \Concrete\Core\Support\CodingStyle\PhpFixerRuleResolver $ruleResolver
* @param \Concrete\Core\Support\CodingStyle\PhpFixerRunner $runner
*/
public function __construct(PhpFixerOptions $options, PhpFixerRuleResolver $ruleResolver, PhpFixerRunner $runner)
{
$this->options = $options;
$this->ruleResolver = $ruleResolver;
$this->runner = $runner;
}
/**
* Get the fixer options.
*
* @return \Concrete\Core\Support\CodingStyle\PhpFixerOptions
*/
public function getOptions()
{
return $this->options;
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \SplFileInfo[] $paths with absolute paths
* @param bool $dryRun
*
* @return array
*/
public function fix(InputInterface $input, OutputInterface $output, array $paths, $dryRun = false)
{
$pathRuleList = [];
foreach ($paths as $path) {
$pathRuleList = $this->mergePathAndFlags($pathRuleList, $this->splitPathToPathAndFlags($path));
}
$this->runner->resetSteps();
foreach ($pathRuleList as $flags => $paths) {
$this->runner->addStep($this->options, $paths, $flags);
}
$progressOutput = new ProcessOutput($output, $this->runner->getEventDispatcher(), null, $this->runner->calculateNumberOfFiles());
$counters = [];
$counter = function (FixerFileProcessedEvent $e) use (&$counters) {
$status = $e->getStatus();
if (isset($counters[$status])) {
++$counters[$status];
} else {
$counters[$status] = 1;
}
};
$this->runner->getEventDispatcher()->addListener(FixerFileProcessedEvent::NAME, $counter);
try {
$progressOutput->printLegend();
$changes = $this->runner->apply($dryRun);
$output->writeln('');
} finally {
$this->runner->getEventDispatcher()->removeListener(FixerFileProcessedEvent::NAME, $counter);
}
return [$counters, $changes, clone $this->runner->getErrorManager()];
}
/**
* @param \SplFileInfo $path
*
* @throws \RuntimeException if $path could not be found
*
* @return array
*/
protected function splitPathToPathAndFlags(SplFileInfo $path)
{
if ($path->isFile()) {
return $this->splitFileToPathAndFlags($path);
}
if ($path->isDir()) {
return $this->splitDirectoryToPathAndFlags($path);
}
throw new RuntimeException(t('Failed to find the file/directory %s', $path->getPathname()));
}
/**
* @param \SplFileInfo $file
*
* @return array
*/
protected function splitFileToPathAndFlags(SplFileInfo $file)
{
if (strpos($file->getFilename(), '.') === 0) {
// Exclude dot files
return [];
}
$isExtensionOk = in_array(mb_strtolower($file->getExtension()), $this->options->getFilterByExtensions());
$fullPath = $this->options->normalizePath($file->getPathname(), false, false);
if (strpos($fullPath, $this->options->getWebRoot()) !== 0) {
// Outside the webroot: we don't know the exact rules.
return $isExtensionOk ? [0 => [$fullPath]] : [];
}
$relativePath = substr($fullPath, strlen($this->options->getWebRoot()));
if ($isExtensionOk === false && !in_array($relativePath, $this->options->getFilterIncludeFiles())) {
// Let's skip this file, since it doesn't have the allowed file extensions and it's not in the allowlist
return [];
}
foreach ($this->options->getIgnoredDirectoriesByPath() as $check) {
if (strpos($relativePath, $check) === 0) {
// The file is in a directory marked as to be ignored: no operation should be done
return [];
}
}
if (in_array($relativePath, $this->options->getBootstrapFiles(), true)) {
// The file is a bootstrap file
return [self::FLAG_BOOTSTRAP => [$fullPath]];
}
foreach ($this->options->getPhpOnlyNonPsr4Regexes() as $pattern) {
if (preg_match($pattern, $relativePath)) {
// The file is a PHP-only file that don't follow PSR-4 class name rules.
return [self::FLAG_PHPONLY => [$fullPath]];
}
}
foreach ($this->options->getPhpOnlyPsr4Regexes() as $pattern) {
if (preg_match($pattern, $relativePath)) {
// The file is a PHP-only file that follow PSR-4 class name rules.
return [self::FLAG_PSR4CLASS => [$fullPath]];
}
}
// No specific rules found.
return [0 => [$fullPath]];
}
/**
* @param \SplFileInfo $directory
*
* @return array
*/
protected function splitDirectoryToPathAndFlags(SplFileInfo $directory)
{
if (strpos($directory->getFilename(), '.') === 0) {
// Exclude dot directories
return [];
}
if (in_array($directory->getFilename(), $this->options->getIgnoredDirectoriesByName(), true)) {
// Explicitly excluded directories
return [];
}
$fullPath = $this->options->normalizePath($directory->getPathname(), true, false);
if (strpos($fullPath, $this->options->getWebRoot()) !== 0) {
// Outside the webroot: we don't know the exact rules.
return [0 => [$fullPath]];
}
$relativePath = substr($fullPath, strlen($this->options->getWebRoot()));
if ($relativePath === '') {
$relativePath = '/';
}
if ($this->options->isDirectoryWithMixedContents($relativePath)) {
// The directory contains files with mixed rules: we have to parse the directory contents.
$result = [];
$iterator = new DirectoryIterator($fullPath);
foreach ($iterator as $child) {
if (!$child->isDot()) {
$result = $this->mergePathAndFlags($result, $this->splitPathToPathAndFlags($child));
}
}
return $result;
}
foreach ($this->options->getIgnoredDirectoriesByPath() as $check) {
if (strpos($relativePath, $check) === 0) {
// The directory is (inside) a directory marked as to be ignored: no operation should be done
return [];
}
}
foreach ($this->options->getPhpOnlyNonPsr4Regexes() as $pattern) {
if (preg_match($pattern, $relativePath)) {
// The directory contains PHP-only files that don't PSR-4 class name rules.
return [self::FLAG_PHPONLY => [$fullPath]];
}
}
foreach ($this->options->getPhpOnlyPsr4Regexes() as $pattern) {
if (preg_match($pattern, $relativePath)) {
// The directory contains PHP-only files that follow PSR-4 class name rules.
return [self::FLAG_PSR4CLASS => [$fullPath]];
}
}
// No specific rules found.
return [0 => [$fullPath]];
}
/**
* @param array $list1
* @param array $list2
*
* @return array
*/
protected function mergePathAndFlags($list1, $list2)
{
foreach ($list2 as $key => $paths) {
if (isset($list1[$key])) {
$list1[$key] = array_values(array_merge($list1[$key], $paths));
} else {
$list1[$key] = $paths;
}
}
return $list1;
}
}