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 |
||
| 42 | class MiscService { |
||
| 43 | |||
| 44 | |||
| 45 | use TArrayTools; |
||
| 46 | |||
| 47 | |||
| 48 | /** @var ILogger */ |
||
| 49 | private $logger; |
||
| 50 | |||
| 51 | /** @var IContactsStore */ |
||
| 52 | private $contactsStore; |
||
| 53 | |||
| 54 | /** @var string */ |
||
| 55 | private $appName; |
||
| 56 | |||
| 57 | /** @var IUserManager */ |
||
| 58 | private $userManager; |
||
| 59 | |||
| 60 | public function __construct( |
||
| 68 | |||
| 69 | public function log($message, $level = 4) { |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * @param Exception $e |
||
| 81 | */ |
||
| 82 | public function e(Exception $e) { |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * @param $arr |
||
| 89 | * @param $k |
||
| 90 | * |
||
| 91 | * @param string $default |
||
| 92 | * |
||
| 93 | * @return array|string |
||
| 94 | */ |
||
| 95 | public static function get($arr, $k, $default = '') { |
||
| 102 | |||
| 103 | |||
| 104 | public static function mustContains($data, $arr) { |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * @param $data |
||
| 119 | * |
||
| 120 | * @return DataResponse |
||
| 121 | */ |
||
| 122 | public function fail($data) { |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * @param $data |
||
| 134 | * |
||
| 135 | * @return DataResponse |
||
| 136 | */ |
||
| 137 | public function success($data) { |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * return the real userId, with its real case |
||
| 147 | * |
||
| 148 | * @param $userId |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | * @throws NoUserException |
||
| 152 | */ |
||
| 153 | public function getRealUserId($userId) { |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $ident |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function getContactDisplayName(string $ident): string { |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * @param string $ident |
||
| 207 | * @param int $type |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | * @deprecated |
||
| 211 | * |
||
| 212 | */ |
||
| 213 | public static function getDisplay($ident, $type) { |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $display |
||
| 225 | * @param string $ident |
||
| 226 | * @param int $type |
||
| 227 | */ |
||
| 228 | private static function getDisplayMember(&$display, $ident, $type) { |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * @param string $display |
||
| 243 | * @param string $ident |
||
| 244 | * @param int $type |
||
| 245 | */ |
||
| 246 | private static function getDisplayContact(&$display, $ident, $type) { |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * @param $ident |
||
| 261 | * |
||
| 262 | * @return mixed|string |
||
| 263 | * @deprecated |
||
| 264 | * |
||
| 265 | */ |
||
| 266 | public static function getContactData($ident) { |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $display |
||
| 292 | * @param array $contact |
||
| 293 | * |
||
| 294 | * @deprecated |
||
| 295 | */ |
||
| 296 | private static function getDisplayContactFromArray(string &$display, array $contact) { |
||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * return Display Name if user exists and display name exists. |
||
| 317 | * returns Exception if user does not exist. |
||
| 318 | * |
||
| 319 | * However, with noException set to true, will return userId even if user does not exist |
||
| 320 | * |
||
| 321 | * @param $userId |
||
| 322 | * @param bool $noException |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | * @throws NoUserException |
||
| 326 | */ |
||
| 327 | public function getDisplayName($userId, $noException = false) { |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * @param array $options |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | public static function generateClientBodyData($options = []) { |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * Hacky way to async the rest of the process without keeping client on hold. |
||
| 357 | * |
||
| 358 | * @param string $result |
||
| 359 | */ |
||
| 360 | public function asyncAndLeaveClientOutOfThis($result = '') { |
||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * Generate uuid: 2b5a7a87-8db1-445f-a17b-405790f91c80 |
||
| 378 | * |
||
| 379 | * @param int $length |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | View Code Duplication | public function token(int $length = 0): string { |
|
| 397 | |||
| 398 | |||
| 399 | /** |
||
| 400 | * @param Member $member |
||
| 401 | * |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | public function getInfosFromContact(Member $member) { |
||
| 414 | |||
| 415 | } |
||
| 416 | |||
| 417 |