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 Admin 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 Admin, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace XoopsModules\Smallworld; |
||
| 23 | class Admin |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Get oldest message in Db |
||
| 27 | * @returns time |
||
| 28 | */ |
||
| 29 | View Code Duplication | public function oldestMsg() |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Get average messages sent per day |
||
| 47 | * @param int $totaldays |
||
| 48 | * @return int|string |
||
| 49 | */ |
||
| 50 | public function AvgMsgDay($totaldays) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * total users using smallworld |
||
| 64 | * @return int |
||
| 65 | */ |
||
| 66 | public function TotalUsers() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get version of module |
||
| 90 | * @returns string |
||
| 91 | */ |
||
| 92 | public function ModuleInstallVersion() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get date when Module was installed |
||
| 102 | * @return string|time |
||
| 103 | */ |
||
| 104 | public function ModuleInstallDate() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Count total days represented in db |
||
| 114 | * @return float|int|time |
||
| 115 | */ |
||
| 116 | public function CountDays() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * find user with most posted messages |
||
| 128 | * @returns array |
||
| 129 | */ |
||
| 130 | public function mostactiveusers_allround() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * find user with most posted messages in last 24 hours |
||
| 167 | * @returns array |
||
| 168 | */ |
||
| 169 | public function mostactiveusers_today() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Find best OR worst rated users |
||
| 208 | * @param string $direction |
||
| 209 | * @returns array |
||
| 210 | * @return array |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | public function topratedusers($direction) |
||
| 214 | { |
||
| 215 | global $xoopsUser, $xoopsDB, $xoopsTpl; |
||
| 216 | $array = []; |
||
| 217 | |||
| 218 | if ('up' === $direction) { |
||
| 219 | $sql = 'SELECT owner, count(*) AS cnt FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE up='1' GROUP BY owner ORDER BY cnt DESC LIMIT 20"; |
||
| 220 | $result = $xoopsDB->queryF($sql); |
||
| 221 | $count = $xoopsDB->getRowsNum($result); |
||
| 222 | $i = 1; |
||
| 223 | View Code Duplication | if ($count >= $i) { |
|
| 224 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 225 | $array['counter'][$i] = $i; |
||
| 226 | $array['img'][$i] = "<img height='10px' width='10px' " . "style='margin:0px 5px;' src = '../assets/images/like.png'>"; |
||
| 227 | if ($array['counter'][$i] > 3) { |
||
| 228 | $array['img'][$i] = ''; |
||
| 229 | } |
||
| 230 | $array['cnt'][$i] = $row['cnt']; |
||
| 231 | $array['user'][$i] = $xoopsUser->getUnameFromId($row['owner']); |
||
| 232 | ++$i; |
||
| 233 | } |
||
| 234 | } else { |
||
| 235 | $array = []; |
||
| 236 | } |
||
| 237 | } else { |
||
| 238 | $sql = 'SELECT owner, count(*) AS cnt FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE down='1' GROUP BY owner ORDER BY cnt DESC LIMIT 20"; |
||
| 239 | $result = $xoopsDB->queryF($sql); |
||
| 240 | $count = $xoopsDB->getRowsNum($result); |
||
| 241 | $i = 1; |
||
| 242 | View Code Duplication | if (0 != $count) { |
|
| 243 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 244 | $array['counter'][$i] = $i; |
||
| 245 | $array['img'][$i] = "<img height='10px' width='10px' " . "style='margin:0px 5px;' src = '../assets/images/dislike.png'>"; |
||
| 246 | if ($array['counter'][$i] > 3) { |
||
| 247 | $array['img'][$i] = ''; |
||
| 248 | } |
||
| 249 | $array['cnt'][$i] = $row['cnt']; |
||
| 250 | $array['user'][$i] = $xoopsUser->getUnameFromId($row['owner']); |
||
| 251 | ++$i; |
||
| 252 | } |
||
| 253 | } else { |
||
| 254 | $array = []; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | return $array; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get all users to loop in admin for administration |
||
| 263 | * @param string $inspect |
||
| 264 | * @returns array |
||
| 265 | * @return array |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | public function getAllUsers($inspect) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * check server if update is available |
||
| 291 | * Server currently at culex.dk |
||
| 292 | * Variable $version = current smallworld version number |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function doCheckUpdate() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Fetch content of comma separated text file |
||
| 335 | * will attempt to use the fopen method first, then curl, then socket |
||
| 336 | * @param string $url |
||
| 337 | * @param array $methods |
||
| 338 | * @returns string |
||
| 339 | * @return bool|false|string |
||
| 340 | * @return bool|false|string |
||
| 341 | */ |
||
| 342 | public function fetchURL($url, $methods = ['fopen', 'curl', 'socket']) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Smallworld_sanitize(array(array) ) |
||
| 424 | * flatten multidimentional arrays to one dimentional |
||
| 425 | * @param array $array |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | public function flatten($array) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Smallworld_sanitize($string) |
||
| 447 | * @param string $text |
||
| 448 | * @returns string |
||
| 449 | * @return string|string[] |
||
| 450 | * @return string|string[] |
||
| 451 | */ |
||
| 452 | public function Smallworld_sanitize($text) |
||
| 464 | } |
||
| 465 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.