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) |
||
356 | |||
357 | /** |
||
358 | * Remove all files under the path |
||
359 | * |
||
360 | * @param string $path Path of the target directory |
||
361 | * @return void |
||
362 | */ |
||
363 | View Code Duplication | function removeDir($path) |
|
397 | |||
398 | /** |
||
399 | * Remove a directory only if it is empty |
||
400 | * |
||
401 | * @param string $path Path of the target directory |
||
402 | * @return void |
||
403 | */ |
||
404 | function removeBlankDir($path) |
||
429 | |||
430 | /** |
||
431 | * Remove files in the target directory |
||
432 | * |
||
433 | * This function keeps the directory structure. |
||
434 | * |
||
435 | * @param string $path Path of the target directory |
||
436 | * @return void |
||
437 | */ |
||
438 | View Code Duplication | function removeFilesInDir($path) |
|
472 | |||
473 | /** |
||
474 | * Makes file size byte into KB, MB according to the size |
||
475 | * |
||
476 | * @see self::returnBytes() |
||
477 | * @param int $size Number of the size |
||
478 | * @return string File size string |
||
479 | */ |
||
480 | function filesize($size) |
||
504 | |||
505 | /** |
||
506 | * Return remote file's content via HTTP |
||
507 | * |
||
508 | * If the target is moved (when return code is 300~399), this function follows the location specified response header. |
||
509 | * |
||
510 | * @param string $url The address of the target file |
||
511 | * @param string $body HTTP request body |
||
512 | * @param int $timeout Connection timeout |
||
513 | * @param string $method GET/POST |
||
514 | * @param string $content_type Content type header of HTTP request |
||
515 | * @param string[] $headers Headers key value array. |
||
516 | * @param string[] $cookies Cookies key value array. |
||
517 | * @param string $post_data Request arguments array for POST method |
||
518 | * @return string If success, the content of the target file. Otherwise: none |
||
519 | */ |
||
520 | function getRemoteResource($url, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array(), $cookies = array(), $post_data = array(), $request_config = array()) |
||
663 | |||
664 | /** |
||
665 | * Retrieves remote file, then stores it into target path. |
||
666 | * |
||
667 | * @param string $url The address of the target file |
||
668 | * @param string $target_filename The location to store |
||
669 | * @param string $body HTTP request body |
||
670 | * @param string $timeout Connection timeout |
||
671 | * @param string $method GET/POST |
||
672 | * @param string $content_type Content type header of HTTP request |
||
673 | * @param string[] $headers Headers key value array. |
||
674 | * @return bool TRUE: success, FALSE: failed |
||
675 | */ |
||
676 | function getRemoteFile($url, $target_filename, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array(), $cookies = array(), $post_data = array(), $request_config = array()) |
||
696 | |||
697 | /** |
||
698 | * Convert size in string into numeric value |
||
699 | * |
||
700 | * @see self::filesize() |
||
701 | * @param $val Size in string (ex., 10, 10K, 10M, 10G ) |
||
702 | * @return int converted size |
||
703 | */ |
||
704 | function returnBytes($val) |
||
718 | |||
719 | /** |
||
720 | * Check available memory to load image file |
||
721 | * |
||
722 | * @param array $imageInfo Image info retrieved by getimagesize function |
||
723 | * @return bool TRUE: it's ok, FALSE: otherwise |
||
724 | */ |
||
725 | function checkMemoryLoadImage(&$imageInfo) |
||
742 | |||
743 | /** |
||
744 | * Moves an image file (resizing is possible) |
||
745 | * |
||
746 | * @param string $source_file Path of the source file |
||
747 | * @param string $target_file Path of the target file |
||
748 | * @param int $resize_width Width to resize |
||
749 | * @param int $resize_height Height to resize |
||
750 | * @param string $target_type If $target_type is set (gif, jpg, png, bmp), result image will be saved as target type |
||
751 | * @param string $thumbnail_type Thumbnail type(crop, ratio) |
||
752 | * @return bool TRUE: success, FALSE: failed |
||
753 | */ |
||
754 | function createImageFile($source_file, $target_file, $resize_width = 0, $resize_height = 0, $target_type = '', $thumbnail_type = 'crop') |
||
949 | |||
950 | /** |
||
951 | * Reads ini file, and puts result into array |
||
952 | * |
||
953 | * @see self::writeIniFile() |
||
954 | * @param string $filename Path of the ini file |
||
955 | * @return array ini array (if the target file does not exist, it returns FALSE) |
||
956 | */ |
||
957 | function readIniFile($filename) |
||
973 | |||
974 | /** |
||
975 | * Write array into ini file |
||
976 | * |
||
977 | * $ini['key1'] = 'value1';<br/> |
||
978 | * $ini['key2'] = 'value2';<br/> |
||
979 | * $ini['section']['key1_in_section'] = 'value1_in_section';<br/> |
||
980 | * $ini['section']['key2_in_section'] = 'value2_in_section';<br/> |
||
981 | * self::writeIniFile('exmple.ini', $ini); |
||
982 | * |
||
983 | * @see self::readIniFile() |
||
984 | * @param string $filename Target ini file name |
||
985 | * @param array $arr Array |
||
986 | * @return bool if array contains nothing it returns FALSE, otherwise TRUE |
||
987 | */ |
||
988 | function writeIniFile($filename, $arr) |
||
997 | |||
998 | /** |
||
999 | * Make array to ini string |
||
1000 | * |
||
1001 | * @param array $arr Array |
||
1002 | * @return string |
||
1003 | */ |
||
1004 | function _makeIniBuff($arr) |
||
1031 | |||
1032 | /** |
||
1033 | * Returns a file object |
||
1034 | * |
||
1035 | * If the directory of the file does not exist, create it. |
||
1036 | * |
||
1037 | * @param string $filename Target file name |
||
1038 | * @param string $mode File mode for fopen |
||
1039 | * @return FileObject File object |
||
1040 | */ |
||
1041 | function openFile($filename, $mode) |
||
1049 | |||
1050 | /** |
||
1051 | * Check whether the given file has the content. |
||
1052 | * |
||
1053 | * @param string $filename Target file name |
||
1054 | * @return bool Returns TRUE if the file exists and contains something. |
||
1055 | */ |
||
1056 | function hasContent($filename) |
||
1060 | |||
1061 | /** |
||
1062 | * Check file exists. |
||
1063 | * |
||
1064 | * @param string $filename Target file name |
||
1065 | * @return bool Returns FALSE if the file does not exists, or Returns full path file(string). |
||
1066 | */ |
||
1067 | function exists($filename) |
||
1072 | |||
1073 | /** |
||
1074 | * Check it is dir |
||
1075 | * |
||
1076 | * @param string $dir Target dir path |
||
1077 | * @return bool Returns FALSE if the dir is not dir, or Returns full path of dir(string). |
||
1078 | */ |
||
1079 | function isDir($path) |
||
1084 | |||
1085 | /** |
||
1086 | * Check is writable dir |
||
1087 | * |
||
1088 | * @param string $path Target dir path |
||
1089 | * @return bool |
||
1090 | */ |
||
1091 | function isWritableDir($path) |
||
1111 | |||
1112 | /** |
||
1113 | * Clears file status cache |
||
1114 | * |
||
1115 | * @param string|array $target filename or directory |
||
1116 | * @param boolean $include include files in the directory |
||
1117 | **/ |
||
1118 | static public function clearStatCache($target, $include = false) |
||
1135 | |||
1136 | /** |
||
1137 | * Invalidates a cached script of OPcache |
||
1138 | * |
||
1139 | * @param string|array $target filename or directory |
||
1140 | * @param boolean $force force |
||
1141 | **/ |
||
1142 | static public function invalidateOpcache($target, $force = true) |
||
1171 | } |
||
1172 | |||
1175 |
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: