1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Represents a list of files on the file system that are to be checked during the run. |
4
|
|
|
* |
5
|
|
|
* File objects are created as needed rather than all at once. |
6
|
|
|
* |
7
|
|
|
* @author Greg Sherwood <[email protected]> |
8
|
|
|
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
9
|
|
|
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PHP_CodeSniffer\Files; |
13
|
|
|
|
14
|
|
|
use PHP_CodeSniffer\Util; |
15
|
|
|
use PHP_CodeSniffer\Ruleset; |
16
|
|
|
use PHP_CodeSniffer\Config; |
17
|
|
|
|
18
|
|
|
class FileList implements \Iterator, \Countable |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A list of file paths that are included in the list. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $files = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The number of files in the list. |
30
|
|
|
* |
31
|
|
|
* @var integer |
32
|
|
|
*/ |
33
|
|
|
private $numFiles = 0; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The config data for the run. |
37
|
|
|
* |
38
|
|
|
* @var \PHP_CodeSniffer\Config |
39
|
|
|
*/ |
40
|
|
|
public $config = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The ruleset used for the run. |
44
|
|
|
* |
45
|
|
|
* @var \PHP_CodeSniffer\Ruleset |
46
|
|
|
*/ |
47
|
|
|
public $ruleset = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* An array of patterns to use for skipping files. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $ignorePatterns = array(); |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Constructs a file list and loads in an array of file paths to process. |
59
|
|
|
* |
60
|
|
|
* @param \PHP_CodeSniffer\Config $config The config data for the run. |
61
|
|
|
* @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run. |
62
|
|
|
* |
63
|
|
|
* @return void |
|
|
|
|
64
|
|
|
*/ |
65
|
|
|
public function __construct(Config $config, Ruleset $ruleset) |
66
|
|
|
{ |
67
|
|
|
$this->ruleset = $ruleset; |
68
|
|
|
$this->config = $config; |
69
|
|
|
|
70
|
|
|
$paths = $config->files; |
|
|
|
|
71
|
|
|
foreach ($paths as $path) { |
72
|
|
|
$this->addFile($path); |
73
|
|
|
}//end foreach |
74
|
|
|
|
75
|
|
|
reset($this->files); |
76
|
|
|
$this->numFiles = count($this->files); |
77
|
|
|
|
78
|
|
|
}//end __construct() |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add a file to the list. |
83
|
|
|
* |
84
|
|
|
* If a file object has already been created, it can be passed here. |
85
|
|
|
* If it is left NULL, it will be created when accessed. |
86
|
|
|
* |
87
|
|
|
* @param string $path The path to the file being added. |
88
|
|
|
* @param \PHP_CodeSniffer\Files\File $file The file being added. |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function addFile($path, $file=null) |
93
|
|
|
{ |
94
|
|
|
// No filtering is done for STDIN. |
95
|
|
|
if ($path === 'STDIN' |
96
|
|
|
|| ($file !== null |
97
|
|
|
&& get_class($file) === 'PHP_CodeSniffer\Files\DummyFile') |
98
|
|
|
) { |
99
|
|
|
$this->files[$path] = $file; |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$filterClass = $this->getFilterClass(); |
104
|
|
|
|
105
|
|
|
$di = new \RecursiveArrayIterator(array($path)); |
106
|
|
|
$filter = new $filterClass($di, $path, $this->config, $this->ruleset); |
107
|
|
|
$iterator = new \RecursiveIteratorIterator($filter); |
108
|
|
|
|
109
|
|
|
foreach ($iterator as $path) { |
110
|
|
|
$this->files[$path] = $file; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
}//end addFile() |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the class name of the filter being used for the run. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
private function getFilterClass() |
122
|
|
|
{ |
123
|
|
|
$filterType = $this->config->filter; |
|
|
|
|
124
|
|
|
|
125
|
|
|
if ($filterType === null) { |
126
|
|
|
$filterClass = '\PHP_CodeSniffer\Filters\Filter'; |
127
|
|
View Code Duplication |
} else { |
|
|
|
|
128
|
|
|
if (strpos($filterType, '.') !== false) { |
129
|
|
|
// This is a path to a custom filter class. |
130
|
|
|
$filename = realpath($filterType); |
131
|
|
|
if ($filename === false) { |
132
|
|
|
echo "ERROR: Custom filter \"$filterType\" not found".PHP_EOL; |
133
|
|
|
exit(2); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$filterClass = \PHP_CodeSniffer\Autoload::loadFile($filename); |
137
|
|
|
} else { |
138
|
|
|
$filterClass = '\PHP_CodeSniffer\Filters\\'.$filterType; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $filterClass; |
143
|
|
|
|
144
|
|
|
}//end getFilterClass() |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Rewind the iterator to the first file. |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
function rewind() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
reset($this->files); |
155
|
|
|
|
156
|
|
|
}//end rewind() |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the file that is currently being processed. |
161
|
|
|
* |
162
|
|
|
* @return \PHP_CodeSniffer\Files\File |
163
|
|
|
*/ |
164
|
|
|
function current() |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
$path = key($this->files); |
167
|
|
|
if ($this->files[$path] === null) { |
168
|
|
|
$this->files[$path] = new LocalFile($path, $this->ruleset, $this->config); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->files[$path]; |
172
|
|
|
|
173
|
|
|
}//end current() |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Return the file path of the current file being processed. |
178
|
|
|
* |
179
|
|
|
* @return void |
180
|
|
|
*/ |
181
|
|
|
function key() |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
return key($this->files); |
184
|
|
|
|
185
|
|
|
}//end key() |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Move forward to the next file. |
190
|
|
|
* |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
|
function next() |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
next($this->files); |
196
|
|
|
|
197
|
|
|
}//end next() |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Checks if current position is valid. |
202
|
|
|
* |
203
|
|
|
* @return boolean |
204
|
|
|
*/ |
205
|
|
|
function valid() |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
if (current($this->files) === false) { |
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return true; |
212
|
|
|
|
213
|
|
|
}//end valid() |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Return the number of files in the list. |
218
|
|
|
* |
219
|
|
|
* @return integer |
220
|
|
|
*/ |
221
|
|
|
function count() |
|
|
|
|
222
|
|
|
{ |
223
|
|
|
return $this->numFiles; |
224
|
|
|
|
225
|
|
|
}//end count() |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
}//end class |
229
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.