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) |
||
| 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) |
||
| 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') |
||
| 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) |
||
| 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") |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
|
| 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) |
||
| 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) |
|
| 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) |
||
| 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()) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Retrieves remote file, then stores it into target path. |
||
| 633 | * |
||
| 634 | * @param string $url The address of the target file |
||
| 635 | * @param string $target_filename The location to store |
||
| 636 | * @param string $body HTTP request body |
||
| 637 | * @param string $timeout Connection timeout |
||
| 638 | * @param string $method GET/POST |
||
| 639 | * @param string $content_type Content type header of HTTP request |
||
| 640 | * @param string[] $headers Headers key value array. |
||
| 641 | * @return bool TRUE: success, FALSE: failed |
||
| 642 | */ |
||
| 643 | function getRemoteFile($url, $target_filename, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array(), $cookies = array(), $post_data = array(), $request_config = array()) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Convert size in string into numeric value |
||
| 656 | * |
||
| 657 | * @see self::filesize() |
||
| 658 | * @param $val Size in string (ex., 10, 10K, 10M, 10G ) |
||
| 659 | * @return int converted size |
||
| 660 | */ |
||
| 661 | function returnBytes($val) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Check available memory to load image file |
||
| 678 | * |
||
| 679 | * @param array $imageInfo Image info retrieved by getimagesize function |
||
| 680 | * @return bool TRUE: it's ok, FALSE: otherwise |
||
| 681 | */ |
||
| 682 | function checkMemoryLoadImage(&$imageInfo) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Moves an image file (resizing is possible) |
||
| 702 | * |
||
| 703 | * @param string $source_file Path of the source file |
||
| 704 | * @param string $target_file Path of the target file |
||
| 705 | * @param int $resize_width Width to resize |
||
| 706 | * @param int $resize_height Height to resize |
||
| 707 | * @param string $target_type If $target_type is set (gif, jpg, png, bmp), result image will be saved as target type |
||
| 708 | * @param string $thumbnail_type Thumbnail type(crop, ratio) |
||
| 709 | * @return bool TRUE: success, FALSE: failed |
||
| 710 | */ |
||
| 711 | function createImageFile($source_file, $target_file, $resize_width = 0, $resize_height = 0, $target_type = '', $thumbnail_type = 'crop') |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Reads ini file, and puts result into array |
||
| 909 | * |
||
| 910 | * @see self::writeIniFile() |
||
| 911 | * @param string $filename Path of the ini file |
||
| 912 | * @return array ini array (if the target file does not exist, it returns FALSE) |
||
| 913 | */ |
||
| 914 | function readIniFile($filename) |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Write array into ini file |
||
| 933 | * |
||
| 934 | * $ini['key1'] = 'value1';<br/> |
||
| 935 | * $ini['key2'] = 'value2';<br/> |
||
| 936 | * $ini['section']['key1_in_section'] = 'value1_in_section';<br/> |
||
| 937 | * $ini['section']['key2_in_section'] = 'value2_in_section';<br/> |
||
| 938 | * self::writeIniFile('exmple.ini', $ini); |
||
| 939 | * |
||
| 940 | * @see self::readIniFile() |
||
| 941 | * @param string $filename Target ini file name |
||
| 942 | * @param array $arr Array |
||
| 943 | * @return bool if array contains nothing it returns FALSE, otherwise TRUE |
||
| 944 | */ |
||
| 945 | function writeIniFile($filename, $arr) |
||
| 954 | |||
| 955 | /** |
||
| 956 | * Make array to ini string |
||
| 957 | * |
||
| 958 | * @param array $arr Array |
||
| 959 | * @return string |
||
| 960 | */ |
||
| 961 | function _makeIniBuff($arr) |
||
| 988 | |||
| 989 | /** |
||
| 990 | * Returns a file object |
||
| 991 | * |
||
| 992 | * If the directory of the file does not exist, create it. |
||
| 993 | * |
||
| 994 | * @param string $filename Target file name |
||
| 995 | * @param string $mode File mode for fopen |
||
| 996 | * @return FileObject File object |
||
| 997 | */ |
||
| 998 | function openFile($filename, $mode) |
||
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Check whether the given file has the content. |
||
| 1009 | * |
||
| 1010 | * @param string $filename Target file name |
||
| 1011 | * @return bool Returns TRUE if the file exists and contains something. |
||
| 1012 | */ |
||
| 1013 | function hasContent($filename) |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Check file exists. |
||
| 1020 | * |
||
| 1021 | * @param string $filename Target file name |
||
| 1022 | * @return bool Returns FALSE if the file does not exists, or Returns full path file(string). |
||
| 1023 | */ |
||
| 1024 | function exists($filename) |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Check it is dir |
||
| 1032 | * |
||
| 1033 | * @param string $dir Target dir path |
||
| 1034 | * @return bool Returns FALSE if the dir is not dir, or Returns full path of dir(string). |
||
| 1035 | */ |
||
| 1036 | function isDir($path) |
||
| 1041 | |||
| 1042 | /** |
||
| 1043 | * Check is writable dir |
||
| 1044 | * |
||
| 1045 | * @param string $path Target dir path |
||
| 1046 | * @return bool |
||
| 1047 | */ |
||
| 1048 | function isWritableDir($path) |
||
| 1068 | } |
||
| 1069 | |||
| 1072 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: