Passed
Push — master ( f01d00...2f7647 )
by Nicolaas
03:27
created

FindFiles::setExtensions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Api;
4
5
class FindFiles
6
{
7
    //generic search settings
8
9
    private $needToFillFileCache = true;
10
11
    private $basePath = '';
12
13
    private $searchPath = '';
14
15
    private $relevantFolders = [];
16
17
    private $defaultIgnoreFolderArray = [
18
        '.svn',
19
        '.git',
20
    ];
21
22
    private $ignoreFolderArray = [];
23
24
    private $extensions = ['php', 'ss', 'yml', 'yaml', 'json', 'js', 'md'];
25
26
    private $findAllExts = false;
27
28
    // files
29
30
    /**
31
     * array of all the files we are searching
32
     * @var array
33
     */
34
    private $fileArray = [];
35
36
    private $flatFileArray = [];
37
38
    public function __construct($basePath = '')
39
    {
40
        $this->basePath = $basePath;
41
    }
42
43
    /**
44
     *   Sets folders to ignore
45
     *   @param array $ignoreFolderArray
46
     *
47
     *   @return FindFiles
48
     */
49
    public function setIgnoreFolderArray($ignoreFolderArray = [])
50
    {
51
        if ($this->ignoreFolderArray === $ignoreFolderArray) {
52
            //do nothing
53
        } else {
54
            $this->ignoreFolderArray = $ignoreFolderArray;
55
            $this->resetFileCache();
56
        }
57
58
        return $this;
59
    }
60
61
    /**
62
     *   Sets folders to ignore
63
     *   @param array $ignoreFolderArray
64
     *   @return self
65
     */
66
    public function addToIgnoreFolderArray($ignoreFolderArray = [])
67
    {
68
        $oldIgnoreFolderArray = $this->ignoreFolderArray;
69
        $this->ignoreFolderArray = array_unique(
70
            array_merge(
71
                $ignoreFolderArray,
72
                $this->defaultIgnoreFolderArray
73
            )
74
        );
75
        if ($oldIgnoreFolderArray !== $this->ignoreFolderArray) {
76
            $this->resetFileCache();
77
        }
78
79
        return $this;
80
    }
81
82
    /**
83
     * remove ignore folders
84
     */
85
    public function resetIgnoreFolderArray()
86
    {
87
        $this->ignoreFolderArray = [];
88
        $this->resetFileCache();
89
90
        return $this;
91
    }
92
93
    /**
94
     *   Sets extensions to look
95
     *   @param bool $boolean
96
     */
97
    public function setFindAllExts($boolean = true)
98
    {
99
        $this->findAllExts = $boolean;
100
        $this->resetFileCache();
101
102
        return $this;
103
    }
104
105
    public function setl($pathLocation)
106
    {
107
        $this->basePath = $pathLocation;
108
        $this->resetFileCache();
109
110
        return $this;
111
    }
112
113
    public function setSearchPath($pathLocation)
114
    {
115
        if ($pathLocation !== $this->searchPath) {
116
            $this->searchPath = $pathLocation;
117
            $this->resetFileCache();
118
        }
119
120
        return $this;
121
    }
122
123
    /**
124
     *   Sets extensions to look
125
     *   @param array $extensions
126
     */
127
    public function setExtensions($extensions = [])
128
    {
129
        $this->extensions = $extensions;
130
        if (count($this->extensions)) {
131
            $this->findAllExts = false;
132
        } else {
133
            $this->findAllExts = true;
134
        }
135
        $this->resetFileCache();
136
137
        return $this;
138
    }
139
140
    /**
141
     * string is error!
142
     * @return array|string
143
     */
144
    public function getFlatFileArray()
145
    {
146
        if ($this->needToFillFileCache) {
147
            $myArray = [];
148
            if ($this->searchPath) {
149
                if (file_exists($this->searchPath)) {
150
                    if (is_file($this->searchPath)) {
151
                        $this->flatFileArray = [$this->searchPath];
152
                    } else {
153
                        $multiDimensionalArray = $this->getFileArray($this->searchPath);
154
                        foreach ($multiDimensionalArray as $folder => $arrayOfFiles) {
155
                            if (count($arrayOfFiles)) {
156
                                $this->relevantFolders[$folder] = $folder;
157
                            }
158
                            foreach ($arrayOfFiles as $file) {
159
                                $myArray[$file] = $file;
160
                            }
161
                        }
162
                        // //flatten it!
163
                        // $this->flatFileArray = new \RecursiveIteratorIterator(
164
                        //     new \RecursiveArrayIterator($multiDimensionalArray)
165
                        // );
166
                        // print_r($this->flatFileArray);
167
                    }
168
                } else {
169
                    return 'SKIPPED: can not find: ' . $this->searchPath . "\n";
170
                }
171
            }
172
            $this->flatFileArray = array_values($myArray);
173
            $this->needToFillFileCache = false;
174
        }
175
176
        return $this->flatFileArray;
177
    }
178
179
    /**
180
     * loads all the applicable files
181
     * @param string $path (e.g. "." or "/var/www/mysite.co.nz")
182
     * @param boolean $runningInnerLoop - is the method calling itself???
183
     */
184
    protected function getFileArray($path, $runningInnerLoop = false)
185
    {
186
        if ($runningInnerLoop || $this->needToFillFileCache) {
187
            $dir = opendir($path);
188
            while ($file = readdir($dir)) {
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

188
            while ($file = readdir(/** @scrutinizer ignore-type */ $dir)) {
Loading history...
189
                $fullPath = $path . '/' . $file;
190
                if (($file === '.') || ($file === '..') || ($fullPath === __FILE__) || ($path === '.' && basename(__FILE__) === $file)) {
191
                    continue;
192
                }
193
                //ignore hidden files and folders
194
                if (substr($file, 0, 1) === '.') {
195
                    continue;
196
                }
197
                //ignore folders with _manifest_exclude in them!
198
                if ($file === '_manifest_exclude') {
199
                    $this->ignoreFolderArray[] = $path;
200
                    unset($this->fileArray[$path]);
201
                    break;
202
                }
203
                if (filetype($fullPath) === 'dir') {
204
                    $conditionA = (in_array($file, $this->ignoreFolderArray, true) && ($path === '.' || $path === $this->searchPath));
205
                    $conditionB = in_array($path, $this->ignoreFolderArray, true);
206
                    if ($conditionA || $conditionB) {
207
                        continue;
208
                    }
209
                    $this->getFileArray($fullPath, $runningInnerLoop = true); //recursive traversing here
210
                } elseif ($this->matchedExtension($file)) { //checks extension if we need to search this file
211
                    if (filesize($fullPath)) {
212
                        $this->fileArray[$path][] = $fullPath; //search file data
213
                    }
214
                }
215
            } //End of while
216
            closedir($dir);
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

216
            closedir(/** @scrutinizer ignore-type */ $dir);
Loading history...
217
        }
218
219
        return $this->fileArray;
220
    }
221
222
    //FIND FILES
223
224
    protected function resetFileCache()
225
    {
226
        $this->fileArray = null;
227
        $this->fileArray = [];
228
        $this->flatFileArray = null;
229
        $this->flatFileArray = [];
230
        $this->needToFillFileCache = true;
231
        //cleanup other data
232
    }
233
234
    /**
235
     * Finds extension of a file
236
     * @param $file
237
     *
238
     * @return string
239
     */
240
    private function findExtension($file)
241
    {
242
        $fileArray = explode('.', $file);
243
244
        return array_pop($fileArray);
245
    }
246
247
    /**
248
     * Checks if a file extension is one of the extensions we are going to search
249
     * @param string $file
250
     * @return boolean
251
     */
252
    private function matchedExtension($file)
253
    {
254
        $fileExtension = $this->findExtension($file);
255
        if ($this->findAllExts) {
256
            return true;
257
        } elseif (in_array('*', $this->extensions, true)) {
258
            return true;
259
        } elseif (in_array($fileExtension, $this->extensions, true)) {
260
            return true;
261
        }
262
        return false;
263
    }
264
}
265