-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSniffer.php
62 lines (54 loc) · 1.46 KB
/
Sniffer.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
<?php
/**
* PHP Composter PHP Code Sniffer WordPress Coding Standards Action.
*
* @package PHPComposter\PHPComposter_PHPCS_WPCS
* @author Gabor Javorszky <[email protected]>
* @license MIT
* @link https://javorszky.co.uk
* @copyright 2017 Gabor Javorszky
*/
namespace PHPComposter\PHPComposter_PHPCS_WPCS;
use PHP_CodeSniffer_CLI;
use PHPComposter\PHPComposter\BaseAction;
/**
* Class Sniffer.
*
* @since 0.1.0
*
* @package PHPComposter\PHPComposter_PHPCS_WPCS
* @author Gabor Javorszky <[email protected]>
*/
class Sniffer extends BaseAction
{
/**
* Run PHP Code Sniffer over PHP files as pre-commit hook.
*
* @since 0.1.0
*/
public function preCommit()
{
$files = $this->getStagedFiles('/*.php$');
if (empty($files)) {
return;
}
echo 'Running PHP CodeSniffer in ' . $this->root . PHP_EOL;
$sniffer = new PHP_CodeSniffer_CLI();
@$config = $this->getExtraKey('php-composter-phpcs-wpcs', [
'standard' => 'WordPress-Extra'
]);
ob_start();
$numErrors = $sniffer->process([
'standard' => $config['standard'],
'files' => $files
]);
$output = ob_get_clean();
echo $output . PHP_EOL;
if ($numErrors === 0) {
exit(0);
} else {
echo 'PHP Code Sniffer found errors! Aborting Commit.' . PHP_EOL;
exit(1);
}
}
}