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 | * @param Member $member |
||
| 164 | */ |
||
| 165 | public function updateCachedName(Member $member) { |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $ident |
||
| 178 | * @param int $type |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public static function getDisplay($ident, $type) { |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $display |
||
| 194 | * @param string $ident |
||
| 195 | * @param int $type |
||
| 196 | */ |
||
| 197 | private static function getDisplayMember(&$display, $ident, $type) { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $display |
||
| 212 | * @param string $ident |
||
| 213 | * @param int $type |
||
| 214 | */ |
||
| 215 | private static function getDisplayContact(&$display, $ident, $type) { |
||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * @param $ident |
||
| 230 | * |
||
| 231 | * @return mixed|string |
||
| 232 | */ |
||
| 233 | public static function getContactData($ident) { |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $display |
||
| 259 | * @param array $contact |
||
| 260 | */ |
||
| 261 | private static function getDisplayContactFromArray(string &$display, array $contact) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * return Display Name if user exists and display name exists. |
||
| 281 | * returns Exception if user does not exist. |
||
| 282 | * |
||
| 283 | * However, with noException set to true, will return userId even if user does not exist |
||
| 284 | * |
||
| 285 | * @param $userId |
||
| 286 | * @param bool $noException |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | * @throws NoUserException |
||
| 290 | */ |
||
| 291 | public function getDisplayName($userId, $noException = false) { |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * @param array $options |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public static function generateClientBodyData($options = []) { |
||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * Hacky way to async the rest of the process without keeping client on hold. |
||
| 321 | * |
||
| 322 | * @param string $result |
||
| 323 | */ |
||
| 324 | public function asyncAndLeaveClientOutOfThis($result = '') { |
||
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * Generate uuid: 2b5a7a87-8db1-445f-a17b-405790f91c80 |
||
| 342 | * |
||
| 343 | * @param int $length |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | View Code Duplication | public function token(int $length = 0): string { |
|
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * @param Member $member |
||
| 365 | * |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | public function getInfosFromContact(Member $member) { |
||
| 378 | |||
| 379 | } |
||
| 380 | |||
| 381 |