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 File 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 File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class File |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * Read file content from local storage |
||
17 | * @param $path |
||
18 | * @return bool|string |
||
19 | */ |
||
20 | public static function read($path) |
||
30 | |||
31 | /** |
||
32 | * Check if $path is exist and readable in filesystem |
||
33 | * @param string $path |
||
34 | * @return bool |
||
35 | */ |
||
36 | public static function exist($path) |
||
41 | |||
42 | /** |
||
43 | * Alias for exist method |
||
44 | * @param string $path |
||
45 | * @return bool |
||
46 | */ |
||
47 | public static function readable($path) { |
||
50 | |||
51 | /** |
||
52 | * Check is file writable |
||
53 | * @param string $path |
||
54 | * @return bool |
||
55 | */ |
||
56 | View Code Duplication | public static function writable($path) |
|
66 | |||
67 | /** |
||
68 | * Check is file executable |
||
69 | * @param string $path |
||
70 | * @return bool |
||
71 | */ |
||
72 | public static function executable($path) |
||
82 | |||
83 | /** |
||
84 | * Write $content to file in $path |
||
85 | * @param string $path |
||
86 | * @param null|string $content |
||
87 | * @param null|int $flags |
||
88 | * @return int |
||
89 | */ |
||
90 | public static function write($path, $content = null, $flags = null) |
||
103 | |||
104 | /** |
||
105 | * Remove file |
||
106 | * @param string $path |
||
107 | * @return bool |
||
108 | */ |
||
109 | public static function remove($path) |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Alternative of functions include, require, include_once and etc in 1 function |
||
122 | * @param string $path |
||
123 | * @param bool|false $return |
||
124 | * @param bool|false $once |
||
125 | * @return bool|mixed |
||
126 | */ |
||
127 | public static function inc($path, $return = false, $once = false) |
||
141 | |||
142 | /** |
||
143 | * Get file make time in unix timestamp |
||
144 | * @param string $path |
||
145 | * @return int |
||
146 | */ |
||
147 | public static function mTime($path) |
||
156 | |||
157 | /** |
||
158 | * Recursive scan directory, based on $path and allowed extensions $ext or without it |
||
159 | * @param string $path |
||
160 | * @param array $ext |
||
161 | * @param bool $returnRelative |
||
162 | * @param $files |
||
163 | * @return array |
||
164 | */ |
||
165 | public static function listFiles($path, array $ext = null, $returnRelative = false, &$files = []) |
||
193 | |||
194 | /** |
||
195 | * Get file size in bytes |
||
196 | * @param string $path |
||
197 | * @return int |
||
198 | */ |
||
199 | public static function size($path) |
||
209 | |||
210 | /** |
||
211 | * Get file md5 hash |
||
212 | * @param string $path |
||
213 | * @return bool|string |
||
214 | */ |
||
215 | public static function getMd5($path) |
||
225 | |||
226 | /** |
||
227 | * Get data from remote $url by curl library |
||
228 | * @param string $url |
||
229 | * @return mixed|null|false |
||
230 | */ |
||
231 | public static function getFromUrl($url) |
||
258 | |||
259 | /** |
||
260 | * Download file from $url and save it into $path |
||
261 | * @param string $url |
||
262 | * @param string $path |
||
263 | * @return bool |
||
264 | */ |
||
265 | public static function saveFromUrl($url, $path) |
||
302 | |||
303 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.