-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Linter.php
173 lines (143 loc) · 5.22 KB
/
Linter.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
<?php
namespace Overtrue\PHPLint;
use InvalidArgumentException;
use Overtrue\PHPLint\Process\Lint;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class Linter
{
private mixed $processCallback;
private array $files = [];
private array $cache = [];
private array $paths;
private array $excludes;
private array $extensions;
private int $processLimit = 5;
private bool $warning;
public function __construct(array|string $paths, array $excludes = [], array $extensions = ['php'], $warning = false)
{
$this->paths = (array)$paths;
$this->excludes = $excludes;
$this->warning = $warning;
$this->extensions = \array_map(function ($extension) {
return \sprintf('*.%s', \ltrim($extension, '.'));
}, $extensions);
}
public function lint(array $files = [], bool $cache = true): array
{
if (empty($files)) {
$files = $this->getFiles();
}
$processCallback = is_callable($this->processCallback) ? $this->processCallback : function () {
};
$errors = [];
$running = [];
$newCache = [];
while (!empty($files) || !empty($running)) {
for ($i = count($running); !empty($files) && $i < $this->processLimit; ++$i) {
$file = array_shift($files);
$filename = $file->getRealPath();
$relativePathname = $file->getRelativePathname();
if (!isset($this->cache[$relativePathname]) || $this->cache[$relativePathname] !== md5_file($filename)) {
$lint = $this->createLintProcess($filename);
$running[$filename] = [
'process' => $lint,
'file' => $file,
'relativePath' => $relativePathname,
];
$lint->start();
} else {
$newCache[$relativePathname] = $this->cache[$relativePathname];
}
}
foreach ($running as $filename => $item) {
/** @var Lint $lint */
$lint = $item['process'];
if ($lint->isRunning()) {
continue;
}
unset($running[$filename]);
if ($lint->hasSyntaxError()) {
$processCallback('error', $item['file']);
$errors[$filename] = array_merge(['file' => $filename, 'file_name' => $item['relativePath']], $lint->getSyntaxError());
} elseif ($this->warning && $lint->hasSyntaxIssue()) {
$processCallback('warning', $item['file']);
$errors[$filename] = array_merge(['file' => $filename, 'file_name' => $item['relativePath']], $lint->getSyntaxIssue());
} else {
$newCache[$item['relativePath']] = md5_file($filename);
$processCallback('ok', $item['file']);
}
}
}
$cache && Cache::put($newCache);
return $errors;
}
public function setCache(array $cache = [])
{
if (is_array($cache)) {
$this->cache = $cache;
} else {
$this->cache = [];
}
}
public function getFiles(): array
{
if (empty($this->files)) {
foreach ($this->paths as $path) {
if (is_dir($path)) {
$this->files = array_merge($this->files, $this->getFilesFromDir($path));
} elseif (is_file($path)) {
$this->files[$path] = new SplFileInfo($path, $path, $path);
}
}
}
return $this->files;
}
protected function getFilesFromDir(string $dir): array
{
$finder = new Finder();
$finder->files()
->ignoreUnreadableDirs()
->ignoreVCS(true)
->filter(function (SplFileInfo $file) {
return $file->isReadable();
})
->in(realpath($dir));
array_map([$finder, 'name'], $this->extensions);
array_map([$finder, 'notPath'], $this->excludes);
return iterator_to_array($finder);
}
public function setFiles(array $files): static
{
foreach ($files as $file) {
if (is_file($file)) {
$file = new SplFileInfo($file, $file, $file);
}
if (!($file instanceof SplFileInfo)) {
throw new InvalidArgumentException("File $file not exists.");
}
$this->files[$file->getRealPath()] = $file;
}
return $this;
}
public function setProcessCallback(callable $processCallback): static
{
$this->processCallback = $processCallback;
return $this;
}
public function setProcessLimit(int $processLimit): static
{
$this->processLimit = $processLimit;
return $this;
}
protected function createLintProcess(string $filename): Lint
{
$command = [
PHP_SAPI == 'cli' ? PHP_BINARY : PHP_BINDIR . '/php',
'-d error_reporting=E_ALL',
'-d display_errors=On',
'-l', $filename,
];
return new Lint($command);
}
}