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