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 int */ |
||
114 | private $level = 0; |
||
115 | |||
116 | /** @var string */ |
||
117 | private $status = 'Unknown'; |
||
118 | |||
119 | /** @var string */ |
||
120 | private $note = ''; |
||
121 | |||
122 | /** @var string */ |
||
123 | private $displayName = ''; |
||
124 | |||
125 | /** @var int */ |
||
126 | private $displayUpdate = 0; |
||
127 | |||
128 | /** @var string */ |
||
129 | private $contactId = ''; |
||
130 | |||
131 | /** @var string */ |
||
132 | private $contactMeta = ''; |
||
133 | |||
134 | /** @var Circle */ |
||
135 | private $circle; |
||
136 | |||
137 | |||
138 | /** @var int */ |
||
139 | private $joined = 0; |
||
140 | |||
141 | |||
142 | /** |
||
143 | * Member constructor. |
||
144 | */ |
||
145 | public function __construct() { |
||
147 | |||
148 | |||
149 | /** |
||
150 | * @param string $id |
||
151 | * |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function setId(string $id): self { |
||
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getId(): string { |
||
166 | |||
167 | |||
168 | /** |
||
169 | * @param string $circleId |
||
170 | * |
||
171 | * @return Member |
||
172 | */ |
||
173 | public function setCircleId(string $circleId): self { |
||
178 | |||
179 | /** |
||
180 | * @return string |
||
181 | */ |
||
182 | public function getCircleId(): string { |
||
185 | |||
186 | |||
187 | /** |
||
188 | * This should replace user_id, user_type and instance; and will use the data from Circle with |
||
189 | * Config=CFG_SINGLE |
||
190 | * |
||
191 | * @param string $singleId |
||
192 | * |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function setSingleId(string $singleId): self { |
||
200 | |||
201 | /** |
||
202 | * @return string |
||
203 | */ |
||
204 | public function getSingleId(): string { |
||
207 | |||
208 | |||
209 | /** |
||
210 | * @param string $userId |
||
211 | * |
||
212 | * @return Member |
||
213 | */ |
||
214 | public function setUserId(string $userId): self { |
||
222 | |||
223 | /** |
||
224 | * @return string |
||
225 | */ |
||
226 | public function getUserId(): string { |
||
229 | |||
230 | |||
231 | /** |
||
232 | * @param int $userType |
||
233 | * |
||
234 | * @return Member |
||
235 | */ |
||
236 | public function setUserType(int $userType): self { |
||
241 | |||
242 | /** |
||
243 | * @return int |
||
244 | */ |
||
245 | public function getUserType(): int { |
||
248 | |||
249 | |||
250 | /** |
||
251 | * @param string $instance |
||
252 | * |
||
253 | * @return Member |
||
254 | */ |
||
255 | public function setInstance(string $instance): self { |
||
260 | |||
261 | /** |
||
262 | * @return string |
||
263 | */ |
||
264 | public function getInstance(): string { |
||
267 | |||
268 | |||
269 | /** |
||
270 | * @param int $level |
||
271 | * |
||
272 | * @return Member |
||
273 | */ |
||
274 | public function setLevel(int $level): self { |
||
279 | |||
280 | /** |
||
281 | * @return int |
||
282 | */ |
||
283 | public function getLevel(): int { |
||
286 | |||
287 | |||
288 | /** |
||
289 | * @param string $status |
||
290 | * |
||
291 | * @return Member |
||
292 | */ |
||
293 | public function setStatus(string $status): self { |
||
298 | |||
299 | /** |
||
300 | * @return string |
||
301 | */ |
||
302 | public function getStatus(): string { |
||
305 | |||
306 | |||
307 | /** |
||
308 | * @param string $note |
||
309 | * |
||
310 | * @return Member |
||
311 | */ |
||
312 | public function setNote(string $note): self { |
||
317 | |||
318 | /** |
||
319 | * @return string |
||
320 | */ |
||
321 | public function getNote(): string { |
||
324 | |||
325 | |||
326 | /** |
||
327 | * @param string $displayName |
||
328 | * |
||
329 | * @return Member |
||
330 | */ |
||
331 | public function setDisplayName(string $displayName): self { |
||
338 | |||
339 | |||
340 | /** |
||
341 | * @param int $displayUpdate |
||
342 | * |
||
343 | * @return Member |
||
344 | */ |
||
345 | public function setDisplayUpdate(int $displayUpdate): self { |
||
350 | |||
351 | /** |
||
352 | * @return int |
||
353 | */ |
||
354 | public function getDisplayUpdate(): int { |
||
357 | |||
358 | |||
359 | /** |
||
360 | * @return string |
||
361 | */ |
||
362 | public function getDisplayName(): string { |
||
365 | |||
366 | |||
367 | /** |
||
368 | * @param string $contactId |
||
369 | * |
||
370 | * @return Member |
||
371 | */ |
||
372 | public function setContactId(string $contactId): self { |
||
377 | |||
378 | /** |
||
379 | * @return string |
||
380 | */ |
||
381 | public function getContactId(): string { |
||
384 | |||
385 | |||
386 | /** |
||
387 | * @param string $contactMeta |
||
388 | * |
||
389 | * @return Member |
||
390 | */ |
||
391 | public function setContactMeta(string $contactMeta): self { |
||
396 | |||
397 | /** |
||
398 | * @return string |
||
399 | */ |
||
400 | public function getContactMeta(): string { |
||
403 | |||
404 | |||
405 | /** |
||
406 | * @param Circle $circle |
||
407 | * |
||
408 | * @return self |
||
409 | */ |
||
410 | public function setCircle(Circle $circle): self { |
||
415 | |||
416 | /** |
||
417 | * @return Circle |
||
418 | */ |
||
419 | public function getCircle(): Circle { |
||
422 | |||
423 | /** |
||
424 | * @return bool |
||
425 | */ |
||
426 | public function hasCircle(): bool { |
||
429 | |||
430 | |||
431 | /** |
||
432 | * @param int $joined |
||
433 | * |
||
434 | * @return Member |
||
435 | */ |
||
436 | public function setJoined(int $joined): self { |
||
441 | |||
442 | /** |
||
443 | * @return int |
||
444 | */ |
||
445 | public function getJoined(): int { |
||
448 | |||
449 | |||
450 | /** |
||
451 | * @return bool |
||
452 | */ |
||
453 | public function isMember(): bool { |
||
456 | |||
457 | |||
458 | /** |
||
459 | * @param Member $member |
||
460 | * @param bool $full |
||
461 | * |
||
462 | * @return bool |
||
463 | */ |
||
464 | public function compareWith(Member $member, bool $full = true): bool { |
||
482 | |||
483 | |||
484 | /** |
||
485 | * @param array $data |
||
486 | * |
||
487 | * @return $this |
||
488 | * @throws InvalidItemException |
||
489 | */ |
||
490 | public function import(array $data): IDeserializable { |
||
519 | |||
520 | |||
521 | /** |
||
522 | * @return string[] |
||
523 | */ |
||
524 | public function jsonSerialize(): array { |
||
550 | |||
551 | |||
552 | /** |
||
553 | * @param array $data |
||
554 | * @param string $prefix |
||
555 | * |
||
556 | * @return INC21QueryRow |
||
557 | * @throws MemberNotFoundException |
||
558 | */ |
||
559 | public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow { |
||
597 | |||
598 | |||
599 | /** |
||
600 | * @param string $levelString |
||
601 | * |
||
602 | * @return int |
||
603 | * @throws ParseMemberLevelException |
||
604 | */ |
||
605 | View Code Duplication | public static function parseLevelString(string $levelString): int { |
|
616 | |||
617 | /** |
||
618 | * @param string $typeString |
||
619 | * |
||
620 | * @return int |
||
621 | * @throws UserTypeNotFoundException |
||
622 | */ |
||
623 | View Code Duplication | public static function parseTypeString(string $typeString): int { |
|
634 | |||
635 | } |
||
636 | |||
637 |