-
Notifications
You must be signed in to change notification settings - Fork 1
/
phpcs-pre-commit.php
40 lines (31 loc) · 1007 Bytes
/
phpcs-pre-commit.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
<?php
$exit = 0;
$runningPhar = Phar::running(false);
if (empty($runningPhar)) {
$phpcode = sprintf('include "%s";', __DIR__ . '/phpcs.php');
} else {
$phpcode = sprintf('Phar::loadPhar("%s");', $runningPhar);
$phpcode .= sprintf('include "phar://%s/scripts/phpcs.php";', $runningPhar);
}
$output = array();
exec('git diff --cached --name-status --diff-filter=ACM', $output);
foreach ($output as $file) {
if ('D' === substr($file, 0, 1)) {
// Deleted file; do nothing.
continue;
}
$fileName = trim(substr($file, 1));
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
if (!preg_match('/^ph(p|tml)$/', $extension)) {
continue;
}
$command = sprintf('php -r %s -- -n %s', escapeshellarg($phpcode), escapeshellarg($fileName));
$output = array();
$return = null;
exec($command, $output, $return);
if ($return != 0 || !empty($output)) {
echo implode("\n", $output) . "\n";
$exit = 1;
}
}
exit($exit);