Test Failed
Push — master ( 3097ea...057c6f )
by Luke
02:24
created

File::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.048

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 1
cts 5
cp 0.2
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 4.048
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
Documentation Bug introduced by
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...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
Documentation Bug introduced by
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
Documentation Bug introduced by
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
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...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
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...
207
        } else {
208 2
            $this->file_list = $this->find($pattern)->in($dir)->get();
0 ignored issues
show
Documentation Bug introduced by
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...
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
317
    /**
318
     * Make the search
319
     *
320
     * @author Luke Watts <[email protected]>
321
     */
322
    public function make()
323
    {
324
        $this->iterator = new \DirectoryIterator($this->getDir());
325
326
        if ($this->isValidPattern($this->pattern)) {
327
            // If first and last are the same treat expression as a regex
328
            $this->setFileListUsingPattern(new \DirectoryIterator($this->getDir()), $this->pattern);
329
        } else {
330
            // Else use plain file name
331
            $this->setFileListUsingPattern(new \DirectoryIterator($this->getDir()), '/^' . $this->pattern . '$/');
332 9
        }
333
    }
334
335
    /**
336
     * Returns the current pattern to search for
337
     *
338
     * @author Luke Watts <[email protected]>
339
     *
340
     * @since 1.0.0
341
     *
342
     * @return mixed
343 6
     */
344
    public function getPattern()
345 6
    {
346
        return $this->pattern;
347
    }
348
349
    /**
350
     * Returns the current limit
351
     *
352
     * @author Luke Watts <[email protected]>
353
     *
354
     * @since  1.0.0
355
     *
356
     * @return integer
357
     */
358
    public function getLimit()
359
    {
360
        return $this->limit;
361
    }
362
363
    /**
364
     * Get the current directory.
365
     *
366
     * @author Luke Watts <[email protected]>
367
     *
368
     * @since  2.0.0
369
     *
370
     * @return mixed
371 9
     */
372
    public function getDir()
373 9
    {
374
        return $this->dir;
375
    }
376
377
    /**
378
     * Returns file list array
379
     *
380
     * @author Luke Watts <[email protected]>
381
     *
382
     * @since  1.0.0
383
     *
384
     * @return array
385 6
     */
386
    public function getFileList()
387 6
    {
388
        return $this->file_list;
389
    }
390
}
391