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()) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Retrieves remote file, then stores it into target path. |
||
| 637 | * |
||
| 638 | * @param string $url The address of the target file |
||
| 639 | * @param string $target_filename The location to store |
||
| 640 | * @param string $body HTTP request body |
||
| 641 | * @param string $timeout Connection timeout |
||
| 642 | * @param string $method GET/POST |
||
| 643 | * @param string $content_type Content type header of HTTP request |
||
| 644 | * @param string[] $headers Headers key value array. |
||
| 645 | * @return bool TRUE: success, FALSE: failed |
||
| 646 | */ |
||
| 647 | function getRemoteFile($url, $target_filename, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array(), $cookies = array(), $post_data = array(), $request_config = array()) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Convert size in string into numeric value |
||
| 670 | * |
||
| 671 | * @see self::filesize() |
||
| 672 | * @param $val Size in string (ex., 10, 10K, 10M, 10G ) |
||
| 673 | * @return int converted size |
||
| 674 | */ |
||
| 675 | function returnBytes($val) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Check available memory to load image file |
||
| 692 | * |
||
| 693 | * @param array $imageInfo Image info retrieved by getimagesize function |
||
| 694 | * @return bool TRUE: it's ok, FALSE: otherwise |
||
| 695 | */ |
||
| 696 | function checkMemoryLoadImage(&$imageInfo) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Moves an image file (resizing is possible) |
||
| 716 | * |
||
| 717 | * @param string $source_file Path of the source file |
||
| 718 | * @param string $target_file Path of the target file |
||
| 719 | * @param int $resize_width Width to resize |
||
| 720 | * @param int $resize_height Height to resize |
||
| 721 | * @param string $target_type If $target_type is set (gif, jpg, png, bmp), result image will be saved as target type |
||
| 722 | * @param string $thumbnail_type Thumbnail type(crop, ratio) |
||
| 723 | * @return bool TRUE: success, FALSE: failed |
||
| 724 | */ |
||
| 725 | function createImageFile($source_file, $target_file, $resize_width = 0, $resize_height = 0, $target_type = '', $thumbnail_type = 'crop') |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Reads ini file, and puts result into array |
||
| 923 | * |
||
| 924 | * @see self::writeIniFile() |
||
| 925 | * @param string $filename Path of the ini file |
||
| 926 | * @return array ini array (if the target file does not exist, it returns FALSE) |
||
| 927 | */ |
||
| 928 | function readIniFile($filename) |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Write array into ini file |
||
| 947 | * |
||
| 948 | * $ini['key1'] = 'value1';<br/> |
||
| 949 | * $ini['key2'] = 'value2';<br/> |
||
| 950 | * $ini['section']['key1_in_section'] = 'value1_in_section';<br/> |
||
| 951 | * $ini['section']['key2_in_section'] = 'value2_in_section';<br/> |
||
| 952 | * self::writeIniFile('exmple.ini', $ini); |
||
| 953 | * |
||
| 954 | * @see self::readIniFile() |
||
| 955 | * @param string $filename Target ini file name |
||
| 956 | * @param array $arr Array |
||
| 957 | * @return bool if array contains nothing it returns FALSE, otherwise TRUE |
||
| 958 | */ |
||
| 959 | function writeIniFile($filename, $arr) |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Make array to ini string |
||
| 971 | * |
||
| 972 | * @param array $arr Array |
||
| 973 | * @return string |
||
| 974 | */ |
||
| 975 | function _makeIniBuff($arr) |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Returns a file object |
||
| 1005 | * |
||
| 1006 | * If the directory of the file does not exist, create it. |
||
| 1007 | * |
||
| 1008 | * @param string $filename Target file name |
||
| 1009 | * @param string $mode File mode for fopen |
||
| 1010 | * @return FileObject File object |
||
| 1011 | */ |
||
| 1012 | function openFile($filename, $mode) |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Check whether the given file has the content. |
||
| 1023 | * |
||
| 1024 | * @param string $filename Target file name |
||
| 1025 | * @return bool Returns TRUE if the file exists and contains something. |
||
| 1026 | */ |
||
| 1027 | function hasContent($filename) |
||
| 1031 | |||
| 1032 | /** |
||
| 1033 | * Check file exists. |
||
| 1034 | * |
||
| 1035 | * @param string $filename Target file name |
||
| 1036 | * @return bool Returns FALSE if the file does not exists, or Returns full path file(string). |
||
| 1037 | */ |
||
| 1038 | function exists($filename) |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Check it is dir |
||
| 1046 | * |
||
| 1047 | * @param string $dir Target dir path |
||
| 1048 | * @return bool Returns FALSE if the dir is not dir, or Returns full path of dir(string). |
||
| 1049 | */ |
||
| 1050 | function isDir($path) |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Check is writable dir |
||
| 1058 | * |
||
| 1059 | * @param string $path Target dir path |
||
| 1060 | * @return bool |
||
| 1061 | */ |
||
| 1062 | function isWritableDir($path) |
||
| 1082 | } |
||
| 1083 | |||
| 1086 |
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: