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 $arr |
||
| 81 | * @param $k |
||
| 82 | * |
||
| 83 | * @param string $default |
||
| 84 | * |
||
| 85 | * @return array|string |
||
| 86 | */ |
||
| 87 | public static function get($arr, $k, $default = '') { |
||
| 94 | |||
| 95 | |||
| 96 | public static function mustContains($data, $arr) { |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * @param $data |
||
| 111 | * |
||
| 112 | * @return DataResponse |
||
| 113 | */ |
||
| 114 | public function fail($data) { |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @param $data |
||
| 126 | * |
||
| 127 | * @return DataResponse |
||
| 128 | */ |
||
| 129 | public function success($data) { |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * return the real userId, with its real case |
||
| 139 | * |
||
| 140 | * @param $userId |
||
| 141 | * |
||
| 142 | * @return string |
||
| 143 | * @throws NoUserException |
||
| 144 | */ |
||
| 145 | public function getRealUserId($userId) { |
||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $ident |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getContactDisplayName(string $ident): string { |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $ident |
||
| 200 | * @param int $type |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | * @deprecated |
||
| 204 | * |
||
| 205 | */ |
||
| 206 | public static function getDisplay($ident, $type) { |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $display |
||
| 218 | * @param string $ident |
||
| 219 | * @param int $type |
||
| 220 | */ |
||
| 221 | private static function getDisplayMember(&$display, $ident, $type) { |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $display |
||
| 236 | * @param string $ident |
||
| 237 | * @param int $type |
||
| 238 | */ |
||
| 239 | private static function getDisplayContact(&$display, $ident, $type) { |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * @param $ident |
||
| 254 | * |
||
| 255 | * @return mixed|string |
||
| 256 | * @deprecated |
||
| 257 | * |
||
| 258 | */ |
||
| 259 | public static function getContactData($ident) { |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $display |
||
| 285 | * @param array $contact |
||
| 286 | * |
||
| 287 | * @deprecated |
||
| 288 | */ |
||
| 289 | private static function getDisplayContactFromArray(string &$display, array $contact) { |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * return Display Name if user exists and display name exists. |
||
| 310 | * returns Exception if user does not exist. |
||
| 311 | * |
||
| 312 | * However, with noException set to true, will return userId even if user does not exist |
||
| 313 | * |
||
| 314 | * @param $userId |
||
| 315 | * @param bool $noException |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | * @throws NoUserException |
||
| 319 | */ |
||
| 320 | public function getDisplayName($userId, $noException = false) { |
||
| 332 | |||
| 333 | |||
| 334 | /** |
||
| 335 | * @param array $options |
||
| 336 | * |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | public static function generateClientBodyData($options = []) { |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * Hacky way to async the rest of the process without keeping client on hold. |
||
| 350 | * |
||
| 351 | * @param string $result |
||
| 352 | */ |
||
| 353 | public function asyncAndLeaveClientOutOfThis($result = '') { |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * Generate uuid: 2b5a7a87-8db1-445f-a17b-405790f91c80 |
||
| 371 | * |
||
| 372 | * @param int $length |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | View Code Duplication | public function token(int $length = 0): string { |
|
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * @param Member $member |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | public function getInfosFromContact(Member $member) { |
||
| 407 | |||
| 408 | } |
||
| 409 | |||
| 410 |