-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathFileList.php
More file actions
248 lines (198 loc) · 5.97 KB
/
FileList.php
File metadata and controls
248 lines (198 loc) · 5.97 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* Represents a list of files on the file system that are to be checked during the run.
*
* File objects are created as needed rather than all at once.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Files;
use PHP_CodeSniffer\Autoload;
use PHP_CodeSniffer\Util;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Exceptions\DeepExitException;
class FileList implements \Iterator, \Countable
{
/**
* A list of file paths that are included in the list.
*
* @var array
*/
private $files = [];
/**
* The number of files in the list.
*
* @var integer
*/
private $numFiles = 0;
/**
* The config data for the run.
*
* @var \PHP_CodeSniffer\Config
*/
public $config = null;
/**
* The ruleset used for the run.
*
* @var \PHP_CodeSniffer\Ruleset
*/
public $ruleset = null;
/**
* An array of patterns to use for skipping files.
*
* @var array
*/
protected $ignorePatterns = [];
/**
* Constructs a file list and loads in an array of file paths to process.
*
* @param \PHP_CodeSniffer\Config $config The config data for the run.
* @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run.
*
* @return void
*/
public function __construct(Config $config, Ruleset $ruleset)
{
$this->ruleset = $ruleset;
$this->config = $config;
$paths = $config->files;
foreach ($paths as $path) {
$isPharFile = Util\Common::isPharFile($path);
if (is_dir($path) === true || $isPharFile === true) {
if ($isPharFile === true) {
$path = 'phar://'.$path;
}
$filterClass = $this->getFilterClass();
$di = new \RecursiveDirectoryIterator($path, (\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS));
$filter = new $filterClass($di, $path, $config, $ruleset);
$iterator = new \RecursiveIteratorIterator($filter);
foreach ($iterator as $file) {
$this->files[$file->getPathname()] = null;
$this->numFiles++;
}
} else {
$this->addFile($path);
}//end if
}//end foreach
reset($this->files);
}//end __construct()
/**
* Add a file to the list.
*
* If a file object has already been created, it can be passed here.
* If it is left NULL, it will be created when accessed.
*
* @param string $path The path to the file being added.
* @param \PHP_CodeSniffer\Files\File $file The file being added.
*
* @return void
*/
public function addFile($path, $file=null)
{
// No filtering is done for STDIN when the filename
// has not been specified.
if ($path === 'STDIN') {
$this->files[$path] = $file;
$this->numFiles++;
return;
}
$filterClass = $this->getFilterClass();
$di = new \RecursiveArrayIterator([$path]);
$filter = new $filterClass($di, $path, $this->config, $this->ruleset);
$iterator = new \RecursiveIteratorIterator($filter);
foreach ($iterator as $path) {
$this->files[$path] = $file;
$this->numFiles++;
}
}//end addFile()
/**
* Get the class name of the filter being used for the run.
*
* @return string
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the specified filter could not be found.
*/
private function getFilterClass()
{
$filterType = $this->config->filter;
if ($filterType === null) {
$filterClass = '\PHP_CodeSniffer\Filters\Filter';
} else {
if (strpos($filterType, '.') !== false) {
// This is a path to a custom filter class.
$filename = realpath($filterType);
if ($filename === false) {
$error = "ERROR: Custom filter \"$filterType\" not found".PHP_EOL;
throw new DeepExitException($error, 3);
}
$filterClass = Autoload::loadFile($filename);
} else {
$filterClass = '\PHP_CodeSniffer\Filters\\'.$filterType;
}
}
return $filterClass;
}//end getFilterClass()
/**
* Rewind the iterator to the first file.
*
* @return void
*/
public function rewind()
{
reset($this->files);
}//end rewind()
/**
* Get the file that is currently being processed.
*
* @return \PHP_CodeSniffer\Files\File
*/
public function current()
{
$path = key($this->files);
if (isset($this->files[$path]) === false) {
$this->files[$path] = new LocalFile($path, $this->ruleset, $this->config);
}
return $this->files[$path];
}//end current()
/**
* Return the file path of the current file being processed.
*
* @return void
*/
public function key()
{
return key($this->files);
}//end key()
/**
* Move forward to the next file.
*
* @return void
*/
public function next()
{
next($this->files);
}//end next()
/**
* Checks if current position is valid.
*
* @return bool
*/
public function valid()
{
if (current($this->files) === false) {
return false;
}
return true;
}//end valid()
/**
* Return the number of files in the list.
*
* @return int
*/
public function count()
{
return $this->numFiles;
}//end count()
}//end class