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") |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Remove a file |
||
| 165 | * |
||
| 166 | * @param string $filename path of target file |
||
| 167 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 168 | */ |
||
| 169 | function removeFile($filename) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Rename a file |
||
| 176 | * |
||
| 177 | * In order to move a file, use this function. |
||
| 178 | * |
||
| 179 | * @param string $source Path of source file |
||
| 180 | * @param string $target Path of target file |
||
| 181 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 182 | */ |
||
| 183 | function rename($source, $target) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Move a file |
||
| 190 | * |
||
| 191 | * @param string $source Path of source file |
||
| 192 | * @param string $target Path of target file |
||
| 193 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 194 | */ |
||
| 195 | function moveFile($source, $target) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Move a directory |
||
| 207 | * |
||
| 208 | * This function just wraps rename function. |
||
| 209 | * |
||
| 210 | * @param string $source_dir Path of source directory |
||
| 211 | * @param string $target_dir Path of target directory |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | function moveDir($source_dir, $target_dir) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Return list of the files in the path |
||
| 221 | * |
||
| 222 | * The array does not contain files, such as '.', '..', and files starting with '.' |
||
| 223 | * |
||
| 224 | * @param string $path Path of target directory |
||
| 225 | * @param string $filter If specified, return only files matching with the filter |
||
| 226 | * @param bool $to_lower If TRUE, file names will be changed into lower case. |
||
| 227 | * @param bool $concat_prefix If TRUE, return file name as absolute path |
||
| 228 | * @return string[] Array of the filenames in the path |
||
| 229 | */ |
||
| 230 | function readDir($path, $filter = '', $to_lower = FALSE, $concat_prefix = FALSE) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Creates a directory |
||
| 276 | * |
||
| 277 | * This function creates directories recursively, which means that if ancestors of the target directory does not exist, they will be created too. |
||
| 278 | * |
||
| 279 | * @param string $path_string Path of target directory |
||
| 280 | * @return bool TRUE if success. It might return nothing when ftp is used and connection to the ftp address failed. |
||
| 281 | */ |
||
| 282 | function makeDir($path_string) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Remove all files under the path |
||
| 361 | * |
||
| 362 | * @param string $path Path of the target directory |
||
| 363 | * @return void |
||
| 364 | */ |
||
| 365 | View Code Duplication | function removeDir($path) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Remove a directory only if it is empty |
||
| 402 | * |
||
| 403 | * @param string $path Path of the target directory |
||
| 404 | * @return void |
||
| 405 | */ |
||
| 406 | function removeBlankDir($path) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Remove files in the target directory |
||
| 434 | * |
||
| 435 | * This function keeps the directory structure. |
||
| 436 | * |
||
| 437 | * @param string $path Path of the target directory |
||
| 438 | * @return void |
||
| 439 | */ |
||
| 440 | View Code Duplication | function removeFilesInDir($path) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Makes file size byte into KB, MB according to the size |
||
| 477 | * |
||
| 478 | * @see self::returnBytes() |
||
| 479 | * @param int $size Number of the size |
||
| 480 | * @return string File size string |
||
| 481 | */ |
||
| 482 | function filesize($size) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Return remote file's content via HTTP |
||
| 509 | * |
||
| 510 | * If the target is moved (when return code is 300~399), this function follows the location specified response header. |
||
| 511 | * |
||
| 512 | * @param string $url The address of the target file |
||
| 513 | * @param string $body HTTP request body |
||
| 514 | * @param int $timeout Connection timeout |
||
| 515 | * @param string $method GET/POST |
||
| 516 | * @param string $content_type Content type header of HTTP request |
||
| 517 | * @param string[] $headers Headers key value array. |
||
| 518 | * @param string[] $cookies Cookies key value array. |
||
| 519 | * @param string $post_data Request arguments array for POST method |
||
| 520 | * @return string If success, the content of the target file. Otherwise: none |
||
| 521 | */ |
||
| 522 | function getRemoteResource($url, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array(), $cookies = array(), $post_data = array(), $request_config = array()) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Retrieves remote file, then stores it into target path. |
||
| 668 | * |
||
| 669 | * @param string $url The address of the target file |
||
| 670 | * @param string $target_filename The location to store |
||
| 671 | * @param string $body HTTP request body |
||
| 672 | * @param string $timeout Connection timeout |
||
| 673 | * @param string $method GET/POST |
||
| 674 | * @param string $content_type Content type header of HTTP request |
||
| 675 | * @param string[] $headers Headers key value array. |
||
| 676 | * @return bool TRUE: success, FALSE: failed |
||
| 677 | */ |
||
| 678 | function getRemoteFile($url, $target_filename, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array(), $cookies = array(), $post_data = array(), $request_config = array()) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Convert size in string into numeric value |
||
| 703 | * |
||
| 704 | * @see self::filesize() |
||
| 705 | * @param $val Size in string (ex., 10, 10K, 10M, 10G ) |
||
| 706 | * @return int converted size |
||
| 707 | */ |
||
| 708 | function returnBytes($val) |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Check available memory to load image file |
||
| 725 | * |
||
| 726 | * @param array $imageInfo Image info retrieved by getimagesize function |
||
| 727 | * @return bool TRUE: it's ok, FALSE: otherwise |
||
| 728 | */ |
||
| 729 | function checkMemoryLoadImage(&$imageInfo) |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Moves an image file (resizing is possible) |
||
| 757 | * |
||
| 758 | * @param string $source_file Path of the source file |
||
| 759 | * @param string $target_file Path of the target file |
||
| 760 | * @param int $resize_width Width to resize |
||
| 761 | * @param int $resize_height Height to resize |
||
| 762 | * @param string $target_type If $target_type is set (gif, jpg, png, bmp), result image will be saved as target type |
||
| 763 | * @param string $thumbnail_type Thumbnail type(crop, ratio) |
||
| 764 | * @param bool $thumbnail_transparent If $target_type is png, set background set transparent color |
||
| 765 | * @return bool TRUE: success, FALSE: failed |
||
| 766 | */ |
||
| 767 | function createImageFile($source_file, $target_file, $resize_width = 0, $resize_height = 0, $target_type = '', $thumbnail_type = 'crop', $thumbnail_transparent = FALSE) |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Reads ini file, and puts result into array |
||
| 982 | * |
||
| 983 | * @see self::writeIniFile() |
||
| 984 | * @param string $filename Path of the ini file |
||
| 985 | * @return array ini array (if the target file does not exist, it returns FALSE) |
||
| 986 | */ |
||
| 987 | function readIniFile($filename) |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Write array into ini file |
||
| 1006 | * |
||
| 1007 | * $ini['key1'] = 'value1';<br/> |
||
| 1008 | * $ini['key2'] = 'value2';<br/> |
||
| 1009 | * $ini['section']['key1_in_section'] = 'value1_in_section';<br/> |
||
| 1010 | * $ini['section']['key2_in_section'] = 'value2_in_section';<br/> |
||
| 1011 | * self::writeIniFile('exmple.ini', $ini); |
||
| 1012 | * |
||
| 1013 | * @see self::readIniFile() |
||
| 1014 | * @param string $filename Target ini file name |
||
| 1015 | * @param array $arr Array |
||
| 1016 | * @return bool if array contains nothing it returns FALSE, otherwise TRUE |
||
| 1017 | */ |
||
| 1018 | function writeIniFile($filename, $arr) |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Make array to ini string |
||
| 1030 | * |
||
| 1031 | * @param array $arr Array |
||
| 1032 | * @return string |
||
| 1033 | */ |
||
| 1034 | function _makeIniBuff($arr) |
||
| 1061 | |||
| 1062 | /** |
||
| 1063 | * Returns a file object |
||
| 1064 | * |
||
| 1065 | * If the directory of the file does not exist, create it. |
||
| 1066 | * |
||
| 1067 | * @param string $filename Target file name |
||
| 1068 | * @param string $mode File mode for fopen |
||
| 1069 | * @return FileObject File object |
||
| 1070 | */ |
||
| 1071 | function openFile($filename, $mode) |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * Check whether the given file has the content. |
||
| 1082 | * |
||
| 1083 | * @param string $filename Target file name |
||
| 1084 | * @return bool Returns TRUE if the file exists and contains something. |
||
| 1085 | */ |
||
| 1086 | function hasContent($filename) |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Check file exists. |
||
| 1093 | * |
||
| 1094 | * @param string $filename Target file name |
||
| 1095 | * @return bool Returns FALSE if the file does not exists, or Returns full path file(string). |
||
| 1096 | */ |
||
| 1097 | function exists($filename) |
||
| 1102 | |||
| 1103 | /** |
||
| 1104 | * Check it is dir |
||
| 1105 | * |
||
| 1106 | * @param string $dir Target dir path |
||
| 1107 | * @return bool Returns FALSE if the dir is not dir, or Returns full path of dir(string). |
||
| 1108 | */ |
||
| 1109 | function isDir($path) |
||
| 1114 | |||
| 1115 | /** |
||
| 1116 | * Check is writable dir |
||
| 1117 | * |
||
| 1118 | * @param string $path Target dir path |
||
| 1119 | * @return bool |
||
| 1120 | */ |
||
| 1121 | function isWritableDir($path) |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Clears file status cache |
||
| 1144 | * |
||
| 1145 | * @param string|array $target filename or directory |
||
| 1146 | * @param boolean $include include files in the directory |
||
| 1147 | **/ |
||
| 1148 | static public function clearStatCache($target, $include = false) |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Invalidates a cached script of OPcache |
||
| 1168 | * |
||
| 1169 | * @param string|array $target filename or directory |
||
| 1170 | * @param boolean $force force |
||
| 1171 | **/ |
||
| 1172 | static public function invalidateOpcache($target, $force = true) |
||
| 1201 | } |
||
| 1202 | |||
| 1205 |
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: