These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file is part of Affinity4\File. |
||
4 | * |
||
5 | * (c) 2017 Luke Watts <[email protected]> |
||
6 | * |
||
7 | * This software is licensed under the MIT license. For the |
||
8 | * full copyright and license information, please view the |
||
9 | * LICENSE file that was distributed with this source code. |
||
10 | */ |
||
11 | namespace Affinity4\File; |
||
12 | |||
13 | /** |
||
14 | * File Class |
||
15 | * |
||
16 | * @author Luke Watts <[email protected]> |
||
17 | * |
||
18 | * @since 1.0.0 |
||
19 | * |
||
20 | * @package Affinity4\File |
||
21 | */ |
||
22 | class File |
||
23 | { |
||
24 | /** |
||
25 | * @author Luke Watts <[email protected]> |
||
26 | * |
||
27 | * @since 1.0.0 |
||
28 | * |
||
29 | * @var \DirectoryIterator |
||
30 | */ |
||
31 | private $iterator; |
||
32 | |||
33 | /** |
||
34 | * @author Luke Watts <[email protected]> |
||
35 | * |
||
36 | * @since 1.0.0 |
||
37 | * |
||
38 | * @var |
||
39 | */ |
||
40 | private $pattern; |
||
41 | |||
42 | /** |
||
43 | * @author Luke Watts <[email protected]> |
||
44 | * |
||
45 | * @since 1.0.0 |
||
46 | * |
||
47 | * @var |
||
48 | */ |
||
49 | private $limit = -1; |
||
50 | |||
51 | /** |
||
52 | * @author Luke Watts <[email protected]> |
||
53 | * |
||
54 | * @since 2.0.0 |
||
55 | * |
||
56 | * @var |
||
57 | */ |
||
58 | private $dir; |
||
59 | |||
60 | /** |
||
61 | * @author Luke Watts <[email protected]> |
||
62 | * |
||
63 | * @since 1.0.0 |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | private $regex_delimiters = ['/', '#', '@', '~']; |
||
68 | |||
69 | /** |
||
70 | * @author Luke Watts <[email protected]> |
||
71 | * |
||
72 | * @since 1.0.0 |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | private $file_list = []; |
||
77 | |||
78 | /** |
||
79 | * Set the pattern to search for. |
||
80 | * |
||
81 | * Can be a regex pattern with the delimiters /, #, @ or ~ |
||
82 | * |
||
83 | * Can also be a plain file name to search for only that file |
||
84 | * |
||
85 | * @author Luke Watts <[email protected]> |
||
86 | * |
||
87 | * @since 1.0.0 |
||
88 | * |
||
89 | * @param $pattern |
||
90 | * |
||
91 | * @return File |
||
92 | */ |
||
93 | 11 | public function find($pattern) |
|
94 | { |
||
95 | 11 | $this->pattern = $pattern; |
|
96 | |||
97 | 11 | return $this; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Alias of the upOne() method |
||
102 | * |
||
103 | * @author Luke Watts <[email protected]> |
||
104 | * |
||
105 | * @since 2.0.0 |
||
106 | * |
||
107 | * @return File |
||
108 | */ |
||
109 | 1 | View Code Duplication | public function parent() |
110 | { |
||
111 | 1 | $pattern = $this->getPattern(); |
|
112 | 1 | $dir = $this->getDir(); |
|
113 | |||
114 | 1 | if ($this->find($pattern)->in($dir)->has() === false) { |
|
115 | $dir = dirname($dir); |
||
116 | $this->file_list = $this->find($pattern)->in($dir)->get(); |
||
0 ignored issues
–
show
|
|||
117 | } else { |
||
118 | 1 | $this->file_list = $this->find($pattern)->in($dir)->get(); |
|
119 | } |
||
120 | |||
121 | 1 | return $this; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Alias of the up() method |
||
126 | * |
||
127 | * @author Luke Watts <[email protected]> |
||
128 | * |
||
129 | * @since 2.0.0 |
||
130 | * |
||
131 | * @return File |
||
132 | */ |
||
133 | 1 | View Code Duplication | public function parents() |
134 | { |
||
135 | 1 | $dir = $this->getDir(); |
|
136 | 1 | $pattern = $this->getPattern(); |
|
137 | |||
138 | 1 | if ($this->find($pattern)->in($dir)->has() === false) { |
|
139 | 1 | $dir = dirname($dir); |
|
140 | |||
141 | 1 | $this->file_list = $this->find($pattern)->in($dir)->up()->get(); |
|
0 ignored issues
–
show
It seems like
$this->find($pattern)->in($dir)->up()->get() of type * is incompatible with the declared type array of property $file_list .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..
Loading history...
|
|||
142 | } else { |
||
143 | $this->file_list = $this->find($pattern)->in($dir)->get(); |
||
0 ignored issues
–
show
It seems like
$this->find($pattern)->in($dir)->get() of type * is incompatible with the declared type array of property $file_list .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..
Loading history...
|
|||
144 | } |
||
145 | |||
146 | 1 | return $this; |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Sets the directory to start search in. |
||
151 | * |
||
152 | * @author Luke Watts <[email protected]> |
||
153 | * |
||
154 | * @since 2.0.0 |
||
155 | * |
||
156 | * @param $dir |
||
157 | * |
||
158 | * @return File |
||
159 | */ |
||
160 | 9 | public function in($dir) |
|
161 | { |
||
162 | 9 | $this->dir = $dir; |
|
163 | |||
164 | 9 | $this->make(); |
|
165 | |||
166 | 9 | return $this; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Search the parent directory. |
||
171 | * |
||
172 | * @author Luke Watts <[email protected]> |
||
173 | * |
||
174 | * @return File |
||
175 | */ |
||
176 | 1 | View Code Duplication | public function upOne() |
177 | { |
||
178 | 1 | $dir = $this->getDir(); |
|
179 | 1 | $pattern = $this->getPattern(); |
|
180 | |||
181 | 1 | if ($this->find($pattern)->in($dir)->has() === false) { |
|
182 | $dir = dirname($dir); |
||
183 | $this->file_list = $this->find($pattern)->in($dir)->get(); |
||
184 | } else { |
||
185 | 1 | $this->file_list = $this->find($pattern)->in($dir)->get(); |
|
186 | } |
||
187 | |||
188 | 1 | return $this; |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * Recursively searches parent directories. |
||
193 | * |
||
194 | * @author Luke Watts <[email protected]> |
||
195 | * |
||
196 | * @return File |
||
197 | */ |
||
198 | 2 | View Code Duplication | public function up() |
199 | { |
||
200 | 2 | $dir = $this->getDir(); |
|
201 | 2 | $pattern = $this->getPattern(); |
|
202 | |||
203 | 2 | if ($this->find($pattern)->in($dir)->has() === false) { |
|
204 | $dir = dirname($dir); |
||
205 | |||
206 | $this->file_list = $this->find($pattern)->in($dir)->up()->get(); |
||
207 | } else { |
||
208 | 2 | $this->file_list = $this->find($pattern)->in($dir)->get(); |
|
209 | } |
||
210 | |||
211 | 2 | return $this; |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * Return specified amount of files |
||
216 | * |
||
217 | * @author Luke Watts <[email protected]> |
||
218 | * |
||
219 | * @param int $limit |
||
220 | * |
||
221 | * @return array|bool|mixed |
||
222 | */ |
||
223 | 8 | public function get($limit = -1) |
|
224 | { |
||
225 | 8 | if ($limit === 0 || $limit < -1) { |
|
226 | 2 | throw new \InvalidArgumentException(sprintf('An integer of %s cannot be passed as a limit to the `get` method. Only -1, 1 or more can be given.', $limit)); |
|
227 | } |
||
228 | 6 | $this->limit = $limit; |
|
229 | |||
230 | 6 | if (isset($this->getFileList()[0])) { |
|
231 | 6 | if ($this->limit === -1) { |
|
232 | 6 | return $this->getFileList(); |
|
233 | 1 | } elseif ($this->limit === 1) { |
|
234 | 1 | return $this->getFileList()[0]; |
|
235 | } else { |
||
236 | 1 | return array_slice($this->getFileList(), 0, $this->limit); |
|
237 | } |
||
238 | } else { |
||
239 | return false; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Checks existence of file. |
||
245 | * |
||
246 | * @author Luke Watts <[email protected]> |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | 4 | public function has() |
|
251 | { |
||
252 | 4 | return isset($this->getFileList()[0]); |
|
253 | } |
||
254 | |||
255 | /** |
||
256 | * Check is the Regex a valid pattern |
||
257 | * |
||
258 | * @author Luke Watts <[email protected]> |
||
259 | * |
||
260 | * @since 2.1.3 |
||
261 | * |
||
262 | * @param $pattern |
||
263 | * |
||
264 | 10 | * @return bool |
|
265 | */ |
||
266 | public function isValidPattern($pattern) |
||
267 | 10 | { |
|
268 | 10 | // Check if first character is one of the self::$regex_delimiters |
|
269 | foreach ($this->regex_delimiters as $delimiter) { |
||
270 | $pos = (strpos($pattern, $delimiter) === 0) ? $delimiter : false; |
||
271 | 10 | ||
272 | // If first character is one of the $common_regex_delimiters |
||
273 | 10 | if ($pos !== false) { |
|
274 | // Then check if the last character is the same |
||
275 | 10 | $index = strlen($pattern) - 1; |
|
276 | |||
277 | 10 | $pos_last = (strrpos($pattern, $pos, $index) === $index) ? $pos : false; |
|
278 | |||
279 | return ($pos_last !== false) ? true : false; |
||
280 | } |
||
281 | 1 | } |
|
282 | |||
283 | return false; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Set the file list using by matching pattern |
||
288 | * |
||
289 | 9 | * @author Luke Watts <[email protected]> |
|
290 | * |
||
291 | 9 | * @since 2.1.4 |
|
292 | * |
||
293 | 9 | * @param \DirectoryIterator $iterator |
|
294 | * @param $pattern |
||
295 | 9 | */ |
|
296 | public function setFileListUsingPattern(\DirectoryIterator $iterator, $pattern) |
||
297 | { |
||
298 | 9 | $this->iterator = $iterator; |
|
299 | 9 | ||
300 | // Reset the array to avoid duplicate entry issue in version 1.0.0 in recursive methods |
||
301 | 9 | $this->file_list = []; |
|
302 | 9 | ||
303 | // If first and last are the same treat expression as a regex |
||
304 | 9 | foreach ($this->iterator as $item) { |
|
305 | 1 | if ($item->isDot() || $item->isDir()) { |
|
306 | continue; |
||
307 | } |
||
308 | 9 | ||
309 | 9 | if (preg_match($pattern, $item->getFilename()) === 1) { |
|
310 | $this->file_list[] = new \SplFileInfo($item->getPathname()); |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Make the search |
||
317 | * |
||
318 | * @author Luke Watts <[email protected]> |
||
319 | */ |
||
320 | public function make() |
||
321 | { |
||
322 | $this->iterator = new \DirectoryIterator($this->getDir()); |
||
323 | |||
324 | if ($this->isValidPattern($this->pattern)) { |
||
325 | // If first and last are the same treat expression as a regex |
||
326 | $this->setFileListUsingPattern(new \DirectoryIterator($this->getDir()), $this->pattern); |
||
327 | } else { |
||
328 | // Else use plain file name |
||
329 | $this->setFileListUsingPattern(new \DirectoryIterator($this->getDir()), '/^' . $this->pattern . '$/'); |
||
330 | } |
||
331 | } |
||
332 | 9 | ||
333 | /** |
||
334 | * Returns the current pattern to search for |
||
335 | * |
||
336 | * @author Luke Watts <[email protected]> |
||
337 | * |
||
338 | * @since 1.0.0 |
||
339 | * |
||
340 | * @return mixed |
||
341 | */ |
||
342 | public function getPattern() |
||
343 | 6 | { |
|
344 | return $this->pattern; |
||
345 | 6 | } |
|
346 | |||
347 | /** |
||
348 | * Returns the current limit |
||
349 | * |
||
350 | * @author Luke Watts <[email protected]> |
||
351 | * |
||
352 | * @since 1.0.0 |
||
353 | * |
||
354 | * @return int |
||
355 | */ |
||
356 | public function getLimit() |
||
357 | { |
||
358 | return $this->limit; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Get the current directory. |
||
363 | * |
||
364 | * @author Luke Watts <[email protected]> |
||
365 | * |
||
366 | * @since 2.0.0 |
||
367 | * |
||
368 | * @return mixed |
||
369 | */ |
||
370 | public function getDir() |
||
371 | 9 | { |
|
372 | return $this->dir; |
||
373 | 9 | } |
|
374 | |||
375 | /** |
||
376 | * Returns file list array |
||
377 | * |
||
378 | * @author Luke Watts <[email protected]> |
||
379 | * |
||
380 | * @since 1.0.0 |
||
381 | * |
||
382 | * @return array |
||
383 | */ |
||
384 | public function getFileList() |
||
385 | 6 | { |
|
386 | return $this->file_list; |
||
387 | 6 | } |
|
388 | } |
||
389 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..