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 |
||
53 | class Member extends ManagedModel implements IFederatedUser, IDeserializable, INC21QueryRow, JsonSerializable { |
||
54 | |||
55 | |||
56 | use TArrayTools; |
||
57 | use TNC21Deserialize; |
||
58 | |||
59 | |||
60 | const LEVEL_NONE = 0; |
||
61 | const LEVEL_MEMBER = 1; |
||
62 | const LEVEL_MODERATOR = 4; |
||
63 | const LEVEL_ADMIN = 8; |
||
64 | const LEVEL_OWNER = 9; |
||
65 | |||
66 | const TYPE_CIRCLE = 16; |
||
67 | const TYPE_SINGLE = 8; |
||
68 | const TYPE_USER = 1; |
||
69 | const TYPE_GROUP = 2; |
||
70 | const TYPE_MAIL = 3; |
||
71 | const TYPE_CONTACT = 4; |
||
72 | |||
73 | const STATUS_NONMEMBER = 'Unknown'; |
||
74 | const STATUS_INVITED = 'Invited'; |
||
75 | const STATUS_REQUEST = 'Requesting'; |
||
76 | const STATUS_MEMBER = 'Member'; |
||
77 | const STATUS_BLOCKED = 'Blocked'; |
||
78 | const STATUS_KICKED = 'Kicked'; |
||
79 | |||
80 | |||
81 | public static $DEF_LEVEL = [ |
||
82 | 1 => 'Member', |
||
83 | 4 => 'Moderator', |
||
84 | 8 => 'Admin', |
||
85 | 9 => 'Owner' |
||
86 | ]; |
||
87 | |||
88 | public static $DEF_TYPE = [ |
||
89 | 1 => 'user', |
||
90 | 16 => 'circle', |
||
91 | 8 => 'single', |
||
92 | 3 => 'mail', |
||
93 | 4 => 'contact', |
||
94 | ]; |
||
95 | |||
96 | /** @var string */ |
||
97 | private $id = ''; |
||
98 | |||
99 | /** @var string */ |
||
100 | private $circleId = ''; |
||
101 | |||
102 | /** @var string */ |
||
103 | private $singleId = ''; |
||
104 | |||
105 | /** @var string */ |
||
106 | private $userId = ''; |
||
107 | |||
108 | /** @var int */ |
||
109 | private $userType = self::TYPE_USER; |
||
110 | |||
111 | /** @var string */ |
||
112 | private $instance = ''; |
||
113 | |||
114 | /** @var int */ |
||
115 | private $level = 0; |
||
116 | |||
117 | /** @var string */ |
||
118 | private $status = 'Unknown'; |
||
119 | |||
120 | /** @var string */ |
||
121 | private $note = ''; |
||
122 | |||
123 | /** @var string */ |
||
124 | private $cachedName = ''; |
||
125 | |||
126 | /** @var int */ |
||
127 | private $cachedUpdate = 0; |
||
128 | |||
129 | /** @var string */ |
||
130 | private $contactId = ''; |
||
131 | |||
132 | /** @var string */ |
||
133 | private $contactMeta = ''; |
||
134 | |||
135 | /** @var Circle */ |
||
136 | private $circle; |
||
137 | |||
138 | |||
139 | /** @var int */ |
||
140 | private $joined = 0; |
||
141 | |||
142 | |||
143 | /** |
||
144 | * Member constructor. |
||
145 | */ |
||
146 | public function __construct() { |
||
148 | |||
149 | |||
150 | /** |
||
151 | * @param string $id |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setId(string $id): self { |
||
160 | |||
161 | /** |
||
162 | * @return string |
||
163 | */ |
||
164 | public function getId(): string { |
||
167 | |||
168 | |||
169 | /** |
||
170 | * @param string $circleId |
||
171 | * |
||
172 | * @return Member |
||
173 | */ |
||
174 | public function setCircleId(string $circleId): self { |
||
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | */ |
||
183 | public function getCircleId(): string { |
||
186 | |||
187 | |||
188 | /** |
||
189 | * This should replace user_id, user_type and instance; and will use the data from Circle with |
||
190 | * Config=CFG_SINGLE |
||
191 | * |
||
192 | * @param string $singleId |
||
193 | * |
||
194 | * @return $this |
||
195 | */ |
||
196 | public function setSingleId(string $singleId): self { |
||
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | public function getSingleId(): string { |
||
208 | |||
209 | |||
210 | /** |
||
211 | * @param string $userId |
||
212 | * |
||
213 | * @return Member |
||
214 | */ |
||
215 | public function setUserId(string $userId): self { |
||
220 | |||
221 | /** |
||
222 | * @return string |
||
223 | */ |
||
224 | public function getUserId(): string { |
||
227 | |||
228 | |||
229 | /** |
||
230 | * @param int $userType |
||
231 | * |
||
232 | * @return Member |
||
233 | */ |
||
234 | public function setUserType(int $userType): self { |
||
239 | |||
240 | /** |
||
241 | * @return int |
||
242 | */ |
||
243 | public function getUserType(): int { |
||
246 | |||
247 | |||
248 | /** |
||
249 | * @param string $instance |
||
250 | * |
||
251 | * @return Member |
||
252 | */ |
||
253 | public function setInstance(string $instance): self { |
||
258 | |||
259 | /** |
||
260 | * @return string |
||
261 | */ |
||
262 | public function getInstance(): string { |
||
265 | |||
266 | |||
267 | /** |
||
268 | * @param int $level |
||
269 | * |
||
270 | * @return Member |
||
271 | */ |
||
272 | public function setLevel(int $level): self { |
||
277 | |||
278 | /** |
||
279 | * @return int |
||
280 | */ |
||
281 | public function getLevel(): int { |
||
284 | |||
285 | |||
286 | /** |
||
287 | * @param string $status |
||
288 | * |
||
289 | * @return Member |
||
290 | */ |
||
291 | public function setStatus(string $status): self { |
||
296 | |||
297 | /** |
||
298 | * @return string |
||
299 | */ |
||
300 | public function getStatus(): string { |
||
303 | |||
304 | |||
305 | /** |
||
306 | * @param string $note |
||
307 | * |
||
308 | * @return Member |
||
309 | */ |
||
310 | public function setNote(string $note): self { |
||
315 | |||
316 | /** |
||
317 | * @return string |
||
318 | */ |
||
319 | public function getNote(): string { |
||
322 | |||
323 | |||
324 | /** |
||
325 | * @param string $cachedName |
||
326 | * |
||
327 | * @return Member |
||
328 | */ |
||
329 | public function setCachedName(string $cachedName): self { |
||
334 | |||
335 | |||
336 | /** |
||
337 | * @param int $cachedUpdate |
||
338 | * |
||
339 | * @return Member |
||
340 | */ |
||
341 | public function setCachedUpdate(int $cachedUpdate): self { |
||
346 | |||
347 | /** |
||
348 | * @return int |
||
349 | */ |
||
350 | public function getCachedUpdate(): int { |
||
353 | |||
354 | |||
355 | /** |
||
356 | * @return string |
||
357 | */ |
||
358 | public function getCachedName(): string { |
||
361 | |||
362 | |||
363 | /** |
||
364 | * @param string $contactId |
||
365 | * |
||
366 | * @return Member |
||
367 | */ |
||
368 | public function setContactId(string $contactId): self { |
||
373 | |||
374 | /** |
||
375 | * @return string |
||
376 | */ |
||
377 | public function getContactId(): string { |
||
380 | |||
381 | |||
382 | /** |
||
383 | * @param string $contactMeta |
||
384 | * |
||
385 | * @return Member |
||
386 | */ |
||
387 | public function setContactMeta(string $contactMeta): self { |
||
392 | |||
393 | /** |
||
394 | * @return string |
||
395 | */ |
||
396 | public function getContactMeta(): string { |
||
399 | |||
400 | |||
401 | /** |
||
402 | * @param Circle $circle |
||
403 | * |
||
404 | * @return self |
||
405 | */ |
||
406 | public function setCircle(Circle $circle): self { |
||
411 | |||
412 | /** |
||
413 | * @return Circle |
||
414 | */ |
||
415 | public function getCircle(): Circle { |
||
418 | |||
419 | /** |
||
420 | * @return bool |
||
421 | */ |
||
422 | public function hasCircle(): bool { |
||
425 | |||
426 | |||
427 | /** |
||
428 | * @param int $joined |
||
429 | * |
||
430 | * @return Member |
||
431 | */ |
||
432 | public function setJoined(int $joined): self { |
||
437 | |||
438 | /** |
||
439 | * @return int |
||
440 | */ |
||
441 | public function getJoined(): int { |
||
444 | |||
445 | |||
446 | /** |
||
447 | * @return bool |
||
448 | */ |
||
449 | public function isMember(): bool { |
||
452 | |||
453 | |||
454 | /** |
||
455 | * @param Member $member |
||
456 | * @param bool $full |
||
457 | * |
||
458 | * @return bool |
||
459 | */ |
||
460 | public function compareWith(Member $member, bool $full = true): bool { |
||
478 | |||
479 | |||
480 | /** |
||
481 | * @param array $data |
||
482 | * |
||
483 | * @return $this |
||
484 | * @throws InvalidItemException |
||
485 | */ |
||
486 | public function import(array $data): IDeserializable { |
||
515 | |||
516 | |||
517 | /** |
||
518 | * @return string[] |
||
519 | */ |
||
520 | public function jsonSerialize(): array { |
||
546 | |||
547 | |||
548 | /** |
||
549 | * @param array $data |
||
550 | * @param string $prefix |
||
551 | * |
||
552 | * @return INC21QueryRow |
||
553 | * @throws MemberNotFoundException |
||
554 | */ |
||
555 | public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow { |
||
593 | |||
594 | |||
595 | /** |
||
596 | * @param string $levelString |
||
597 | * |
||
598 | * @return int |
||
599 | * @throws MemberLevelException |
||
600 | */ |
||
601 | View Code Duplication | public static function parseLevelString(string $levelString): int { |
|
612 | |||
613 | /** |
||
614 | * @param string $typeString |
||
615 | * |
||
616 | * @return int |
||
617 | * @throws UserTypeNotFoundException |
||
618 | */ |
||
619 | View Code Duplication | public static function parseTypeString(string $typeString): int { |
|
630 | |||
631 | } |
||
632 | |||
633 |