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_USER = 1; |
||
| 67 | const TYPE_GROUP = 2; |
||
| 68 | const TYPE_MAIL = 3; |
||
| 69 | const TYPE_CONTACT = 4; |
||
| 70 | |||
| 71 | const STATUS_NONMEMBER = 'Unknown'; |
||
| 72 | const STATUS_INVITED = 'Invited'; |
||
| 73 | const STATUS_REQUEST = 'Requesting'; |
||
| 74 | const STATUS_MEMBER = 'Member'; |
||
| 75 | const STATUS_BLOCKED = 'Blocked'; |
||
| 76 | const STATUS_KICKED = 'Kicked'; |
||
| 77 | |||
| 78 | public static $DEF_LEVEL = [ |
||
| 79 | 1 => 'Member', |
||
| 80 | 4 => 'Moderator', |
||
| 81 | 8 => 'Admin', |
||
| 82 | 9 => 'Owner' |
||
| 83 | ]; |
||
| 84 | |||
| 85 | public static $DEF_TYPE = [ |
||
| 86 | 1 => 'local', |
||
| 87 | 16 => 'circle', |
||
| 88 | 3 => 'mail', |
||
| 89 | 4 => 'contact', |
||
| 90 | ]; |
||
| 91 | |||
| 92 | /** @var string */ |
||
| 93 | private $id = ''; |
||
| 94 | |||
| 95 | /** @var string */ |
||
| 96 | private $circleId = ''; |
||
| 97 | |||
| 98 | /** @var string */ |
||
| 99 | private $singleId = ''; |
||
| 100 | |||
| 101 | /** @var string */ |
||
| 102 | private $userId = ''; |
||
| 103 | |||
| 104 | /** @var int */ |
||
| 105 | private $userType = self::TYPE_USER; |
||
| 106 | |||
| 107 | /** @var string */ |
||
| 108 | private $instance = ''; |
||
| 109 | |||
| 110 | /** @var int */ |
||
| 111 | private $level = 0; |
||
| 112 | |||
| 113 | /** @var string */ |
||
| 114 | private $status = 'Unknown'; |
||
| 115 | |||
| 116 | /** @var string */ |
||
| 117 | private $note = ''; |
||
| 118 | |||
| 119 | /** @var string */ |
||
| 120 | private $cachedName = ''; |
||
| 121 | |||
| 122 | /** @var int */ |
||
| 123 | private $cachedUpdate = 0; |
||
| 124 | |||
| 125 | /** @var string */ |
||
| 126 | private $contactId = ''; |
||
| 127 | |||
| 128 | /** @var string */ |
||
| 129 | private $contactMeta = ''; |
||
| 130 | |||
| 131 | /** @var Circle */ |
||
| 132 | private $circle; |
||
| 133 | |||
| 134 | |||
| 135 | /** @var int */ |
||
| 136 | private $joined = 0; |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Member constructor. |
||
| 141 | */ |
||
| 142 | public function __construct() { |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $id |
||
| 148 | * |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | public function setId(string $id): self { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function getId(): string { |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $circleId |
||
| 167 | * |
||
| 168 | * @return Member |
||
| 169 | */ |
||
| 170 | public function setCircleId(string $circleId): self { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function getCircleId(): string { |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * This should replace user_id, user_type and instance; and will use the data from Circle with |
||
| 186 | * Config=CFG_SINGLE |
||
| 187 | * |
||
| 188 | * @param string $singleId |
||
| 189 | * |
||
| 190 | * @return $this |
||
| 191 | */ |
||
| 192 | public function setSingleId(string $singleId): self { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getSingleId(): string { |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $userId |
||
| 208 | * |
||
| 209 | * @return Member |
||
| 210 | */ |
||
| 211 | public function setUserId(string $userId): self { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public function getUserId(): string { |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * @param int $userType |
||
| 227 | * |
||
| 228 | * @return Member |
||
| 229 | */ |
||
| 230 | public function setUserType(int $userType): self { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return int |
||
| 238 | */ |
||
| 239 | public function getUserType(): int { |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $instance |
||
| 246 | * |
||
| 247 | * @return Member |
||
| 248 | */ |
||
| 249 | public function setInstance(string $instance): self { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public function getInstance(): string { |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * @param int $level |
||
| 265 | * |
||
| 266 | * @return Member |
||
| 267 | */ |
||
| 268 | public function setLevel(int $level): self { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return int |
||
| 276 | */ |
||
| 277 | public function getLevel(): int { |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * @param string $status |
||
| 284 | * |
||
| 285 | * @return Member |
||
| 286 | */ |
||
| 287 | public function setStatus(string $status): self { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function getStatus(): string { |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $note |
||
| 303 | * |
||
| 304 | * @return Member |
||
| 305 | */ |
||
| 306 | public function setNote(string $note): self { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getNote(): string { |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $cachedName |
||
| 322 | * |
||
| 323 | * @return Member |
||
| 324 | */ |
||
| 325 | public function setCachedName(string $cachedName): self { |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * @param int $cachedUpdate |
||
| 334 | * |
||
| 335 | * @return Member |
||
| 336 | */ |
||
| 337 | public function setCachedUpdate(int $cachedUpdate): self { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return int |
||
| 345 | */ |
||
| 346 | public function getCachedUpdate(): int { |
||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function getCachedName(): string { |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $contactId |
||
| 361 | * |
||
| 362 | * @return Member |
||
| 363 | */ |
||
| 364 | public function setContactId(string $contactId): self { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | public function getContactId(): string { |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $contactMeta |
||
| 380 | * |
||
| 381 | * @return Member |
||
| 382 | */ |
||
| 383 | public function setContactMeta(string $contactMeta): self { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function getContactMeta(): string { |
||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * @param Circle $circle |
||
| 399 | * |
||
| 400 | * @return self |
||
| 401 | */ |
||
| 402 | public function setCircle(Circle $circle): self { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return Circle |
||
| 410 | */ |
||
| 411 | public function getCircle(): Circle { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return bool |
||
| 417 | */ |
||
| 418 | public function hasCircle(): bool { |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * @param int $joined |
||
| 425 | * |
||
| 426 | * @return Member |
||
| 427 | */ |
||
| 428 | public function setJoined(int $joined): self { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @return int |
||
| 436 | */ |
||
| 437 | public function getJoined(): int { |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * @return bool |
||
| 444 | */ |
||
| 445 | public function isMember(): bool { |
||
| 448 | |||
| 449 | |||
| 450 | /** |
||
| 451 | * @param Member $member |
||
| 452 | * @param bool $full |
||
| 453 | * |
||
| 454 | * @return bool |
||
| 455 | */ |
||
| 456 | public function compareWith(Member $member, bool $full = true): bool { |
||
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * @param array $data |
||
| 478 | * |
||
| 479 | * @return $this |
||
| 480 | * @throws InvalidItemException |
||
| 481 | */ |
||
| 482 | public function import(array $data): IDeserializable { |
||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * @return string[] |
||
| 515 | */ |
||
| 516 | public function jsonSerialize(): array { |
||
| 542 | |||
| 543 | |||
| 544 | /** |
||
| 545 | * @param array $data |
||
| 546 | * @param string $prefix |
||
| 547 | * |
||
| 548 | * @return INC21QueryRow |
||
| 549 | * @throws MemberNotFoundException |
||
| 550 | */ |
||
| 551 | public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow { |
||
| 589 | |||
| 590 | |||
| 591 | /** |
||
| 592 | * @param string $levelString |
||
| 593 | * |
||
| 594 | * @return int |
||
| 595 | * @throws MemberLevelException |
||
| 596 | */ |
||
| 597 | View Code Duplication | public static function parseLevelString(string $levelString): int { |
|
| 608 | |||
| 609 | /** |
||
| 610 | * @param string $typeString |
||
| 611 | * |
||
| 612 | * @return int |
||
| 613 | * @throws UserTypeNotFoundException |
||
| 614 | */ |
||
| 615 | View Code Duplication | public static function parseTypeString(string $typeString): int { |
|
| 626 | |||
| 627 | } |
||
| 628 | |||
| 629 |