Skip to content

Commit 074f24a

Browse files
committed
Adding actual sniff file
1 parent f6e8eb6 commit 074f24a

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/Sniffer.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* PHP Composter PHP Code Sniffer WordPress Coding Standards Action.
4+
*
5+
* @package PHPComposter\PHPComposter_PHPCS_WPCS
6+
* @author Gabor Javorszky <[email protected]>
7+
* @license MIT
8+
* @link https://javorszky.co.uk
9+
* @copyright 2017 Gabor Javorszky
10+
*/
11+
12+
namespace PHPComposter\PHPComposter_PHPCS_WPCS;
13+
14+
use PHP_CodeSniffer_CLI;
15+
use PHPComposter\PHPComposter\BaseAction;
16+
17+
/**
18+
* Class Sniffer.
19+
*
20+
* @since 0.1.0
21+
*
22+
* @package PHPComposter\PHPComposter_PHPCS_WPCS
23+
* @author Gabor Javorszky <[email protected]>
24+
*/
25+
class Sniffer extends BaseAction
26+
{
27+
28+
/**
29+
* Run PHP Code Sniffer over PHP files as pre-commit hook.
30+
*
31+
* @since 0.1.0
32+
*/
33+
public function preCommit()
34+
{
35+
$files = $this->getStagedFiles('/*.php$');
36+
if (empty($files)) {
37+
return;
38+
}
39+
40+
echo 'Running PHP CodeSniffer in ' . $this->root . PHP_EOL;
41+
$sniffer = new PHP_CodeSniffer_CLI();
42+
43+
ob_start();
44+
$numErrors = $sniffer->process(array('standard' => 'WordPress-Extra', 'files' => $files));
45+
$output = ob_get_clean();
46+
47+
echo $output . PHP_EOL;
48+
49+
if ($numErrors === 0) {
50+
exit(0);
51+
} else {
52+
echo 'PHP Code Sniffer found errors! Aborting Commit.' . PHP_EOL;
53+
exit(1);
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)