Completed
Push — master ( 83af11...14d3c9 )
by Luke
02:24
created

File::isValidPattern()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 8
cts 8
cp 1
rs 8.8571
cc 6
eloc 8
nc 11
nop 1
crap 6
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
     * @param $pattern
257
     *
258
     * @author Luke Watts <[email protected]>
259
     *
260
     * @since 2.1.3
261
     *
262
     * @return bool
263
     */
264 10
    public function isValidPattern($pattern)
265
    {
266
        // Check if first character is one of the self::$regex_delimiters
267 10
        foreach ($this->regex_delimiters as $delimiter) {
268 10
            $pos = (strpos($pattern, $delimiter) === 0) ? $delimiter : false;
269
270
            // If first character is one of the $common_regex_delimiters
271 10
            if ($pos !== false) {
272
                // Then check if the last character is the same
273 10
                $index = strlen($pattern) - 1;
274
275 10
                $pos_last = (strrpos($pattern, $pos, $index) === $index) ? $pos : false;
276
277 10
                return ($pos_last !== false) ? true : false;
278
            }
279
        }
280
281 1
        return false;
282
    }
283
284
    /**
285
     * Make the search
286
     *
287
     * @author Luke Watts <[email protected]>
288
     */
289 9
    public function make()
290
    {
291 9
        $this->iterator = new \DirectoryIterator($this->getDir());
292
293 9
        if ($this->isValidPattern($this->pattern)) {
294
            // Reset the array to avoid duplicate entry issue in version 1.0.0 in recursive methods
295 9
            $this->file_list = [];
296
297
            // If first and last are the same treat expression as a regex
298 9
            foreach ($this->iterator as $item) {
299 9
                $filename = $item->getFilename();
300
301 9
                if ($item->isDot()) {
302 9
                    continue;
303
                }
304 9
                if ($item->isDir()) {
305 1
                    continue;
306
                }
307
308 9
                if (preg_match($this->pattern, $filename) === 1) {
309 9
                    $this->file_list[] = new \SplFileInfo($item->getPathname());
310
                }
311
            }
312
        } else {
313
            // Reset the array to avoid duplicate entry issue in version 1.0.0 in recursive methods
314
            $this->file_list = [];
315
316
            // Else use plain file name
317
            foreach ($this->iterator as $item) {
318
                $filename = $item->getFilename();
319
320
                if ($item->isDot()) {
321
                    continue;
322
                }
323
                if ($item->isDir()) {
324
                    continue;
325
                }
326
327
                if (preg_match('/^' . preg_quote($this->pattern) . '$/', $filename) === 1) {
328
                    $this->file_list[] = new \SplFileInfo($item->getPathname());
329
                }
330
            }
331
        }
332 9
    }
333
334
    /**
335
     * Returns the current pattern to search for
336
     *
337
     * @author Luke Watts <[email protected]>
338
     *
339
     * @since 1.0.0
340
     *
341
     * @return mixed
342
     */
343 6
    public function getPattern()
344
    {
345 6
        return $this->pattern;
346
    }
347
348
    /**
349
     * Returns the current limit
350
     *
351
     * @author Luke Watts <[email protected]>
352
     *
353
     * @since  1.0.0
354
     *
355
     * @return mixed
356
     */
357
    public function getLimit()
358
    {
359
        return $this->limit;
360
    }
361
362
    /**
363
     * Get the current directory.
364
     *
365
     * @author Luke Watts <[email protected]>
366
     *
367
     * @since  2.0.0
368
     *
369
     * @return mixed
370
     */
371 9
    public function getDir()
372
    {
373 9
        return $this->dir;
374
    }
375
376
    /**
377
     * Returns file list array
378
     *
379
     * @author Luke Watts <[email protected]>
380
     *
381
     * @since  1.0.0
382
     *
383
     * @return array
384
     */
385 6
    public function getFileList()
386
    {
387 6
        return $this->file_list;
388
    }
389
}
390