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 MiscService 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 MiscService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class MiscService { |
||
| 42 | |||
| 43 | /** @var ILogger */ |
||
| 44 | private $logger; |
||
| 45 | |||
| 46 | /** @var IContactsStore */ |
||
| 47 | private $contactsStore; |
||
| 48 | |||
| 49 | /** @var string */ |
||
| 50 | private $appName; |
||
| 51 | |||
| 52 | /** @var IUserManager */ |
||
| 53 | private $userManager; |
||
| 54 | |||
| 55 | public function __construct( |
||
| 63 | |||
| 64 | public function log($message, $level = 4) { |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * @param $arr |
||
| 76 | * @param $k |
||
| 77 | * |
||
| 78 | * @param string $default |
||
| 79 | * |
||
| 80 | * @return array|string |
||
| 81 | */ |
||
| 82 | public static function get($arr, $k, $default = '') { |
||
| 89 | |||
| 90 | |||
| 91 | public static function mustContains($data, $arr) { |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @param $data |
||
| 106 | * |
||
| 107 | * @return DataResponse |
||
| 108 | */ |
||
| 109 | public function fail($data) { |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @param $data |
||
| 121 | * |
||
| 122 | * @return DataResponse |
||
| 123 | */ |
||
| 124 | public function success($data) { |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * return the real userId, with its real case |
||
| 134 | * |
||
| 135 | * @param $userId |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | * @throws NoUserException |
||
| 139 | */ |
||
| 140 | public function getRealUserId($userId) { |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * @param Member $member |
||
| 159 | */ |
||
| 160 | public function updateCachedName(Member $member) { |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $ident |
||
| 171 | * @param int $type |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public static function getDisplay($ident, $type) { |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $display |
||
| 187 | * @param string $ident |
||
| 188 | * @param int $type |
||
| 189 | */ |
||
| 190 | private static function getDisplayMember(&$display, $ident, $type) { |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $display |
||
| 205 | * @param string $ident |
||
| 206 | * @param int $type |
||
| 207 | */ |
||
| 208 | private static function getDisplayContact(&$display, $ident, $type) { |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * @param $ident |
||
| 223 | * |
||
| 224 | * @return mixed|string |
||
| 225 | */ |
||
| 226 | public static function getContactData($ident) { |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $display |
||
| 252 | * @param array $contact |
||
| 253 | */ |
||
| 254 | private static function getDisplayContactFromArray(string &$display, array $contact) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * return Display Name if user exists and display name exists. |
||
| 274 | * returns Exception if user does not exist. |
||
| 275 | * |
||
| 276 | * However, with noException set to true, will return userId even if user does not exist |
||
| 277 | * |
||
| 278 | * @param $userId |
||
| 279 | * @param bool $noException |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | * @throws NoUserException |
||
| 283 | */ |
||
| 284 | public function getDisplayName($userId, $noException = false) { |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * @param array $options |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public static function generateClientBodyData($options = []) { |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Hacky way to async the rest of the process without keeping client on hold. |
||
| 314 | * |
||
| 315 | * @param string $result |
||
| 316 | */ |
||
| 317 | public function asyncAndLeaveClientOutOfThis($result = '') { |
||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * Generate uuid: 2b5a7a87-8db1-445f-a17b-405790f91c80 |
||
| 335 | * |
||
| 336 | * @param int $length |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | View Code Duplication | public function token(int $length = 0): string { |
|
| 354 | |||
| 355 | } |
||
| 356 | |||
| 357 |