Test Failed
Push — master ( 5d500b...cf90e3 )
by Luke
02:53
created

File::upOne()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 14
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
crap 2
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
    public function parent()
110
    {
111 1
        return $this->upOne();
112 1
    }
113
114 1
    /**
115
     * Alias of the up() method
116
     *
117
     * @author Luke Watts <[email protected]>
118 1
     *
119
     * @since  2.0.0
120
     *
121 1
     * @return File
122
     */
123
    public function parents()
124
    {
125
        return $this->up();
126
    }
127
128
    /**
129
     * Sets the directory to start search in.
130
     *
131
     * @author Luke Watts <[email protected]>
132
     *
133 1
     * @since  2.0.0
134
     *
135 1
     * @param $dir
136 1
     *
137
     * @return File
138 1
     */
139 1
    public function in($dir)
140
    {
141 1
        $this->dir = $dir;
142
143
        $this->make();
144
145
        return $this;
146 1
    }
147
148
    /**
149
     * Search the parent directory.
150
     *
151
     * @author Luke Watts <[email protected]>
152
     *
153
     * @return File
154
     */
155 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...
156
    {
157
        $dir = $this->getDir();
158
        $pattern = $this->getPattern();
159
160 9
        if ($this->find($pattern)->in($dir)->has() === false) {
161
            $dir = dirname($dir);
162 9
            $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...
163
        } else {
164 9
            $this->file_list = $this->find($pattern)->in($dir)->get();
165
        }
166 9
167
        return $this;
168
    }
169
170
    /**
171
     * Recursively searches parent directories.
172
     *
173
     * @author Luke Watts <[email protected]>
174
     *
175
     * @return File
176 1
     */
177 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...
178 1
    {
179 1
        $dir = $this->getDir();
180
        $pattern = $this->getPattern();
181 1
182
        if ($this->find($pattern)->in($dir)->has() === false) {
183
            $dir = dirname($dir);
184
185 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...
186
        } else {
187
            $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...
188 1
        }
189
190
        return $this;
191
    }
192
193
    /**
194
     * Return specified amount of files
195
     *
196
     * @author Luke Watts <[email protected]>
197
     *
198 2
     * @param int $limit
199
     *
200 2
     * @return array|bool|mixed
201 2
     */
202
    public function get($limit = -1)
203 2
    {
204
        if ($limit === 0 || $limit < -1) {
205
            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));
206
        }
207
        $this->limit = $limit;
208 2
209
        if (isset($this->getFileList()[0])) {
210
            if ($this->limit === -1) {
211 2
                return $this->getFileList();
212
            } elseif ($this->limit === 1) {
213
                return $this->getFileList()[0];
214
            } else {
215
                return array_slice($this->getFileList(), 0, $this->limit);
216
            }
217
        } else {
218
            return false;
219
        }
220
    }
221
222
    /**
223 8
     * Checks existence of file.
224
     *
225 8
     * @author Luke Watts <[email protected]>
226 2
     *
227
     * @return bool
228 6
     */
229
    public function has()
230 6
    {
231 6
        return isset($this->getFileList()[0]);
232 6
    }
233 1
234 1
    /**
235
     * Check is the Regex a valid pattern
236 1
     *
237
     * @author Luke Watts <[email protected]>
238
     *
239
     * @since  2.1.3
240
     *
241
     * @param $pattern
242
     *
243
     * @return bool
244
     */
245
    public function isValidPattern($pattern)
246
    {
247
        // Check if first character is one of the self::$regex_delimiters
248
        foreach ($this->regex_delimiters as $delimiter) {
249
            $pos = (strpos($pattern, $delimiter) === 0) ? $delimiter : false;
250 4
251
            // If first character is one of the $common_regex_delimiters
252 4
            if ($pos !== false) {
253
                // Then check if the last character is the same
254
                $index = strlen($pattern) - 1;
255
256
                $pos_last = (strrpos($pattern, $pos, $index) === $index) ? $pos : false;
257
258
                return ($pos_last !== false) ? true : false;
259
            }
260
        }
261
262
        return false;
263
    }
264 10
265
    /**
266
     * Set the file list using by matching pattern
267 10
     *
268 10
     * @author Luke Watts <[email protected]>
269
     *
270
     * @since  2.1.4
271 10
     *
272
     * @param \DirectoryIterator $iterator
273 10
     * @param                    $pattern
274
     */
275 10
    public function setFileListUsingPattern(\DirectoryIterator $iterator, $pattern)
276
    {
277 10
        $this->iterator = $iterator;
278
279
        // Reset the array to avoid duplicate entry issue in version 1.0.0 in recursive methods
280
        $this->file_list = [];
281 1
282
        // If first and last are the same treat expression as a regex
283
        foreach ($this->iterator as $item) {
284
            if ($item->isDot() || $item->isDir()) {
285
                continue;
286
            }
287
288
            if (preg_match($pattern, $item->getFilename()) === 1) {
289 9
                $this->file_list[] = new \SplFileInfo($item->getPathname());
290
            }
291 9
        }
292
    }
293 9
294
    /**
295 9
     * Make the search
296
     *
297
     * @author Luke Watts <[email protected]>
298 9
     */
299 9
    public function make()
300
    {
301 9
        $this->iterator = new \DirectoryIterator($this->getDir());
302 9
303
        if ($this->isValidPattern($this->pattern)) {
304 9
            // If first and last are the same treat expression as a regex
305 1
            $this->setFileListUsingPattern(new \DirectoryIterator($this->getDir()), $this->pattern);
306
        } else {
307
            // Else use plain file name
308 9
            $this->setFileListUsingPattern(new \DirectoryIterator($this->getDir()), '/^' . $this->pattern . '$/');
309 9
        }
310
    }
311
312
    /**
313
     * Returns the current pattern to search for
314
     *
315
     * @author Luke Watts <[email protected]>
316
     *
317
     * @since 1.0.0
318
     *
319
     * @return mixed
320
     */
321
    public function getPattern()
322
    {
323
        return $this->pattern;
324
    }
325
326
    /**
327
     * Get the current directory.
328
     *
329
     * @author Luke Watts <[email protected]>
330
     *
331
     * @since  2.0.0
332 9
     *
333
     * @return mixed
334
     */
335
    public function getDir()
336
    {
337
        return $this->dir;
338
    }
339
340
    /**
341
     * Returns file list array
342
     *
343 6
     * @author Luke Watts <[email protected]>
344
     *
345 6
     * @since  1.0.0
346
     *
347
     * @return array
348
     */
349
    public function getFileList()
350
    {
351
        return $this->file_list;
352
    }
353
}
354