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 |
||
| 48 | class Member extends ManagedModel implements IFederatedUser, INC21Convert, INC21QueryRow, JsonSerializable { |
||
| 49 | |||
| 50 | |||
| 51 | use TArrayTools; |
||
| 52 | |||
| 53 | |||
| 54 | const LEVEL_NONE = 0; |
||
| 55 | const LEVEL_MEMBER = 1; |
||
| 56 | const LEVEL_MODERATOR = 4; |
||
| 57 | const LEVEL_ADMIN = 8; |
||
| 58 | const LEVEL_OWNER = 9; |
||
| 59 | |||
| 60 | const TYPE_CIRCLE = 16; |
||
| 61 | const TYPE_USER = 1; |
||
| 62 | const TYPE_GROUP = 2; |
||
| 63 | const TYPE_MAIL = 3; |
||
| 64 | const TYPE_CONTACT = 4; |
||
| 65 | |||
| 66 | const STATUS_NONMEMBER = 'Unknown'; |
||
| 67 | const STATUS_INVITED = 'Invited'; |
||
| 68 | const STATUS_REQUEST = 'Requesting'; |
||
| 69 | const STATUS_MEMBER = 'Member'; |
||
| 70 | const STATUS_BLOCKED = 'Blocked'; |
||
| 71 | const STATUS_KICKED = 'Kicked'; |
||
| 72 | |||
| 73 | const ID_LENGTH = 15; |
||
| 74 | |||
| 75 | static $DEF_LEVEL = [ |
||
| 76 | 1 => 'Member', |
||
| 77 | 4 => 'Moderator', |
||
| 78 | 8 => 'Admin', |
||
| 79 | 9 => 'Owner' |
||
| 80 | ]; |
||
| 81 | |||
| 82 | |||
| 83 | /** @var string */ |
||
| 84 | private $id = ''; |
||
| 85 | |||
| 86 | /** @var string */ |
||
| 87 | private $circleId = ''; |
||
| 88 | |||
| 89 | /** @var string */ |
||
| 90 | private $singleId = ''; |
||
| 91 | |||
| 92 | /** @var string */ |
||
| 93 | private $userId = ''; |
||
| 94 | |||
| 95 | /** @var int */ |
||
| 96 | private $userType = self::TYPE_USER; |
||
| 97 | |||
| 98 | /** @var string */ |
||
| 99 | private $instance = ''; |
||
| 100 | |||
| 101 | /** @var int */ |
||
| 102 | private $level = 0; |
||
| 103 | |||
| 104 | /** @var string */ |
||
| 105 | private $status = 'Unknown'; |
||
| 106 | |||
| 107 | /** @var string */ |
||
| 108 | private $note = ''; |
||
| 109 | |||
| 110 | /** @var string */ |
||
| 111 | private $cachedName = ''; |
||
| 112 | |||
| 113 | /** @var int */ |
||
| 114 | private $cachedUpdate = 0; |
||
| 115 | |||
| 116 | /** @var string */ |
||
| 117 | private $contactId = ''; |
||
| 118 | |||
| 119 | /** @var string */ |
||
| 120 | private $contactMeta = ''; |
||
| 121 | |||
| 122 | /** @var Circle */ |
||
| 123 | private $circle; |
||
| 124 | |||
| 125 | |||
| 126 | /** @var int */ |
||
| 127 | private $joined = 0; |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Member constructor. |
||
| 132 | */ |
||
| 133 | public function __construct() { |
||
| 134 | } |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $id |
||
| 139 | * |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | public function setId(string $id): self { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getId(): string { |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $circleId |
||
| 158 | * |
||
| 159 | * @return Member |
||
| 160 | */ |
||
| 161 | public function setCircleId(string $circleId): self { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function getCircleId(): string { |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * This should replace user_id, user_type and instance; and will use the data from Circle with |
||
| 177 | * Config=CFG_SINGLE |
||
| 178 | * |
||
| 179 | * @param string $singleId |
||
| 180 | * |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | public function setSingleId(string $singleId): self { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function getSingleId(): string { |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $userId |
||
| 199 | * |
||
| 200 | * @return Member |
||
| 201 | */ |
||
| 202 | public function setUserId(string $userId): self { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function getUserId(): string { |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * @param int $userType |
||
| 218 | * |
||
| 219 | * @return Member |
||
| 220 | */ |
||
| 221 | public function setUserType(int $userType): self { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return int |
||
| 229 | */ |
||
| 230 | public function getUserType(): int { |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $instance |
||
| 237 | * |
||
| 238 | * @return Member |
||
| 239 | */ |
||
| 240 | public function setInstance(string $instance): self { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getInstance(): string { |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * @param int $level |
||
| 256 | * |
||
| 257 | * @return Member |
||
| 258 | */ |
||
| 259 | public function setLevel(int $level): self { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return int |
||
| 267 | */ |
||
| 268 | public function getLevel(): int { |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $status |
||
| 275 | * |
||
| 276 | * @return Member |
||
| 277 | */ |
||
| 278 | public function setStatus(string $status): self { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getStatus(): string { |
||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $note |
||
| 294 | * |
||
| 295 | * @return Member |
||
| 296 | */ |
||
| 297 | public function setNote(string $note): self { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function getNote(): string { |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $cachedName |
||
| 313 | * |
||
| 314 | * @return Member |
||
| 315 | */ |
||
| 316 | public function setCachedName(string $cachedName): self { |
||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * @param int $cachedUpdate |
||
| 325 | * |
||
| 326 | * @return Member |
||
| 327 | */ |
||
| 328 | public function setCachedUpdate(int $cachedUpdate): self { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return int |
||
| 336 | */ |
||
| 337 | public function getCachedUpdate(): int { |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function getCachedName(): string { |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $contactId |
||
| 352 | * |
||
| 353 | * @return Member |
||
| 354 | */ |
||
| 355 | public function setContactId(string $contactId): self { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | public function getContactId(): string { |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * @param string $contactMeta |
||
| 371 | * |
||
| 372 | * @return Member |
||
| 373 | */ |
||
| 374 | public function setContactMeta(string $contactMeta): self { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | public function getContactMeta(): string { |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * @param Circle $circle |
||
| 390 | * |
||
| 391 | * @return self |
||
| 392 | */ |
||
| 393 | public function setCircle(Circle $circle): self { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @return Circle |
||
| 401 | */ |
||
| 402 | public function getCircle(): Circle { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | public function hasCircle(): bool { |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * @param int $joined |
||
| 416 | * |
||
| 417 | * @return Member |
||
| 418 | */ |
||
| 419 | public function setJoined(int $joined): self { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return int |
||
| 427 | */ |
||
| 428 | public function getJoined(): int { |
||
| 431 | |||
| 432 | |||
| 433 | /** |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | public function isMember(): bool { |
||
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * @param Member $member |
||
| 443 | * |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | public function compareWith(Member $member): bool { |
||
| 460 | |||
| 461 | |||
| 462 | /** |
||
| 463 | * @param array $data |
||
| 464 | * |
||
| 465 | * @return $this |
||
| 466 | */ |
||
| 467 | public function import(array $data): INC21Convert { |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * @return string[] |
||
| 489 | */ |
||
| 490 | public function jsonSerialize(): array { |
||
| 516 | |||
| 517 | |||
| 518 | /** |
||
| 519 | * @param array $data |
||
| 520 | * @param string $prefix |
||
| 521 | * |
||
| 522 | * @return INC21QueryRow |
||
| 523 | * @throws MemberNotFoundException |
||
| 524 | */ |
||
| 525 | public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow { |
||
| 563 | |||
| 564 | } |
||
| 565 | |||
| 566 |