generated from shgysk8zer0/blank-repo
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linter.php
82 lines (65 loc) · 1.69 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
<?php
namespace shgysk8zer0\HTTP;
use \FilesystemIterator as FS;
use \InvalidArgumentException;
final class Linter
{
private $_scan_exts = ['php', 'phtml'];
private $_ignored_dirs = [];
final public function ignoreDirs(string ...$dirs): void
{
$dirs = array_filter($dirs, 'is_dir');
$this->_ignored_dirs = array_map(function(string $path): string
{
return realpath($path);
}, $dirs);
}
final public function scanExts(string ...$exts): void
{
$this->_scan_exts = array_map(function(string $ext): string
{
return strtolower($ext);
}, $exts);
}
final public function isAllowedDir(FS $path): bool
{
return $path->isDir() and ! in_array($path->getPathname(), $this->_ignored_dirs);
}
final public function isLintable(FS $path): bool
{
return $path->isFile() and in_array($path->getExtension(), $this->_scan_exts);
}
final public function scan(string $path): bool
{
if (is_dir($path)) {
$dir = new FS($path, FS::KEY_AS_PATHNAME | FS::CURRENT_AS_SELF | FS::SKIP_DOTS);
$valid = true;
foreach ($dir as $path => $fs) {
if ($this->isAllowedDir($fs)) {
static::scan($path);
} elseif ($this->isLintable($fs)) {
if (! $this->lintFile($fs)) {
$valid = false;
}
}
}
return $valid;
} else {
throw new InvalidArgumentException(sprintf('%s is not a directory', $path));
}
}
final public function lintFile(FS $path): bool
{
$valid = true;
if ($this->isLintable($path)) {
$msg = '';
if (! php_check_syntax($path->getPathname(), $msg)) {
$valid = false;
echo $msg . PHP_EOL;
}
return $valid;
} else {
throw new InvalidArgumentException(sprintf('Cannot lint path: %s', $path->getPathne()));
}
}
}