Total Complexity | 44 |
Total Lines | 258 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like FindFiles often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FindFiles, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 = '') |
||
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) |
||
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)) { |
||
|
|||
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); |
||
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) |
||
263 | } |
||
264 | } |
||
265 |