forked from iangreenleaf/Scisr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeSniffer.php
71 lines (62 loc) · 1.81 KB
/
CodeSniffer.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
63
64
65
66
67
68
69
70
71
<?php
/**
* Handles the job of finding and parsing files.
*/
class Scisr_CodeSniffer extends PHP_CodeSniffer
{
private $_dbFiles;
/**
* If we can cache the results of process()
* @var boolean
*/
private $cacheable = false;
public function __construct(Scisr_Db_Files $dbFiles, $verbosity=0, $tabWidth=0)
{
$this->_dbFiles = $dbFiles;
// PHP_CodeSniffer messes up the cwd, so restore it after we construct
$cwd = getcwd();
parent::__construct($verbosity, $tabWidth);
chdir($cwd);
$this->setAllowedFileExtensions(array('php', 'inc', 'html'));
}
public function __destruct()
{
// no need to reset cwd
}
/**
* Add a listener
* @param PHP_CodeSniffer_Sniff the listener to add. Unlike
* PHP_CodeSniffer's methods, this one takes an instantiated object.
*/
public function addListener(PHP_CodeSniffer_Sniff $listener)
{
$this->listeners[] = $listener;
}
/**
* Clear all registered listeners
*/
public function clearListeners()
{
$this->listeners = array();
}
public function process($files, $local=false, $cacheable=false)
{
$this->_cacheable = $cacheable;
parent::process($files, $local);
}
public function processFile($file, $contents=null)
{
if ($this->_cacheable) {
// If we have cached results and they're not stale, don't bother processing
$cacheTime = $this->_dbFiles->getTimeParsed($file);
$mtime = filemtime($file);
if ($cacheTime !== null && $cacheTime > $mtime) {
return;
}
}
parent::processFile($file, $contents);
if ($this->_cacheable) {
$this->_dbFiles->registerFile($file);
}
}
}