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:
Complex classes like FileHandler 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FileHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class FileHandler |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * Changes path of target file, directory into absolute path |
||
14 | * |
||
15 | * @param string $source path to change into absolute path |
||
16 | * @return string Absolute path |
||
17 | */ |
||
18 | function getRealPath($source) |
||
19 | { |
||
20 | if(strlen($source) >= 2 && substr_compare($source, './', 0, 2) === 0) |
||
21 | { |
||
22 | return _XE_PATH_ . substr($source, 2); |
||
23 | } |
||
24 | |||
25 | return $source; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Copy a directory to target |
||
30 | * |
||
31 | * If target directory does not exist, this function creates it |
||
32 | * |
||
33 | * @param string $source_dir Path of source directory |
||
34 | * @param string $target_dir Path of target dir |
||
35 | * @param string $filter Regex to filter files. If file matches this regex, the file is not copied. |
||
36 | * @param string $type If set as 'force'. Even if the file exists in target, the file is copied. |
||
37 | * @return void |
||
38 | */ |
||
39 | function copyDir($source_dir, $target_dir, $filter = null, $type = null) |
||
40 | { |
||
41 | $source_dir = self::getRealPath($source_dir); |
||
42 | $target_dir = self::getRealPath($target_dir); |
||
43 | if(!is_dir($source_dir)) |
||
44 | { |
||
45 | return FALSE; |
||
46 | } |
||
47 | |||
48 | // generate when no target exists |
||
49 | self::makeDir($target_dir); |
||
50 | |||
51 | if(substr($source_dir, -1) != DIRECTORY_SEPARATOR) |
||
52 | { |
||
53 | $source_dir .= DIRECTORY_SEPARATOR; |
||
54 | } |
||
55 | |||
56 | if(substr($target_dir, -1) != DIRECTORY_SEPARATOR) |
||
57 | { |
||
58 | $target_dir .= DIRECTORY_SEPARATOR; |
||
59 | } |
||
60 | |||
61 | $oDir = dir($source_dir); |
||
62 | while($file = $oDir->read()) |
||
63 | { |
||
64 | if($file{0} == '.') |
||
65 | { |
||
66 | continue; |
||
67 | } |
||
68 | |||
69 | if($filter && preg_match($filter, $file)) |
||
|
|||
70 | { |
||
71 | continue; |
||
72 | } |
||
73 | |||
74 | if(is_dir($source_dir . $file)) |
||
75 | { |
||
76 | self::copyDir($source_dir . $file, $target_dir . $file, $type); |
||
77 | } |
||
78 | else |
||
79 | { |
||
80 | if($type == 'force') |
||
81 | { |
||
82 | @unlink($target_dir . $file); |
||
83 | } |
||
84 | else |
||
85 | { |
||
86 | if(!file_exists($target_dir . $file)) |
||
87 | { |
||
88 | @copy($source_dir . $file, $target_dir . $file); |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | $oDir->close(); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Copy a file to target |
||
98 | * |
||
99 | * @param string $source Path of source file |
||
100 | * @param string $target Path of target file |
||
101 | * @param string $force Y: overwrite |
||
102 | * @return void |
||
103 | */ |
||
104 | function copyFile($source, $target, $force = 'Y') |
||
105 | { |
||
106 | setlocale(LC_CTYPE, 'en_US.UTF8', 'ko_KR.UTF8'); |
||
107 | $source = self::getRealPath($source); |
||
108 | $target_dir = self::getRealPath(dirname($target)); |
||
109 | $target = basename($target); |
||
110 | |||
111 | self::makeDir($target_dir); |
||
112 | |||
113 | if($force == 'Y') |
||
114 | { |
||
115 | @unlink($target_dir . DIRECTORY_SEPARATOR . $target); |
||
116 | } |
||
117 | |||
118 | @copy($source, $target_dir . DIRECTORY_SEPARATOR . $target); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Returns the content of the file |
||
123 | * |
||
124 | * @param string $filename Path of target file |
||
125 | * @return string The content of the file. If target file does not exist, this function returns nothing. |
||
126 | */ |
||
127 | function readFile($filename) |
||
128 | { |
||
129 | if(($filename = self::exists($filename)) === FALSE || filesize($filename) < 1) |
||
130 | { |
||
131 | return; |
||
132 | } |
||
133 | |||
134 | return @file_get_contents($filename); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Write $buff into the specified file |
||
139 | * |
||
140 | * @param string $filename Path of target file |
||
141 | * @param string $buff Content to be written |
||
142 | * @param string $mode a(append) / w(write) |
||
143 | * @return void |
||
144 | */ |
||
145 | function writeFile($filename, $buff, $mode = "w") |
||
146 | { |
||
147 | $filename = self::getRealPath($filename); |
||
148 | $pathinfo = pathinfo($filename); |
||
149 | self::makeDir($pathinfo['dirname']); |
||
150 | |||
151 | $flags = 0; |
||
152 | if(strtolower($mode) == 'a') |
||
153 | { |
||
154 | $flags = FILE_APPEND; |
||
155 | } |
||
156 | |||
157 | @file_put_contents($filename, $buff, $flags|LOCK_EX); |
||
158 | @chmod($filename, 0644); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Remove a file |
||
163 | * |
||
164 | * @param string $filename path of target file |
||
165 | * @return bool Returns TRUE on success or FALSE on failure. |
||
166 | */ |
||
167 | function removeFile($filename) |
||
168 | { |
||
169 | return (($filename = self::exists($filename)) !== FALSE) && @unlink($filename); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Rename a file |
||
174 | * |
||
175 | * In order to move a file, use this function. |
||
176 | * |
||
177 | * @param string $source Path of source file |
||
178 | * @param string $target Path of target file |
||
179 | * @return bool Returns TRUE on success or FALSE on failure. |
||
180 | */ |
||
181 | function rename($source, $target) |
||
182 | { |
||
183 | return @rename(self::getRealPath($source), self::getRealPath($target)); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Move a file |
||
188 | * |
||
189 | * @param string $source Path of source file |
||
190 | * @param string $target Path of target file |
||
191 | * @return bool Returns TRUE on success or FALSE on failure. |
||
192 | */ |
||
193 | function moveFile($source, $target) |
||
194 | { |
||
195 | if(($source = self::exists($source)) !== FALSE) |
||
196 | { |
||
197 | self::removeFile($target); |
||
198 | return self::rename($source, $target); |
||
199 | } |
||
200 | return FALSE; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Move a directory |
||
205 | * |
||
206 | * This function just wraps rename function. |
||
207 | * |
||
208 | * @param string $source_dir Path of source directory |
||
209 | * @param string $target_dir Path of target directory |
||
210 | * @return void |
||
211 | */ |
||
212 | function moveDir($source_dir, $target_dir) |
||
213 | { |
||
214 | self::rename($source_dir, $target_dir); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Return list of the files in the path |
||
219 | * |
||
220 | * The array does not contain files, such as '.', '..', and files starting with '.' |
||
221 | * |
||
222 | * @param string $path Path of target directory |
||
223 | * @param string $filter If specified, return only files matching with the filter |
||
224 | * @param bool $to_lower If TRUE, file names will be changed into lower case. |
||
225 | * @param bool $concat_prefix If TRUE, return file name as absolute path |
||
226 | * @return string[] Array of the filenames in the path |
||
227 | */ |
||
228 | function readDir($path, $filter = '', $to_lower = FALSE, $concat_prefix = FALSE) |
||
229 | { |
||
230 | $path = self::getRealPath($path); |
||
231 | $output = array(); |
||
232 | |||
233 | if(substr($path, -1) != '/') |
||
234 | { |
||
235 | $path .= '/'; |
||
236 | } |
||
237 | |||
238 | if(!is_dir($path)) |
||
239 | { |
||
240 | return $output; |
||
241 | } |
||
242 | |||
243 | $files = scandir($path); |
||
244 | foreach($files as $file) |
||
245 | { |
||
246 | if($file{0} == '.' || ($filter && !preg_match($filter, $file))) |
||
247 | { |
||
248 | continue; |
||
249 | } |
||
250 | |||
251 | if($to_lower) |
||
252 | { |
||
253 | $file = strtolower($file); |
||
254 | } |
||
255 | |||
256 | if($filter) |
||
257 | { |
||
258 | $file = preg_replace($filter, '$1', $file); |
||
259 | } |
||
260 | |||
261 | if($concat_prefix) |
||
262 | { |
||
263 | $file = sprintf('%s%s', str_replace(_XE_PATH_, '', $path), $file); |
||
264 | } |
||
265 | |||
266 | $output[] = str_replace(array('/\\', '//'), '/', $file); |
||
267 | } |
||
268 | |||
269 | return $output; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Creates a directory |
||
274 | * |
||
275 | * This function creates directories recursively, which means that if ancestors of the target directory does not exist, they will be created too. |
||
276 | * |
||
277 | * @param string $path_string Path of target directory |
||
278 | * @return bool TRUE if success. It might return nothing when ftp is used and connection to the ftp address failed. |
||
279 | */ |
||
280 | function makeDir($path_string) |
||
281 | { |
||
282 | if(self::exists($path_string) !== FALSE) |
||
283 | { |
||
284 | return TRUE; |
||
285 | } |
||
286 | |||
287 | if(!ini_get('safe_mode')) |
||
288 | { |
||
289 | @mkdir($path_string, 0755, TRUE); |
||
290 | @chmod($path_string, 0755); |
||
291 | } |
||
292 | // if safe_mode is on, use FTP |
||
293 | else |
||
294 | { |
||
295 | static $oFtp = NULL; |
||
296 | |||
297 | $ftp_info = Context::getFTPInfo(); |
||
298 | if($oFtp == NULL) |
||
299 | { |
||
300 | if(!Context::isFTPRegisted()) |
||
301 | { |
||
302 | return; |
||
303 | } |
||
304 | |||
305 | require_once(_XE_PATH_ . 'libs/ftp.class.php'); |
||
306 | $oFtp = new ftp(); |
||
307 | if(!$ftp_info->ftp_host) |
||
308 | { |
||
309 | $ftp_info->ftp_host = "127.0.0.1"; |
||
310 | } |
||
311 | if(!$ftp_info->ftp_port) |
||
312 | { |
||
313 | $ftp_info->ftp_port = 21; |
||
314 | } |
||
315 | if(!$oFtp->ftp_connect($ftp_info->ftp_host, $ftp_info->ftp_port)) |
||
316 | { |
||
317 | return; |
||
318 | } |
||
319 | if(!$oFtp->ftp_login($ftp_info->ftp_user, $ftp_info->ftp_password)) |
||
320 | { |
||
321 | $oFtp->ftp_quit(); |
||
322 | return; |
||
323 | } |
||
324 | } |
||
325 | |||
326 | if(!($ftp_path = $ftp_info->ftp_root_path)) |
||
327 | { |
||
328 | $ftp_path = DIRECTORY_SEPARATOR; |
||
329 | } |
||
330 | |||
331 | $path_string = str_replace(_XE_PATH_, '', $path_string); |
||
332 | $path_list = explode(DIRECTORY_SEPARATOR, $path_string); |
||
333 | |||
334 | $path = _XE_PATH_; |
||
335 | for($i = 0, $c = count($path_list); $i < $c; $i++) |
||
336 | { |
||
337 | if(!$path_list[$i]) |
||
338 | { |
||
339 | continue; |
||
340 | } |
||
341 | |||
342 | $path .= $path_list[$i] . DIRECTORY_SEPARATOR; |
||
343 | $ftp_path .= $path_list[$i] . DIRECTORY_SEPARATOR; |
||
344 | if(!is_dir($path)) |
||
345 | { |
||
346 | $oFtp->ftp_mkdir($ftp_path); |
||
347 | $oFtp->ftp_site("CHMOD 777 " . $ftp_path); |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | |||
352 | return is_dir($path_string); |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Remove all files under the path |
||
357 | * |
||
358 | * @param string $path Path of the target directory |
||
359 | * @return void |
||
360 | */ |
||
361 | View Code Duplication | function removeDir($path) |
|
362 | { |
||
363 | if(($path = self::isDir($path)) === FALSE) |
||
364 | { |
||
365 | return; |
||
366 | } |
||
367 | |||
368 | if(self::isDir($path)) |
||
369 | { |
||
370 | $files = array_diff(scandir($path), array('..', '.')); |
||
371 | |||
372 | foreach($files as $file) |
||
373 | { |
||
374 | if(($target = self::getRealPath($path . DIRECTORY_SEPARATOR . $file)) === FALSE) |
||
375 | { |
||
376 | continue; |
||
377 | } |
||
378 | |||
379 | if(is_dir($target)) |
||
380 | { |
||
381 | self::removeDir($target); |
||
382 | } |
||
383 | else |
||
384 | { |
||
385 | unlink($target); |
||
386 | } |
||
387 | } |
||
388 | rmdir($path); |
||
389 | } |
||
390 | else |
||
391 | { |
||
392 | unlink($path); |
||
393 | } |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Remove a directory only if it is empty |
||
398 | * |
||
399 | * @param string $path Path of the target directory |
||
400 | * @return void |
||
401 | */ |
||
402 | function removeBlankDir($path) |
||
403 | { |
||
404 | if(($path = self::isDir($path)) === FALSE) |
||
405 | { |
||
406 | return; |
||
407 | } |
||
408 | |||
409 | $files = array_diff(scandir($path), array('..', '.')); |
||
410 | |||
411 | if(count($files) < 1) |
||
412 | { |
||
413 | rmdir($path); |
||
414 | return; |
||
415 | } |
||
416 | |||
417 | foreach($files as $file) |
||
418 | { |
||
419 | if(($target = self::isDir($path . DIRECTORY_SEPARATOR . $file)) === FALSE) |
||
420 | { |
||
421 | continue; |
||
422 | } |
||
423 | |||
424 | self::removeBlankDir($target); |
||
425 | } |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Remove files in the target directory |
||
430 | * |
||
431 | * This function keeps the directory structure. |
||
432 | * |
||
433 | * @param string $path Path of the target directory |
||
434 | * @return void |
||
435 | */ |
||
436 | View Code Duplication | function removeFilesInDir($path) |
|
437 | { |
||
438 | if(($path = self::getRealPath($path)) === FALSE) |
||
439 | { |
||
440 | return; |
||
441 | } |
||
442 | |||
443 | if(is_dir($path)) |
||
444 | { |
||
445 | $files = array_diff(scandir($path), array('..', '.')); |
||
446 | |||
447 | foreach($files as $file) |
||
448 | { |
||
449 | if(($target = self::getRealPath($path . DIRECTORY_SEPARATOR . $file)) === FALSE) |
||
450 | { |
||
451 | continue; |
||
452 | } |
||
453 | |||
454 | if(is_dir($target)) |
||
455 | { |
||
456 | self::removeFilesInDir($target); |
||
457 | } |
||
458 | else |
||
459 | { |
||
460 | unlink($target); |
||
461 | } |
||
462 | } |
||
463 | } |
||
464 | else |
||
465 | { |
||
466 | if(self::exists($path)) unlink($path); |
||
467 | } |
||
468 | |||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Makes file size byte into KB, MB according to the size |
||
473 | * |
||
474 | * @see self::returnBytes() |
||
475 | * @param int $size Number of the size |
||
476 | * @return string File size string |
||
477 | */ |
||
478 | function filesize($size) |
||
479 | { |
||
480 | if(!$size) |
||
481 | { |
||
482 | return '0Byte'; |
||
483 | } |
||
484 | |||
485 | if($size === 1) |
||
486 | { |
||
487 | return '1Byte'; |
||
488 | } |
||
489 | |||
490 | if($size < 1024) |
||
491 | { |
||
492 | return $size . 'Bytes'; |
||
493 | } |
||
494 | |||
495 | if($size >= 1024 && $size < 1024 * 1024) |
||
496 | { |
||
497 | return sprintf("%0.1fKB", $size / 1024); |
||
498 | } |
||
499 | |||
500 | return sprintf("%0.2fMB", $size / (1024 * 1024)); |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Return remote file's content via HTTP |
||
505 | * |
||
506 | * If the target is moved (when return code is 300~399), this function follows the location specified response header. |
||
507 | * |
||
508 | * @param string $url The address of the target file |
||
509 | * @param string $body HTTP request body |
||
510 | * @param int $timeout Connection timeout |
||
511 | * @param string $method GET/POST |
||
512 | * @param string $content_type Content type header of HTTP request |
||
513 | * @param string[] $headers Headers key value array. |
||
514 | * @param string[] $cookies Cookies key value array. |
||
515 | * @param string $post_data Request arguments array for POST method |
||
516 | * @return string If success, the content of the target file. Otherwise: none |
||
517 | */ |
||
518 | function getRemoteResource($url, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array(), $cookies = array(), $post_data = array(), $request_config = array()) |
||
615 | |||
616 | /** |
||
617 | * Retrieves remote file, then stores it into target path. |
||
618 | * |
||
619 | * @param string $url The address of the target file |
||
620 | * @param string $target_filename The location to store |
||
621 | * @param string $body HTTP request body |
||
622 | * @param string $timeout Connection timeout |
||
623 | * @param string $method GET/POST |
||
624 | * @param string $content_type Content type header of HTTP request |
||
625 | * @param string[] $headers Headers key value array. |
||
626 | * @return bool TRUE: success, FALSE: failed |
||
627 | */ |
||
628 | function getRemoteFile($url, $target_filename, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array(), $cookies = array(), $post_data = array(), $request_config = array()) |
||
629 | { |
||
630 | if(!($body = self::getRemoteResource($url, $body, $timeout, $method, $content_type, $headers,$cookies,$post_data,$request_config))) |
||
638 | |||
639 | /** |
||
640 | * Convert size in string into numeric value |
||
641 | * |
||
642 | * @see self::filesize() |
||
643 | * @param $val Size in string (ex., 10, 10K, 10M, 10G ) |
||
644 | * @return int converted size |
||
645 | */ |
||
646 | function returnBytes($val) |
||
660 | |||
661 | /** |
||
662 | * Check available memory to load image file |
||
663 | * |
||
664 | * @param array $imageInfo Image info retrieved by getimagesize function |
||
665 | * @return bool TRUE: it's ok, FALSE: otherwise |
||
666 | */ |
||
667 | function checkMemoryLoadImage(&$imageInfo) |
||
684 | |||
685 | /** |
||
686 | * Moves an image file (resizing is possible) |
||
687 | * |
||
688 | * @param string $source_file Path of the source file |
||
689 | * @param string $target_file Path of the target file |
||
690 | * @param int $resize_width Width to resize |
||
691 | * @param int $resize_height Height to resize |
||
692 | * @param string $target_type If $target_type is set (gif, jpg, png, bmp), result image will be saved as target type |
||
693 | * @param string $thumbnail_type Thumbnail type(crop, ratio) |
||
694 | * @return bool TRUE: success, FALSE: failed |
||
695 | */ |
||
696 | function createImageFile($source_file, $target_file, $resize_width = 0, $resize_height = 0, $target_type = '', $thumbnail_type = 'crop') |
||
891 | |||
892 | /** |
||
893 | * Reads ini file, and puts result into array |
||
894 | * |
||
895 | * @see self::writeIniFile() |
||
896 | * @param string $filename Path of the ini file |
||
897 | * @return array ini array (if the target file does not exist, it returns FALSE) |
||
898 | */ |
||
899 | function readIniFile($filename) |
||
915 | |||
916 | /** |
||
917 | * Write array into ini file |
||
918 | * |
||
919 | * $ini['key1'] = 'value1';<br/> |
||
920 | * $ini['key2'] = 'value2';<br/> |
||
921 | * $ini['section']['key1_in_section'] = 'value1_in_section';<br/> |
||
922 | * $ini['section']['key2_in_section'] = 'value2_in_section';<br/> |
||
923 | * self::writeIniFile('exmple.ini', $ini); |
||
924 | * |
||
925 | * @see self::readIniFile() |
||
926 | * @param string $filename Target ini file name |
||
927 | * @param array $arr Array |
||
928 | * @return bool if array contains nothing it returns FALSE, otherwise TRUE |
||
929 | */ |
||
930 | function writeIniFile($filename, $arr) |
||
939 | |||
940 | /** |
||
941 | * Make array to ini string |
||
942 | * |
||
943 | * @param array $arr Array |
||
944 | * @return string |
||
945 | */ |
||
946 | function _makeIniBuff($arr) |
||
973 | |||
974 | /** |
||
975 | * Returns a file object |
||
976 | * |
||
977 | * If the directory of the file does not exist, create it. |
||
978 | * |
||
979 | * @param string $filename Target file name |
||
980 | * @param string $mode File mode for fopen |
||
981 | * @return FileObject File object |
||
982 | */ |
||
983 | function openFile($filename, $mode) |
||
991 | |||
992 | /** |
||
993 | * Check whether the given file has the content. |
||
994 | * |
||
995 | * @param string $filename Target file name |
||
996 | * @return bool Returns TRUE if the file exists and contains something. |
||
997 | */ |
||
998 | function hasContent($filename) |
||
1002 | |||
1003 | /** |
||
1004 | * Check file exists. |
||
1005 | * |
||
1006 | * @param string $filename Target file name |
||
1007 | * @return bool Returns FALSE if the file does not exists, or Returns full path file(string). |
||
1008 | */ |
||
1009 | function exists($filename) |
||
1014 | |||
1015 | /** |
||
1016 | * Check it is dir |
||
1017 | * |
||
1018 | * @param string $dir Target dir path |
||
1019 | * @return bool Returns FALSE if the dir is not dir, or Returns full path of dir(string). |
||
1020 | */ |
||
1021 | function isDir($path) |
||
1026 | |||
1027 | /** |
||
1028 | * Check is writable dir |
||
1029 | * |
||
1030 | * @param string $path Target dir path |
||
1031 | * @return bool |
||
1032 | */ |
||
1033 | function isWritableDir($path) |
||
1053 | } |
||
1054 | |||
1057 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: