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 $cachedName = ''; |
||
124 | |||
125 | /** @var int */ |
||
126 | private $cachedUpdate = 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 { |
||
219 | |||
220 | /** |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getUserId(): string { |
||
226 | |||
227 | |||
228 | /** |
||
229 | * @param int $userType |
||
230 | * |
||
231 | * @return Member |
||
232 | */ |
||
233 | public function setUserType(int $userType): self { |
||
238 | |||
239 | /** |
||
240 | * @return int |
||
241 | */ |
||
242 | public function getUserType(): int { |
||
245 | |||
246 | |||
247 | /** |
||
248 | * @param string $instance |
||
249 | * |
||
250 | * @return Member |
||
251 | */ |
||
252 | public function setInstance(string $instance): self { |
||
257 | |||
258 | /** |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getInstance(): string { |
||
264 | |||
265 | |||
266 | /** |
||
267 | * @param int $level |
||
268 | * |
||
269 | * @return Member |
||
270 | */ |
||
271 | public function setLevel(int $level): self { |
||
276 | |||
277 | /** |
||
278 | * @return int |
||
279 | */ |
||
280 | public function getLevel(): int { |
||
283 | |||
284 | |||
285 | /** |
||
286 | * @param string $status |
||
287 | * |
||
288 | * @return Member |
||
289 | */ |
||
290 | public function setStatus(string $status): self { |
||
295 | |||
296 | /** |
||
297 | * @return string |
||
298 | */ |
||
299 | public function getStatus(): string { |
||
302 | |||
303 | |||
304 | /** |
||
305 | * @param string $note |
||
306 | * |
||
307 | * @return Member |
||
308 | */ |
||
309 | public function setNote(string $note): self { |
||
314 | |||
315 | /** |
||
316 | * @return string |
||
317 | */ |
||
318 | public function getNote(): string { |
||
321 | |||
322 | |||
323 | /** |
||
324 | * @param string $cachedName |
||
325 | * |
||
326 | * @return Member |
||
327 | */ |
||
328 | public function setCachedName(string $cachedName): self { |
||
333 | |||
334 | |||
335 | /** |
||
336 | * @param int $cachedUpdate |
||
337 | * |
||
338 | * @return Member |
||
339 | */ |
||
340 | public function setCachedUpdate(int $cachedUpdate): self { |
||
345 | |||
346 | /** |
||
347 | * @return int |
||
348 | */ |
||
349 | public function getCachedUpdate(): int { |
||
352 | |||
353 | |||
354 | /** |
||
355 | * @return string |
||
356 | */ |
||
357 | public function getCachedName(): string { |
||
360 | |||
361 | |||
362 | /** |
||
363 | * @param string $contactId |
||
364 | * |
||
365 | * @return Member |
||
366 | */ |
||
367 | public function setContactId(string $contactId): self { |
||
372 | |||
373 | /** |
||
374 | * @return string |
||
375 | */ |
||
376 | public function getContactId(): string { |
||
379 | |||
380 | |||
381 | /** |
||
382 | * @param string $contactMeta |
||
383 | * |
||
384 | * @return Member |
||
385 | */ |
||
386 | public function setContactMeta(string $contactMeta): self { |
||
391 | |||
392 | /** |
||
393 | * @return string |
||
394 | */ |
||
395 | public function getContactMeta(): string { |
||
398 | |||
399 | |||
400 | /** |
||
401 | * @param Circle $circle |
||
402 | * |
||
403 | * @return self |
||
404 | */ |
||
405 | public function setCircle(Circle $circle): self { |
||
410 | |||
411 | /** |
||
412 | * @return Circle |
||
413 | */ |
||
414 | public function getCircle(): Circle { |
||
417 | |||
418 | /** |
||
419 | * @return bool |
||
420 | */ |
||
421 | public function hasCircle(): bool { |
||
424 | |||
425 | |||
426 | /** |
||
427 | * @param int $joined |
||
428 | * |
||
429 | * @return Member |
||
430 | */ |
||
431 | public function setJoined(int $joined): self { |
||
436 | |||
437 | /** |
||
438 | * @return int |
||
439 | */ |
||
440 | public function getJoined(): int { |
||
443 | |||
444 | |||
445 | /** |
||
446 | * @return bool |
||
447 | */ |
||
448 | public function isMember(): bool { |
||
451 | |||
452 | |||
453 | /** |
||
454 | * @param Member $member |
||
455 | * @param bool $full |
||
456 | * |
||
457 | * @return bool |
||
458 | */ |
||
459 | public function compareWith(Member $member, bool $full = true): bool { |
||
477 | |||
478 | |||
479 | /** |
||
480 | * @param array $data |
||
481 | * |
||
482 | * @return $this |
||
483 | * @throws InvalidItemException |
||
484 | */ |
||
485 | public function import(array $data): IDeserializable { |
||
514 | |||
515 | |||
516 | /** |
||
517 | * @return string[] |
||
518 | */ |
||
519 | public function jsonSerialize(): array { |
||
545 | |||
546 | |||
547 | /** |
||
548 | * @param array $data |
||
549 | * @param string $prefix |
||
550 | * |
||
551 | * @return INC21QueryRow |
||
552 | * @throws MemberNotFoundException |
||
553 | */ |
||
554 | public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow { |
||
592 | |||
593 | |||
594 | /** |
||
595 | * @param string $levelString |
||
596 | * |
||
597 | * @return int |
||
598 | * @throws MemberLevelException |
||
599 | */ |
||
600 | View Code Duplication | public static function parseLevelString(string $levelString): int { |
|
611 | |||
612 | /** |
||
613 | * @param string $typeString |
||
614 | * |
||
615 | * @return int |
||
616 | * @throws UserTypeNotFoundException |
||
617 | */ |
||
618 | View Code Duplication | public static function parseTypeString(string $typeString): int { |
|
629 | |||
630 | } |
||
631 | |||
632 |