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 |
||
| 51 | class Member extends ManagedModel implements IFederatedUser, INC21Convert, INC21QueryRow, JsonSerializable { |
||
| 52 | |||
| 53 | |||
| 54 | use TArrayTools; |
||
| 55 | use TNC21Convert; |
||
| 56 | |||
| 57 | |||
| 58 | const LEVEL_NONE = 0; |
||
| 59 | const LEVEL_MEMBER = 1; |
||
| 60 | const LEVEL_MODERATOR = 4; |
||
| 61 | const LEVEL_ADMIN = 8; |
||
| 62 | const LEVEL_OWNER = 9; |
||
| 63 | |||
| 64 | const TYPE_CIRCLE = 16; |
||
| 65 | const TYPE_USER = 1; |
||
| 66 | const TYPE_GROUP = 2; |
||
| 67 | const TYPE_MAIL = 3; |
||
| 68 | const TYPE_CONTACT = 4; |
||
| 69 | |||
| 70 | const STATUS_NONMEMBER = 'Unknown'; |
||
| 71 | const STATUS_INVITED = 'Invited'; |
||
| 72 | const STATUS_REQUEST = 'Requesting'; |
||
| 73 | const STATUS_MEMBER = 'Member'; |
||
| 74 | const STATUS_BLOCKED = 'Blocked'; |
||
| 75 | const STATUS_KICKED = 'Kicked'; |
||
| 76 | |||
| 77 | static $DEF_LEVEL = [ |
||
| 78 | 1 => 'Member', |
||
| 79 | 4 => 'Moderator', |
||
| 80 | 8 => 'Admin', |
||
| 81 | 9 => 'Owner' |
||
| 82 | ]; |
||
| 83 | |||
| 84 | |||
| 85 | /** @var string */ |
||
| 86 | private $id = ''; |
||
| 87 | |||
| 88 | /** @var string */ |
||
| 89 | private $circleId = ''; |
||
| 90 | |||
| 91 | /** @var string */ |
||
| 92 | private $singleId = ''; |
||
| 93 | |||
| 94 | /** @var string */ |
||
| 95 | private $userId = ''; |
||
| 96 | |||
| 97 | /** @var int */ |
||
| 98 | private $userType = self::TYPE_USER; |
||
| 99 | |||
| 100 | /** @var string */ |
||
| 101 | private $instance = ''; |
||
| 102 | |||
| 103 | /** @var int */ |
||
| 104 | private $level = 0; |
||
| 105 | |||
| 106 | /** @var string */ |
||
| 107 | private $status = 'Unknown'; |
||
| 108 | |||
| 109 | /** @var string */ |
||
| 110 | private $note = ''; |
||
| 111 | |||
| 112 | /** @var string */ |
||
| 113 | private $cachedName = ''; |
||
| 114 | |||
| 115 | /** @var int */ |
||
| 116 | private $cachedUpdate = 0; |
||
| 117 | |||
| 118 | /** @var string */ |
||
| 119 | private $contactId = ''; |
||
| 120 | |||
| 121 | /** @var string */ |
||
| 122 | private $contactMeta = ''; |
||
| 123 | |||
| 124 | /** @var Circle */ |
||
| 125 | private $circle; |
||
| 126 | |||
| 127 | |||
| 128 | /** @var int */ |
||
| 129 | private $joined = 0; |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * Member constructor. |
||
| 134 | */ |
||
| 135 | public function __construct() { |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $id |
||
| 141 | * |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function setId(string $id): self { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function getId(): string { |
||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $circleId |
||
| 160 | * |
||
| 161 | * @return Member |
||
| 162 | */ |
||
| 163 | public function setCircleId(string $circleId): self { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function getCircleId(): string { |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * This should replace user_id, user_type and instance; and will use the data from Circle with |
||
| 179 | * Config=CFG_SINGLE |
||
| 180 | * |
||
| 181 | * @param string $singleId |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | public function setSingleId(string $singleId): self { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function getSingleId(): string { |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $userId |
||
| 201 | * |
||
| 202 | * @return Member |
||
| 203 | */ |
||
| 204 | public function setUserId(string $userId): self { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public function getUserId(): string { |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * @param int $userType |
||
| 220 | * |
||
| 221 | * @return Member |
||
| 222 | */ |
||
| 223 | public function setUserType(int $userType): self { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return int |
||
| 231 | */ |
||
| 232 | public function getUserType(): int { |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $instance |
||
| 239 | * |
||
| 240 | * @return Member |
||
| 241 | */ |
||
| 242 | public function setInstance(string $instance): self { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getInstance(): string { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * @param int $level |
||
| 258 | * |
||
| 259 | * @return Member |
||
| 260 | */ |
||
| 261 | public function setLevel(int $level): self { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return int |
||
| 269 | */ |
||
| 270 | public function getLevel(): int { |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $status |
||
| 277 | * |
||
| 278 | * @return Member |
||
| 279 | */ |
||
| 280 | public function setStatus(string $status): self { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getStatus(): string { |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $note |
||
| 296 | * |
||
| 297 | * @return Member |
||
| 298 | */ |
||
| 299 | public function setNote(string $note): self { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function getNote(): string { |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $cachedName |
||
| 315 | * |
||
| 316 | * @return Member |
||
| 317 | */ |
||
| 318 | public function setCachedName(string $cachedName): self { |
||
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * @param int $cachedUpdate |
||
| 327 | * |
||
| 328 | * @return Member |
||
| 329 | */ |
||
| 330 | public function setCachedUpdate(int $cachedUpdate): self { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return int |
||
| 338 | */ |
||
| 339 | public function getCachedUpdate(): int { |
||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function getCachedName(): string { |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $contactId |
||
| 354 | * |
||
| 355 | * @return Member |
||
| 356 | */ |
||
| 357 | public function setContactId(string $contactId): self { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | public function getContactId(): string { |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * @param string $contactMeta |
||
| 373 | * |
||
| 374 | * @return Member |
||
| 375 | */ |
||
| 376 | public function setContactMeta(string $contactMeta): self { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | public function getContactMeta(): string { |
||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * @param Circle $circle |
||
| 392 | * |
||
| 393 | * @return self |
||
| 394 | */ |
||
| 395 | public function setCircle(Circle $circle): self { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @return Circle |
||
| 403 | */ |
||
| 404 | public function getCircle(): Circle { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | public function hasCircle(): bool { |
||
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * @param int $joined |
||
| 418 | * |
||
| 419 | * @return Member |
||
| 420 | */ |
||
| 421 | public function setJoined(int $joined): self { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return int |
||
| 429 | */ |
||
| 430 | public function getJoined(): int { |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | public function isMember(): bool { |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * @param Member $member |
||
| 445 | * @param bool $full |
||
| 446 | * |
||
| 447 | * @return bool |
||
| 448 | */ |
||
| 449 | public function compareWith(Member $member, bool $full = true): bool { |
||
| 467 | |||
| 468 | |||
| 469 | /** |
||
| 470 | * @param array $data |
||
| 471 | * |
||
| 472 | * @return $this |
||
| 473 | * @throws InvalidItemException |
||
| 474 | */ |
||
| 475 | public function import(array $data): INC21Convert { |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * @return string[] |
||
| 508 | */ |
||
| 509 | public function jsonSerialize(): array { |
||
| 535 | |||
| 536 | |||
| 537 | /** |
||
| 538 | * @param array $data |
||
| 539 | * @param string $prefix |
||
| 540 | * |
||
| 541 | * @return INC21QueryRow |
||
| 542 | * @throws MemberNotFoundException |
||
| 543 | */ |
||
| 544 | public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow { |
||
| 582 | |||
| 583 | |||
| 584 | /** |
||
| 585 | * @param string $levelString |
||
| 586 | * |
||
| 587 | * @return int |
||
| 588 | * @throws MemberLevelException |
||
| 589 | */ |
||
| 590 | public static function parseLevelString(string $levelString): int { |
||
| 601 | |||
| 602 | } |
||
| 603 | |||
| 604 |