-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathphpcs-pr
More file actions
executable file
·87 lines (73 loc) · 2.47 KB
/
phpcs-pr
File metadata and controls
executable file
·87 lines (73 loc) · 2.47 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
#!/usr/bin/env php
<?php
/**
* Run PHPCS tests against a PR.
*
* The only argument is for the PR's base branch, which is then compared to the HEAD of the PR to retrieve the list
* of changed files. The PHPCS tests are only run against these changed files, to speed up the tests.
*/
if (empty($argv[1])) {
fwrite(STDERR, 'You must provide a base branch to check this PR against.');
fwrite(STDERR, "\n");
exit(1);
}
// Get a changelist of files from Git for this PR.
$fileList = shell_exec('git diff --name-only --diff-filter=ACMR origin/' . $argv[1] . ' HEAD');
$files = array_filter(explode("\n", $fileList));
foreach ($files as &$file) {
if (strpos($file, ' ') !== false) {
$file = str_replace(' ', '\\ ', $file);
}
}
// no changes found in diff, early exit
if (!count($files)) {
fwrite(STDOUT, "\e[0;32mFound no changed files.\e[0m");
fwrite(STDOUT, "\n");
exit(0);
}
// Run all changed files through the PHPCS code sniffer and generate a CSV report
$csv = shell_exec('./vendor/bin/phpcs --colors -nq --report="csv" --extensions="php" ' . implode(' ', $files));
$lines = array_map(function ($row) {
return array_map(function ($column) {
return trim($column, '"');
}, explode(',', $row));
}, array_filter(explode("\n", $csv)));
// Remove header row
array_shift($lines);
if (!count($lines)) {
fwrite(STDOUT, "\e[0;32mFound no issues with code quality.\e[0m");
fwrite(STDOUT, "\n");
exit(0);
}
// Group errors by file
$files = [];
foreach ($lines as $line) {
$filename = str_replace(dirname(dirname(dirname(__DIR__))), '', $line[0]);
if (empty($files[$filename])) {
$files[$filename] = [];
}
$files[$filename][] = [
'warning' => (($line[3] ?? 'err') === 'warning'),
'message' => $line[4] ?? 'unknown',
'line' => $line[1] ?? '0',
];
}
// Render report
fwrite(STDERR, "\e[0;31mFound "
. ((count($lines) === 1)
? '1 issue'
: count($lines) . ' issues')
. " with code quality.\e[0m");
fwrite(STDERR, "\n");
foreach ($files as $file => $errors) {
fwrite(STDERR, "\n");
fwrite(STDERR, "\e[1;37m" . str_replace('"', '', $file) . "\e[0m");
fwrite(STDERR, "\n\n");
foreach ($errors as $error) {
fwrite(STDERR, "\e[2m" . str_pad(' L' . $error['line'], 7) . " | \e[0m");
fwrite(STDERR, $error['warning'] ? "\e[1;33mWARN:\e[0m " : "\e[0;31mERR:\e[0m ");
fwrite(STDERR, $error['message']);
fwrite(STDERR, "\n");
}
}
exit(1);