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 PostMessageRequest 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 PostMessageRequest, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
13 | class PostMessageRequest extends Request |
||
14 | { |
||
15 | /* |
||
16 | * Show message directly on phone. The message is not saved on the phone. (Also known as flash messages) |
||
17 | */ |
||
18 | const CLASS_0 = 0; |
||
19 | |||
20 | /* |
||
21 | * Save message in phone memory. Either on the phone or in SIM. |
||
22 | */ |
||
23 | const CLASS_1 = 1; |
||
24 | |||
25 | /* |
||
26 | * Message contains SIM data. |
||
27 | */ |
||
28 | const CLASS_2 = 2; |
||
29 | |||
30 | /* |
||
31 | * Message contains info that indicate that it should be |
||
32 | * sent to external units, normally used by terminal equipment. |
||
33 | */ |
||
34 | const CLASS_3 = 3; |
||
35 | |||
36 | /* |
||
37 | * Send normal message (160 chars, but if more than 160 chars, 153 chars per part message) |
||
38 | */ |
||
39 | const FORMAT_GSM = 'GSM'; |
||
40 | |||
41 | /* |
||
42 | * To send speciality chars like chinese letters. A normal message is 160 chars, but ifyou use |
||
43 | * unicode each message can only hold 70 chars (But if more than 70 chars, 67 chars per part message) |
||
44 | */ |
||
45 | const FORMAT_UNICODE = 'UNICODE'; |
||
46 | |||
47 | /* |
||
48 | * Send a binary message body in hex and define udh |
||
49 | */ |
||
50 | const FORMAT_BINARY = 'BINARY'; |
||
51 | |||
52 | /* |
||
53 | * Send a link that is opened on the phone |
||
54 | */ |
||
55 | const FORMAT_WAPPUSH = 'WAPPUSH'; |
||
56 | |||
57 | /* |
||
58 | * Array of attachments to send as MMS To send a presentation, the first attachment |
||
59 | * needs to be a SMIL document with the extension .smil Sender should be a valid shortcode |
||
60 | */ |
||
61 | const FORMAT_MMS = 'MMS'; |
||
62 | |||
63 | /** |
||
64 | * @var Recipient[] |
||
65 | */ |
||
66 | protected $recipients; |
||
67 | |||
68 | /** |
||
69 | * @var Sender |
||
70 | */ |
||
71 | protected $sender; |
||
72 | |||
73 | /** |
||
74 | * @var Message |
||
75 | */ |
||
76 | protected $message; |
||
77 | |||
78 | /** |
||
79 | * @var boolean |
||
80 | */ |
||
81 | protected $status; |
||
82 | |||
83 | /** |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $statusUrl; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $returnData; |
||
92 | |||
93 | /** |
||
94 | * @var int |
||
95 | */ |
||
96 | protected $class; |
||
97 | |||
98 | /** |
||
99 | * @var \DateTimeInterface |
||
100 | */ |
||
101 | protected $sendTime; |
||
102 | |||
103 | /** |
||
104 | * @var int |
||
105 | */ |
||
106 | protected $price; |
||
107 | |||
108 | /** |
||
109 | * @var boolean |
||
110 | */ |
||
111 | protected $charity; |
||
112 | |||
113 | /** |
||
114 | * @var string |
||
115 | */ |
||
116 | protected $invoiceText; |
||
117 | |||
118 | /** |
||
119 | * @var int |
||
120 | */ |
||
121 | protected $validity; |
||
122 | |||
123 | /** |
||
124 | * @var integer |
||
125 | */ |
||
126 | protected $contentType; |
||
127 | |||
128 | /** |
||
129 | * @var string |
||
130 | */ |
||
131 | protected $format; |
||
132 | |||
133 | /** |
||
134 | * @var string |
||
135 | */ |
||
136 | protected $udh; |
||
137 | |||
138 | /** |
||
139 | * @var array |
||
140 | */ |
||
141 | protected $attachment; |
||
142 | |||
143 | /** |
||
144 | * @var string |
||
145 | */ |
||
146 | protected $pushUrl; |
||
147 | |||
148 | /** |
||
149 | * @var string |
||
150 | */ |
||
151 | protected $pushExpire; |
||
152 | |||
153 | /** |
||
154 | * @var array |
||
155 | */ |
||
156 | protected $filter; |
||
157 | |||
158 | /** |
||
159 | * @var array |
||
160 | */ |
||
161 | protected $segmentation; |
||
162 | |||
163 | /** |
||
164 | * @var int |
||
165 | */ |
||
166 | protected $pid; |
||
167 | |||
168 | /** |
||
169 | * @var string |
||
170 | */ |
||
171 | protected $advanced; |
||
172 | |||
173 | /** |
||
174 | * @var string |
||
175 | */ |
||
176 | protected $protocol; |
||
177 | |||
178 | /** |
||
179 | * @var string |
||
180 | */ |
||
181 | protected $revenueText; |
||
182 | |||
183 | 3 | public function __construct(Sender $sender, Message $message, array $recipients) |
|
189 | |||
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | 1 | public function validate(): void |
|
223 | |||
224 | 1 | public function getMethod(): string |
|
228 | |||
229 | 1 | public function getUri(): string |
|
233 | |||
234 | 1 | public function getBody(): array |
|
273 | |||
274 | 1 | public function getResponseClass(): string |
|
278 | |||
279 | 3 | public function addRecipient(Recipient $recipient) : PostMessageRequest |
|
284 | |||
285 | /** |
||
286 | * Returns the possible classes for the payload |
||
287 | * |
||
288 | * @return array |
||
289 | */ |
||
290 | 1 | View Code Duplication | public static function getClasses() : array |
299 | |||
300 | /** |
||
301 | * Returns the possible formats for the payload |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | 1 | View Code Duplication | public static function getFormats() : array |
315 | |||
316 | /* |
||
317 | * Getters / Setters |
||
318 | */ |
||
319 | |||
320 | /** |
||
321 | * @return Recipient[] |
||
322 | */ |
||
323 | 1 | public function getRecipients(): array |
|
327 | |||
328 | /** |
||
329 | * @param Recipient[] $recipients |
||
330 | * @return PostMessageRequest |
||
331 | */ |
||
332 | 3 | public function setRecipients(array $recipients) |
|
340 | |||
341 | /** |
||
342 | * @return Sender |
||
343 | */ |
||
344 | 1 | public function getSender(): Sender |
|
348 | |||
349 | /** |
||
350 | * @param Sender $sender |
||
351 | * @return PostMessageRequest |
||
352 | */ |
||
353 | 3 | public function setSender(Sender $sender) |
|
358 | |||
359 | /** |
||
360 | * @return Message |
||
361 | */ |
||
362 | 1 | public function getMessage(): Message |
|
366 | |||
367 | /** |
||
368 | * @param Message $message |
||
369 | * @return PostMessageRequest |
||
370 | */ |
||
371 | 3 | public function setMessage(Message $message) |
|
383 | |||
384 | /** |
||
385 | * @return bool |
||
386 | */ |
||
387 | 1 | public function isStatus(): bool |
|
391 | |||
392 | /** |
||
393 | * @param bool $status |
||
394 | * @return PostMessageRequest |
||
395 | */ |
||
396 | 1 | public function setStatus(bool $status) |
|
401 | |||
402 | /** |
||
403 | * @return string |
||
404 | */ |
||
405 | 1 | public function getStatusUrl(): string |
|
409 | |||
410 | /** |
||
411 | * @param string $statusUrl |
||
412 | * @return PostMessageRequest |
||
413 | */ |
||
414 | 1 | public function setStatusUrl(string $statusUrl) |
|
419 | |||
420 | /** |
||
421 | * @return string |
||
422 | */ |
||
423 | 1 | public function getReturnData(): string |
|
427 | |||
428 | /** |
||
429 | * @param string $returnData |
||
430 | * @return PostMessageRequest |
||
431 | */ |
||
432 | 1 | public function setReturnData(string $returnData) |
|
437 | |||
438 | /** |
||
439 | * @return int |
||
440 | */ |
||
441 | 1 | public function getClass(): int |
|
445 | |||
446 | /** |
||
447 | * @param int $class |
||
448 | * @return PostMessageRequest |
||
449 | */ |
||
450 | 1 | public function setClass(int $class) |
|
455 | |||
456 | /** |
||
457 | * @return \DateTimeInterface |
||
458 | */ |
||
459 | 1 | public function getSendTime(): \DateTimeInterface |
|
463 | |||
464 | /** |
||
465 | * @param \DateTimeInterface $sendTime |
||
466 | * @return PostMessageRequest |
||
467 | */ |
||
468 | 1 | public function setSendTime(\DateTimeInterface $sendTime) |
|
473 | |||
474 | /** |
||
475 | * @return int |
||
476 | */ |
||
477 | 1 | public function getPrice(): int |
|
481 | |||
482 | /** |
||
483 | * @param int $price |
||
484 | * @return PostMessageRequest |
||
485 | */ |
||
486 | 1 | public function setPrice(int $price) |
|
491 | |||
492 | /** |
||
493 | * @return bool |
||
494 | */ |
||
495 | 1 | public function isCharity(): bool |
|
499 | |||
500 | /** |
||
501 | * @param bool $charity |
||
502 | * @return PostMessageRequest |
||
503 | */ |
||
504 | 1 | public function setCharity(bool $charity) |
|
509 | |||
510 | /** |
||
511 | * @return string |
||
512 | */ |
||
513 | 1 | public function getInvoiceText(): string |
|
517 | |||
518 | /** |
||
519 | * @param string $invoiceText |
||
520 | * @return PostMessageRequest |
||
521 | */ |
||
522 | 1 | public function setInvoiceText(string $invoiceText) |
|
527 | |||
528 | /** |
||
529 | * @return int |
||
530 | */ |
||
531 | 1 | public function getValidity(): int |
|
535 | |||
536 | /** |
||
537 | * @param int|\DateInterval $validity In minutes |
||
538 | * @return PostMessageRequest |
||
539 | */ |
||
540 | 1 | public function setValidity($validity) |
|
553 | |||
554 | /** |
||
555 | * @return int |
||
556 | */ |
||
557 | 1 | public function getContentType(): int |
|
561 | |||
562 | /** |
||
563 | * @param int $contentType |
||
564 | * @return PostMessageRequest |
||
565 | */ |
||
566 | 1 | public function setContentType(int $contentType) |
|
571 | |||
572 | /** |
||
573 | * @return string |
||
574 | */ |
||
575 | 2 | public function getFormat(): string |
|
579 | |||
580 | /** |
||
581 | * @param string $format |
||
582 | * @return PostMessageRequest |
||
583 | */ |
||
584 | 3 | public function setFormat(string $format) |
|
589 | |||
590 | /** |
||
591 | * @return string |
||
592 | */ |
||
593 | 1 | public function getUdh(): string |
|
597 | |||
598 | /** |
||
599 | * @param string $udh |
||
600 | * @return PostMessageRequest |
||
601 | */ |
||
602 | 1 | public function setUdh(string $udh) |
|
607 | |||
608 | /** |
||
609 | * @return array |
||
610 | */ |
||
611 | 1 | public function getAttachment(): array |
|
615 | |||
616 | /** |
||
617 | * @param array $attachment |
||
618 | * @return PostMessageRequest |
||
619 | */ |
||
620 | 1 | public function setAttachment(array $attachment) |
|
625 | |||
626 | /** |
||
627 | * @return string |
||
628 | */ |
||
629 | 1 | public function getPushUrl(): string |
|
633 | |||
634 | /** |
||
635 | * @param string $pushUrl |
||
636 | * @return PostMessageRequest |
||
637 | */ |
||
638 | 1 | public function setPushUrl(string $pushUrl) |
|
643 | |||
644 | /** |
||
645 | * @return string |
||
646 | */ |
||
647 | 1 | public function getPushExpire(): string |
|
651 | |||
652 | /** |
||
653 | * @param string|\DateTimeInterface $pushExpire |
||
654 | * @return PostMessageRequest |
||
655 | */ |
||
656 | 1 | public function setPushExpire($pushExpire) |
|
665 | |||
666 | /** |
||
667 | * @return array |
||
668 | */ |
||
669 | 1 | public function getFilter(): array |
|
673 | |||
674 | /** |
||
675 | * @param array $filter |
||
676 | * @return PostMessageRequest |
||
677 | */ |
||
678 | 1 | public function setFilter(array $filter) |
|
683 | |||
684 | /** |
||
685 | * @return array |
||
686 | */ |
||
687 | 1 | public function getSegmentation(): array |
|
691 | |||
692 | /** |
||
693 | * @param array $segmentation |
||
694 | * @return PostMessageRequest |
||
695 | */ |
||
696 | 1 | public function setSegmentation(array $segmentation) |
|
701 | |||
702 | /** |
||
703 | * @return int |
||
704 | */ |
||
705 | 1 | public function getPid(): int |
|
709 | |||
710 | /** |
||
711 | * @param int $pid |
||
712 | * @return PostMessageRequest |
||
713 | */ |
||
714 | 1 | public function setPid(int $pid) |
|
719 | |||
720 | /** |
||
721 | * @return string |
||
722 | */ |
||
723 | 1 | public function getAdvanced(): string |
|
727 | |||
728 | /** |
||
729 | * @param string $advanced |
||
730 | * @return PostMessageRequest |
||
731 | */ |
||
732 | 1 | public function setAdvanced(string $advanced) |
|
737 | |||
738 | /** |
||
739 | * @return string |
||
740 | */ |
||
741 | 1 | public function getProtocol(): string |
|
745 | |||
746 | /** |
||
747 | * @param string $protocol |
||
748 | * @return PostMessageRequest |
||
749 | */ |
||
750 | 1 | public function setProtocol(string $protocol) |
|
755 | |||
756 | /** |
||
757 | * @return string |
||
758 | */ |
||
759 | 1 | public function getRevenueText(): string |
|
763 | |||
764 | /** |
||
765 | * @param string $revenueText |
||
766 | * @return PostMessageRequest |
||
767 | */ |
||
768 | 1 | public function setRevenueText(string $revenueText) |
|
773 | } |
||
774 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.