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