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 DeprecatedCore 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 DeprecatedCore, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace EvolutionCMS\Legacy; |
||
| 5 | class DeprecatedCore implements DeprecatedCoreInterface |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @deprecated |
||
| 9 | * |
||
| 10 | * return @void |
||
| 11 | */ |
||
| 12 | public function dbConnect() |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @deprecated |
||
| 21 | * |
||
| 22 | * @param $sql |
||
| 23 | * @return bool|mysqli_result|resource |
||
|
|
|||
| 24 | */ |
||
| 25 | public function dbQuery($sql) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @deprecated |
||
| 34 | * |
||
| 35 | * @param $rs |
||
| 36 | * @return int |
||
| 37 | */ |
||
| 38 | public function recordCount($rs) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @deprecated |
||
| 47 | * |
||
| 48 | * @param $rs |
||
| 49 | * @param string $mode |
||
| 50 | * @return array|bool|mixed|object|stdClass |
||
| 51 | */ |
||
| 52 | public function fetchRow($rs, $mode = 'assoc') |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @deprecated |
||
| 61 | * |
||
| 62 | * @param $rs |
||
| 63 | * @return int |
||
| 64 | */ |
||
| 65 | public function affectedRows($rs) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @deprecated |
||
| 74 | * |
||
| 75 | * @param $rs |
||
| 76 | * @return int|mixed |
||
| 77 | */ |
||
| 78 | public function insertId($rs) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @deprecated |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public function dbClose() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @deprecated |
||
| 98 | * |
||
| 99 | * @param array $array |
||
| 100 | * @param string $ulroot |
||
| 101 | * @param string $ulprefix |
||
| 102 | * @param string $type |
||
| 103 | * @param bool $ordered |
||
| 104 | * @param int $tablevel |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function makeList($array, $ulroot = 'root', $ulprefix = 'sub_', $type = '', $ordered = false, $tablevel = 0) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @deprecated |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | public function getUserData() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns true, install or interact when inside manager |
||
| 152 | * |
||
| 153 | * @deprecated |
||
| 154 | * |
||
| 155 | * @return bool|string |
||
| 156 | */ |
||
| 157 | public function insideManager() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @deprecated |
||
| 176 | * |
||
| 177 | * @param $chunkName |
||
| 178 | * @return bool|string |
||
| 179 | */ |
||
| 180 | public function putChunk($chunkName) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @deprecated |
||
| 189 | * |
||
| 190 | * @return array|string |
||
| 191 | */ |
||
| 192 | public function getDocGroups() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @deprecated |
||
| 201 | * |
||
| 202 | * @param string $o |
||
| 203 | * @param string $n |
||
| 204 | * @return bool|string |
||
| 205 | */ |
||
| 206 | public function changePassword($o, $n) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @deprecated |
||
| 215 | * |
||
| 216 | * @return array|bool |
||
| 217 | */ |
||
| 218 | public function userLoggedIn() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @deprecated |
||
| 247 | * |
||
| 248 | * @param string $method |
||
| 249 | * @param string $prefix |
||
| 250 | * @param string $trim |
||
| 251 | * @param $REQUEST_METHOD |
||
| 252 | * @return array|bool |
||
| 253 | */ |
||
| 254 | public function getFormVars($method = "", $prefix = "", $trim = "", $REQUEST_METHOD) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Displays a javascript alert message in the web browser |
||
| 290 | * |
||
| 291 | * @deprecated |
||
| 292 | * |
||
| 293 | * @param string $msg Message to show |
||
| 294 | * @param string $url URL to redirect to |
||
| 295 | */ |
||
| 296 | public function webAlert($msg, $url = "") |
||
| 313 | |||
| 314 | ######################################## |
||
| 315 | // END New database functions - rad14701 |
||
| 316 | ######################################## |
||
| 317 | } |
||
| 318 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.