-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathManager.php
293 lines (245 loc) · 9.23 KB
/
Manager.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
namespace JakubOnderka\PhpParallelLint;
use JakubOnderka\PhpParallelLint\Contracts\SyntaxErrorCallback;
use JakubOnderka\PhpParallelLint\Process\GitBlameProcess;
use JakubOnderka\PhpParallelLint\Process\PhpExecutable;
use ReturnTypeWillChange;
class Manager
{
/** @var Output */
protected $output;
/**
* @param null|Settings $settings
* @return Result
* @throws Exception
* @throws \Exception
*/
public function run($settings = null)
{
$settings = ($settings instanceof Settings) ? $settings : new Settings();
$output = $this->output ?: $this->getDefaultOutput($settings);
$phpExecutable = PhpExecutable::getPhpExecutable($settings->phpExecutable);
$olderThanPhp54 = $phpExecutable->getVersionId() < 50400; // From PHP version 5.4 are tokens translated by default
$translateTokens = $phpExecutable->isIsHhvmType() || $olderThanPhp54;
$output->writeHeader($phpExecutable->getVersionId(), $settings->parallelJobs, $phpExecutable->getHhvmVersion());
$files = $this->getFilesFromPaths($settings->paths, $settings->extensions, $settings->excluded);
if (empty($files)) {
throw new Exception('No file found to check.');
}
$output->setTotalFileCount(count($files));
$parallelLint = new ParallelLint($phpExecutable, $settings->parallelJobs);
$parallelLint->setAspTagsEnabled($settings->aspTags);
$parallelLint->setShortTagEnabled($settings->shortTag);
$parallelLint->setShowDeprecated($settings->showDeprecated);
$parallelLint->setSyntaxErrorCallback($this->createSyntaxErrorCallback($settings));
$parallelLint->setProcessCallback(function ($status, $file) use ($output) {
if ($status === ParallelLint::STATUS_OK) {
$output->ok();
} else if ($status === ParallelLint::STATUS_SKIP) {
$output->skip();
} else if ($status === ParallelLint::STATUS_ERROR) {
$output->error();
} else {
$output->fail();
}
});
$result = $parallelLint->lint($files);
if ($settings->blame) {
$this->gitBlame($result, $settings);
}
$output->writeResult($result, new ErrorFormatter($settings->colors, $translateTokens), $settings->ignoreFails);
return $result;
}
/**
* @param Output $output
*/
public function setOutput(Output $output)
{
$this->output = $output;
}
/**
* @param Settings $settings
* @return Output
*/
protected function getDefaultOutput(Settings $settings)
{
$writer = new ConsoleWriter;
switch ($settings->format) {
case Settings::FORMAT_JSON:
return new JsonOutput($writer);
case Settings::FORMAT_GITLAB:
return new GitLabOutput($writer);
case Settings::FORMAT_CHECKSTYLE:
return new CheckstyleOutput($writer);
}
if ($settings->colors === Settings::DISABLED) {
$output = new TextOutput($writer);
} else {
$output = new TextOutputColored($writer, $settings->colors);
}
$output->showProgress = $settings->showProgress;
return $output;
}
/**
* @param Result $result
* @param Settings $settings
* @throws Exception
*/
protected function gitBlame(Result $result, Settings $settings)
{
if (!GitBlameProcess::gitExists($settings->gitExecutable)) {
return;
}
foreach ($result->getErrors() as $error) {
if ($error instanceof SyntaxError) {
$process = new GitBlameProcess($settings->gitExecutable, $error->getFilePath(), $error->getLine());
$process->waitForFinish();
if ($process->isSuccess()) {
$blame = new Blame;
$blame->name = $process->getAuthor();
$blame->email = $process->getAuthorEmail();
$blame->datetime = $process->getAuthorTime();
$blame->commitHash = $process->getCommitHash();
$blame->summary = $process->getSummary();
$error->setBlame($blame);
}
}
}
}
/**
* @param array $paths
* @param array $extensions
* @param array $excluded
* @return array
* @throws NotExistsPathException
*/
protected function getFilesFromPaths(array $paths, array $extensions, array $excluded = array())
{
$extensions = array_map('preg_quote', $extensions, array_fill(0, count($extensions), '`'));
$regex = '`\.(?:' . implode('|', $extensions) . ')$`iD';
$files = array();
foreach ($paths as $path) {
if (is_file($path)) {
$files[] = $path;
} else if (is_dir($path)) {
$iterator = new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS);
if (!empty($excluded)) {
$iterator = new RecursiveDirectoryFilterIterator($iterator, $excluded);
}
$iterator = new \RecursiveIteratorIterator(
$iterator,
\RecursiveIteratorIterator::LEAVES_ONLY,
\RecursiveIteratorIterator::CATCH_GET_CHILD
);
$iterator = new \RegexIterator($iterator, $regex);
/** @var \SplFileInfo[] $iterator */
foreach ($iterator as $directoryFile) {
$files[] = (string) $directoryFile;
}
} else {
throw new NotExistsPathException($path);
}
}
$files = array_unique($files);
return $files;
}
protected function createSyntaxErrorCallback(Settings $settings)
{
if ($settings->syntaxErrorCallbackFile === null) {
return null;
}
$fullFilePath = realpath($settings->syntaxErrorCallbackFile);
if ($fullFilePath === false) {
throw new NotExistsPathException($settings->syntaxErrorCallbackFile);
}
require_once $fullFilePath;
$expectedClassName = basename($fullFilePath, '.php');
if (!class_exists($expectedClassName)) {
throw new NotExistsClassException($expectedClassName, $settings->syntaxErrorCallbackFile);
}
$callbackInstance = new $expectedClassName;
if (!($callbackInstance instanceof SyntaxErrorCallback)) {
throw new NotImplementCallbackException($expectedClassName);
}
return $callbackInstance;
}
}
class RecursiveDirectoryFilterIterator extends \RecursiveFilterIterator
{
/** @var \RecursiveDirectoryIterator */
private $iterator;
/** @var array */
private $excluded = array();
/**
* @param \RecursiveDirectoryIterator $iterator
* @param array $excluded
*/
public function __construct(\RecursiveDirectoryIterator $iterator, array $excluded)
{
parent::__construct($iterator);
$this->iterator = $iterator;
$this->excluded = array_map(array($this, 'getPathname'), $excluded);
}
/**
* (PHP 5 >= 5.1.0)<br/>
* Check whether the current element of the iterator is acceptable
*
* @link http://php.net/manual/en/filteriterator.accept.php
* @return bool true if the current element is acceptable, otherwise false.
*/
#[ReturnTypeWillChange]
public function accept()
{
$current = $this->current()->getPathname();
$current = $this->normalizeDirectorySeparator($current);
if ('.' . DIRECTORY_SEPARATOR !== $current[0] . $current[1]) {
$current = '.' . DIRECTORY_SEPARATOR . $current;
}
return !in_array($current, $this->excluded);
}
/**
* (PHP 5 >= 5.1.0)<br/>
* Check whether the inner iterator's current element has children
*
* @link http://php.net/manual/en/recursivefilteriterator.haschildren.php
* @return bool true if the inner iterator has children, otherwise false
*/
#[ReturnTypeWillChange]
public function hasChildren()
{
return $this->iterator->hasChildren();
}
/**
* (PHP 5 >= 5.1.0)<br/>
* Return the inner iterator's children contained in a RecursiveFilterIterator
*
* @link http://php.net/manual/en/recursivefilteriterator.getchildren.php
* @return \RecursiveFilterIterator containing the inner iterator's children.
*/
#[ReturnTypeWillChange]
public function getChildren()
{
return new self($this->iterator->getChildren(), $this->excluded);
}
/**
* @param string $file
* @return string
*/
private function getPathname($file)
{
$file = $this->normalizeDirectorySeparator($file);
if ('.' . DIRECTORY_SEPARATOR !== $file[0] . $file[1]) {
$file = '.' . DIRECTORY_SEPARATOR . $file;
}
$directoryFile = new \SplFileInfo($file);
return $directoryFile->getPathname();
}
/**
* @param string $file
* @return string
*/
private function normalizeDirectorySeparator($file)
{
return str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $file);
}
}