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 |
||
| 48 | class Member extends ManagedModel implements IMember, 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 = 14; |
||
| 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 $userId; |
||
| 91 | |||
| 92 | /** @var int */ |
||
| 93 | private $userType; |
||
| 94 | |||
| 95 | /** @var string */ |
||
| 96 | private $instance; |
||
| 97 | |||
| 98 | /** @var int */ |
||
| 99 | private $level = 0; |
||
| 100 | |||
| 101 | /** @var string */ |
||
| 102 | private $status = 'Unknown'; |
||
| 103 | |||
| 104 | /** @var string */ |
||
| 105 | private $note = ''; |
||
| 106 | |||
| 107 | /** @var string */ |
||
| 108 | private $cachedName = ''; |
||
| 109 | |||
| 110 | /** @var int */ |
||
| 111 | private $cachedUpdate = 0; |
||
| 112 | |||
| 113 | /** @var string */ |
||
| 114 | private $contactId = ''; |
||
| 115 | |||
| 116 | /** @var string */ |
||
| 117 | private $contactMeta = ''; |
||
| 118 | |||
| 119 | /** @var int */ |
||
| 120 | private $joined = 0; |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Member constructor. |
||
| 125 | * |
||
| 126 | * @param string $userId |
||
| 127 | * @param int $type |
||
| 128 | * @param string $instance |
||
| 129 | */ |
||
| 130 | public function __construct(string $userId = '', int $type = self::TYPE_USER, $instance = '') { |
||
| 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 | * @param string $userId |
||
| 177 | * |
||
| 178 | * @return Member |
||
| 179 | */ |
||
| 180 | public function setUserId(string $userId): self { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getUserId(): string { |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * @param int $userType |
||
| 196 | * |
||
| 197 | * @return Member |
||
| 198 | */ |
||
| 199 | public function setUserType(int $userType): self { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return int |
||
| 207 | */ |
||
| 208 | public function getUserType(): int { |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $instance |
||
| 215 | * |
||
| 216 | * @return Member |
||
| 217 | */ |
||
| 218 | public function setInstance(string $instance): self { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function getInstance(): string { |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * @param int $level |
||
| 234 | * |
||
| 235 | * @return Member |
||
| 236 | */ |
||
| 237 | public function setLevel(int $level): self { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | public function getLevel(): int { |
||
| 249 | |||
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $status |
||
| 253 | * |
||
| 254 | * @return Member |
||
| 255 | */ |
||
| 256 | public function setStatus(string $status): self { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getStatus(): string { |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $note |
||
| 272 | * |
||
| 273 | * @return Member |
||
| 274 | */ |
||
| 275 | public function setNote(string $note): self { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getNote(): string { |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $cachedName |
||
| 291 | * |
||
| 292 | * @return Member |
||
| 293 | */ |
||
| 294 | public function setCachedName(string $cachedName): self { |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * @param int $cachedUpdate |
||
| 303 | * |
||
| 304 | * @return Member |
||
| 305 | */ |
||
| 306 | public function setCachedUpdate(int $cachedUpdate): self { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return int |
||
| 314 | */ |
||
| 315 | public function getCachedUpdate(): int { |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public function getCachedName(): string { |
||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $contactId |
||
| 330 | * |
||
| 331 | * @return Member |
||
| 332 | */ |
||
| 333 | public function setContactId(string $contactId): self { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getContactId(): string { |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $contactMeta |
||
| 349 | * |
||
| 350 | * @return Member |
||
| 351 | */ |
||
| 352 | public function setContactMeta(string $contactMeta): self { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function getContactMeta(): string { |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * @param int $joined |
||
| 368 | * |
||
| 369 | * @return Member |
||
| 370 | */ |
||
| 371 | public function setJoined(int $joined): self { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return int |
||
| 379 | */ |
||
| 380 | public function getJoined(): int { |
||
| 383 | |||
| 384 | |||
| 385 | /** |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function isMember(): bool { |
||
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * @param Member $member |
||
| 395 | * |
||
| 396 | * @return bool |
||
| 397 | */ |
||
| 398 | public function compareWith(Member $member): bool { |
||
| 411 | |||
| 412 | |||
| 413 | /** |
||
| 414 | * @param IMember $member |
||
| 415 | * |
||
| 416 | * @return self |
||
| 417 | */ |
||
| 418 | public function importFromIMember(IMember $member): IMember { |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * @param array $data |
||
| 427 | * |
||
| 428 | * @return $this |
||
| 429 | */ |
||
| 430 | public function import(array $data): INC21Convert { |
||
| 447 | |||
| 448 | |||
| 449 | /** |
||
| 450 | * @return string[] |
||
| 451 | */ |
||
| 452 | View Code Duplication | public function jsonSerialize(): array { |
|
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * @param array $data |
||
| 475 | * @param string $prefix |
||
| 476 | * |
||
| 477 | * @return INC21QueryRow |
||
| 478 | * @throws MemberNotFoundException |
||
| 479 | */ |
||
| 480 | public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow { |
||
| 513 | |||
| 514 | } |
||
| 515 | |||
| 516 |