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 Member 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 Member, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class Member extends ManagedModel implements IFederatedUser, IDeserializable, INC21QueryRow, JsonSerializable { |
||
| 53 | |||
| 54 | |||
| 55 | use TArrayTools; |
||
| 56 | use TNC21Deserialize; |
||
| 57 | |||
| 58 | |||
| 59 | const LEVEL_NONE = 0; |
||
| 60 | const LEVEL_MEMBER = 1; |
||
| 61 | const LEVEL_MODERATOR = 4; |
||
| 62 | const LEVEL_ADMIN = 8; |
||
| 63 | const LEVEL_OWNER = 9; |
||
| 64 | |||
| 65 | const TYPE_CIRCLE = 16; |
||
| 66 | const TYPE_SINGLE = 8; |
||
| 67 | const TYPE_USER = 1; |
||
| 68 | const TYPE_GROUP = 2; |
||
| 69 | const TYPE_MAIL = 3; |
||
| 70 | const TYPE_CONTACT = 4; |
||
| 71 | |||
| 72 | const STATUS_NONMEMBER = 'Unknown'; |
||
| 73 | const STATUS_INVITED = 'Invited'; |
||
| 74 | const STATUS_REQUEST = 'Requesting'; |
||
| 75 | const STATUS_MEMBER = 'Member'; |
||
| 76 | const STATUS_BLOCKED = 'Blocked'; |
||
| 77 | const STATUS_KICKED = 'Kicked'; |
||
| 78 | |||
| 79 | |||
| 80 | public static $DEF_LEVEL = [ |
||
| 81 | 1 => 'Member', |
||
| 82 | 4 => 'Moderator', |
||
| 83 | 8 => 'Admin', |
||
| 84 | 9 => 'Owner' |
||
| 85 | ]; |
||
| 86 | |||
| 87 | public static $DEF_TYPE = [ |
||
| 88 | 1 => 'user', |
||
| 89 | 16 => 'circle', |
||
| 90 | 8 => 'single', |
||
| 91 | 3 => 'mail', |
||
| 92 | 4 => 'contact', |
||
| 93 | ]; |
||
| 94 | |||
| 95 | /** @var string */ |
||
| 96 | private $id = ''; |
||
| 97 | |||
| 98 | /** @var string */ |
||
| 99 | private $circleId = ''; |
||
| 100 | |||
| 101 | /** @var string */ |
||
| 102 | private $singleId = ''; |
||
| 103 | |||
| 104 | /** @var string */ |
||
| 105 | private $userId = ''; |
||
| 106 | |||
| 107 | /** @var int */ |
||
| 108 | private $userType = self::TYPE_USER; |
||
| 109 | |||
| 110 | /** @var string */ |
||
| 111 | private $instance = ''; |
||
| 112 | |||
| 113 | /** @var bool */ |
||
| 114 | private $local = false; |
||
| 115 | |||
| 116 | /** @var int */ |
||
| 117 | private $level = 0; |
||
| 118 | |||
| 119 | /** @var string */ |
||
| 120 | private $status = 'Unknown'; |
||
| 121 | |||
| 122 | /** @var string */ |
||
| 123 | private $note = ''; |
||
| 124 | |||
| 125 | /** @var string */ |
||
| 126 | private $displayName = ''; |
||
| 127 | |||
| 128 | /** @var int */ |
||
| 129 | private $displayUpdate = 0; |
||
| 130 | |||
| 131 | /** @var string */ |
||
| 132 | private $contactId = ''; |
||
| 133 | |||
| 134 | /** @var string */ |
||
| 135 | private $contactMeta = ''; |
||
| 136 | |||
| 137 | /** @var Circle */ |
||
| 138 | private $circle; |
||
| 139 | |||
| 140 | |||
| 141 | /** @var int */ |
||
| 142 | private $joined = 0; |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Member constructor. |
||
| 147 | */ |
||
| 148 | public function __construct() { |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string $id |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function setId(string $id): self { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getId(): string { |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $circleId |
||
| 173 | * |
||
| 174 | * @return Member |
||
| 175 | */ |
||
| 176 | public function setCircleId(string $circleId): self { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getCircleId(): string { |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * This should replace user_id, user_type and instance; and will use the data from Circle with |
||
| 192 | * Config=CFG_SINGLE |
||
| 193 | * |
||
| 194 | * @param string $singleId |
||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function setSingleId(string $singleId): self { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getSingleId(): string { |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $userId |
||
| 214 | * |
||
| 215 | * @return Member |
||
| 216 | */ |
||
| 217 | public function setUserId(string $userId): self { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getUserId(): string { |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * @param int $userType |
||
| 236 | * |
||
| 237 | * @return Member |
||
| 238 | */ |
||
| 239 | public function setUserType(int $userType): self { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return int |
||
| 247 | */ |
||
| 248 | public function getUserType(): int { |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $instance |
||
| 255 | * |
||
| 256 | * @return Member |
||
| 257 | */ |
||
| 258 | public function setInstance(string $instance): self { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function getInstance(): string { |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * @param bool $local |
||
| 274 | * |
||
| 275 | * @return Member |
||
| 276 | */ |
||
| 277 | public function setLocal(bool $local): self { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function isLocal(): bool { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @param int $level |
||
| 293 | * |
||
| 294 | * @return Member |
||
| 295 | */ |
||
| 296 | public function setLevel(int $level): self { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return int |
||
| 304 | */ |
||
| 305 | public function getLevel(): int { |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $status |
||
| 312 | * |
||
| 313 | * @return Member |
||
| 314 | */ |
||
| 315 | public function setStatus(string $status): self { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function getStatus(): string { |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $note |
||
| 331 | * |
||
| 332 | * @return Member |
||
| 333 | */ |
||
| 334 | public function setNote(string $note): self { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | public function getNote(): string { |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $displayName |
||
| 350 | * |
||
| 351 | * @return Member |
||
| 352 | */ |
||
| 353 | public function setDisplayName(string $displayName): self { |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * @param int $displayUpdate |
||
| 364 | * |
||
| 365 | * @return Member |
||
| 366 | */ |
||
| 367 | public function setDisplayUpdate(int $displayUpdate): self { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return int |
||
| 375 | */ |
||
| 376 | public function getDisplayUpdate(): int { |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | public function getDisplayName(): string { |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $contactId |
||
| 391 | * |
||
| 392 | * @return Member |
||
| 393 | */ |
||
| 394 | public function setContactId(string $contactId): self { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function getContactId(): string { |
||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * @param string $contactMeta |
||
| 410 | * |
||
| 411 | * @return Member |
||
| 412 | */ |
||
| 413 | public function setContactMeta(string $contactMeta): self { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | public function getContactMeta(): string { |
||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * @param Circle $circle |
||
| 429 | * |
||
| 430 | * @return self |
||
| 431 | */ |
||
| 432 | public function setCircle(Circle $circle): self { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return Circle |
||
| 440 | */ |
||
| 441 | public function getCircle(): Circle { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return bool |
||
| 447 | */ |
||
| 448 | public function hasCircle(): bool { |
||
| 451 | |||
| 452 | |||
| 453 | /** |
||
| 454 | * @param int $joined |
||
| 455 | * |
||
| 456 | * @return Member |
||
| 457 | */ |
||
| 458 | public function setJoined(int $joined): self { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return int |
||
| 466 | */ |
||
| 467 | public function getJoined(): int { |
||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * @return bool |
||
| 474 | */ |
||
| 475 | public function isMember(): bool { |
||
| 478 | |||
| 479 | |||
| 480 | /** |
||
| 481 | * @param Member $member |
||
| 482 | * @param bool $full |
||
| 483 | * |
||
| 484 | * @return bool |
||
| 485 | */ |
||
| 486 | public function compareWith(Member $member, bool $full = true): bool { |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * @param array $data |
||
| 508 | * |
||
| 509 | * @return $this |
||
| 510 | * @throws InvalidItemException |
||
| 511 | */ |
||
| 512 | public function import(array $data): IDeserializable { |
||
| 542 | |||
| 543 | |||
| 544 | /** |
||
| 545 | * @return string[] |
||
| 546 | */ |
||
| 547 | public function jsonSerialize(): array { |
||
| 574 | |||
| 575 | |||
| 576 | /** |
||
| 577 | * @param array $data |
||
| 578 | * @param string $prefix |
||
| 579 | * |
||
| 580 | * @return INC21QueryRow |
||
| 581 | * @throws MemberNotFoundException |
||
| 582 | */ |
||
| 583 | public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow { |
||
| 622 | |||
| 623 | |||
| 624 | /** |
||
| 625 | * @param string $levelString |
||
| 626 | * |
||
| 627 | * @return int |
||
| 628 | * @throws ParseMemberLevelException |
||
| 629 | */ |
||
| 630 | View Code Duplication | public static function parseLevelString(string $levelString): int { |
|
| 641 | |||
| 642 | /** |
||
| 643 | * @param string $typeString |
||
| 644 | * |
||
| 645 | * @return int |
||
| 646 | * @throws UserTypeNotFoundException |
||
| 647 | */ |
||
| 648 | View Code Duplication | public static function parseTypeString(string $typeString): int { |
|
| 659 | |||
| 660 | } |
||
| 661 | |||
| 662 |