-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax.php
More file actions
109 lines (82 loc) · 2.3 KB
/
syntax.php
File metadata and controls
109 lines (82 loc) · 2.3 KB
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
<?php
declare(strict_types=1);
/**
* Lightweight recursive PHP syntax check using `php -l`.
*
* Usage:
* php .github/scripts/syntax.php [path ...]
*/
$paths = array_slice($_SERVER['argv'] ?? [], 1);
if ($paths === []) {
fwrite(STDERR, "Error: at least one path is required.\n");
fwrite(STDERR, "Usage: php .github/scripts/syntax.php [path ...]\n");
exit(2);
}
$files = [];
foreach ($paths as $path) {
if (! is_string($path) || $path === '') {
continue;
}
if (is_file($path)) {
if (str_ends_with($path, '.php')) {
$files[] = $path;
}
continue;
}
if (! is_dir($path)) {
continue;
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
);
foreach ($iterator as $entry) {
if (! $entry instanceof SplFileInfo) {
continue;
}
if (! $entry->isFile()) {
continue;
}
$filename = $entry->getFilename();
if (! str_ends_with($filename, '.php')) {
continue;
}
$files[] = $entry->getPathname();
}
}
$files = array_values(array_unique($files));
sort($files);
if ($files === []) {
fwrite(STDOUT, "No PHP files found.\n");
exit(0);
}
$failed = [];
foreach ($files as $file) {
$command = [PHP_BINARY, '-d', 'display_errors=1', '-l', $file];
$descriptorSpec = [
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
];
$process = proc_open($command, $descriptorSpec, $pipes);
if (! is_resource($process)) {
$failed[] = [$file, 'Could not start PHP lint process'];
continue;
}
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$exitCode = proc_close($process);
if ($exitCode !== 0) {
$output = trim((string) $stdout . "\n" . (string) $stderr);
$failed[] = [$file, $output !== '' ? $output : 'Unknown lint failure'];
}
}
if ($failed === []) {
fwrite(STDOUT, sprintf("Syntax OK: %d PHP files checked.\n", count($files)));
exit(0);
}
fwrite(STDERR, sprintf("Syntax errors in %d file(s):\n", count($failed)));
foreach ($failed as [$file, $error]) {
fwrite(STDERR, "- {$file}\n{$error}\n");
}
exit(1);