comparison vendor/squizlabs/php_codesniffer/src/Files/FileList.php @ 17:129ea1e6d783

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