Passed
Push — master ( e370f2...96bfac )
by Luke
02:26
created

File   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 332
Duplicated Lines 8.73 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 98.55%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 31
c 5
b 0
f 0
lcom 1
cbo 0
dl 29
loc 332
ccs 68
cts 69
cp 0.9855
rs 9.8

14 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 6 1
A parent() 0 4 1
A parents() 0 4 1
A in() 0 8 1
A upOne() 14 14 2
A up() 15 15 2
B get() 0 19 6
A has() 0 4 1
B isValidPattern() 0 19 6
B setFileListUsingPattern() 0 18 5
A make() 0 12 2
A getPattern() 0 4 1
A getDir() 0 4 1
A getFileList() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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