Complex classes like Message 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 Message, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Message implements PayloadInterface |
||
15 | { |
||
16 | /* |
||
17 | * Show message directly on phone. The message is not saved on the phone. (Also known as flash messages) |
||
18 | */ |
||
19 | const CLASS_0 = 0; |
||
20 | |||
21 | /* |
||
22 | * Save message in phone memory. Either on the phone or in SIM. |
||
23 | */ |
||
24 | const CLASS_1 = 1; |
||
25 | |||
26 | /* |
||
27 | * Message contains SIM data. |
||
28 | */ |
||
29 | const CLASS_2 = 2; |
||
30 | |||
31 | /* |
||
32 | * Message contains info that indicate that it should be |
||
33 | * sent to external units, normally used by terminal equipment. |
||
34 | */ |
||
35 | const CLASS_3 = 3; |
||
36 | |||
37 | /* |
||
38 | * Send normal message (160 chars, but if more than 160 chars, 153 chars per part message) |
||
39 | */ |
||
40 | const FORMAT_GSM = 'GSM'; |
||
41 | |||
42 | /* |
||
43 | * To send speciality chars like chinese letters. A normal message is 160 chars, but ifyou use |
||
44 | * unicode each message can only hold 70 chars (But if more than 70 chars, 67 chars per part message) |
||
45 | */ |
||
46 | const FORMAT_UNICODE = 'UNICODE'; |
||
47 | |||
48 | /* |
||
49 | * Send a binary message body in hex and define udh |
||
50 | */ |
||
51 | const FORMAT_BINARY = 'BINARY'; |
||
52 | |||
53 | /* |
||
54 | * Send a link that is opened on the phone |
||
55 | */ |
||
56 | const FORMAT_WAPPUSH = 'WAPPUSH'; |
||
57 | |||
58 | /* |
||
59 | * Array of attachments to send as MMS To send a presentation, the first attachment |
||
60 | * needs to be a SMIL document with the extension .smil Sender should be a valid shortcode |
||
61 | */ |
||
62 | const FORMAT_MMS = 'MMS'; |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $recipients; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $sender; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $message; |
||
78 | |||
79 | /** |
||
80 | * @var boolean |
||
81 | */ |
||
82 | protected $status; |
||
83 | |||
84 | /** |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $statusUrl; |
||
88 | |||
89 | /** |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $returnData; |
||
93 | |||
94 | /** |
||
95 | * @var int |
||
96 | */ |
||
97 | protected $class; |
||
98 | |||
99 | /** |
||
100 | * @var \DateTimeInterface |
||
101 | */ |
||
102 | protected $sendTime; |
||
103 | |||
104 | /** |
||
105 | * @var int |
||
106 | */ |
||
107 | protected $price; |
||
108 | |||
109 | /** |
||
110 | * @var boolean |
||
111 | */ |
||
112 | protected $charity; |
||
113 | |||
114 | /** |
||
115 | * @var string |
||
116 | */ |
||
117 | protected $invoiceText; |
||
118 | |||
119 | /** |
||
120 | * @var int |
||
121 | */ |
||
122 | protected $validity; |
||
123 | |||
124 | /** |
||
125 | * @var integer |
||
126 | */ |
||
127 | protected $contentType; |
||
128 | |||
129 | /** |
||
130 | * @var string |
||
131 | */ |
||
132 | protected $format; |
||
133 | |||
134 | /** |
||
135 | * @var string |
||
136 | */ |
||
137 | protected $udh; |
||
138 | |||
139 | /** |
||
140 | * @var array |
||
141 | */ |
||
142 | protected $attachment; |
||
143 | |||
144 | /** |
||
145 | * @var string |
||
146 | */ |
||
147 | protected $pushUrl; |
||
148 | |||
149 | /** |
||
150 | * @var string |
||
151 | */ |
||
152 | protected $pushExpire; |
||
153 | |||
154 | /** |
||
155 | * @var array |
||
156 | */ |
||
157 | protected $filter; |
||
158 | |||
159 | /** |
||
160 | * @var array |
||
161 | */ |
||
162 | protected $segmentation; |
||
163 | |||
164 | /** |
||
165 | * @var int |
||
166 | */ |
||
167 | protected $pid; |
||
168 | |||
169 | /** |
||
170 | * @var string |
||
171 | */ |
||
172 | protected $advanced; |
||
173 | |||
174 | /** |
||
175 | * @var string |
||
176 | */ |
||
177 | protected $protocol; |
||
178 | |||
179 | /** |
||
180 | * @var string |
||
181 | */ |
||
182 | protected $revenueText; |
||
183 | |||
184 | public function __construct() |
||
188 | |||
189 | public static function create(string $sender, string $message, array $recipients) |
||
199 | |||
200 | /** |
||
201 | * @inheritdoc |
||
202 | */ |
||
203 | public function getPayload(): array |
||
244 | |||
245 | /** |
||
246 | * @inheritdoc |
||
247 | */ |
||
248 | public function validate(): void |
||
290 | |||
291 | public function addRecipient($recipient) : Message |
||
296 | |||
297 | public function getChunkCount() : int |
||
301 | |||
302 | /** |
||
303 | * Returns the possible classes for the payload |
||
304 | * |
||
305 | * @return array |
||
306 | */ |
||
307 | public static function getClasses() : array |
||
311 | |||
312 | /** |
||
313 | * Returns the possible formats for the payload |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | public static function getFormats() : array |
||
327 | |||
328 | /* |
||
329 | * Getters / Setters |
||
330 | */ |
||
331 | |||
332 | /** |
||
333 | * @return array |
||
334 | */ |
||
335 | public function getRecipients(): array |
||
339 | |||
340 | /** |
||
341 | * @param array $recipients |
||
342 | * @return Message |
||
343 | */ |
||
344 | public function setRecipients(array $recipients) |
||
349 | |||
350 | /** |
||
351 | * @return string |
||
352 | */ |
||
353 | public function getSender(): string |
||
357 | |||
358 | /** |
||
359 | * @param string $sender |
||
360 | * @return Message |
||
361 | */ |
||
362 | public function setSender(string $sender) |
||
367 | |||
368 | /** |
||
369 | * @return string |
||
370 | */ |
||
371 | public function getMessage(): string |
||
375 | |||
376 | /** |
||
377 | * @param string $message |
||
378 | * @return Message |
||
379 | */ |
||
380 | public function setMessage(string $message) |
||
385 | |||
386 | /** |
||
387 | * @return bool |
||
388 | */ |
||
389 | public function isStatus(): bool |
||
393 | |||
394 | /** |
||
395 | * @param bool $status |
||
396 | * @return Message |
||
397 | */ |
||
398 | public function setStatus(bool $status) |
||
403 | |||
404 | /** |
||
405 | * @return string |
||
406 | */ |
||
407 | public function getStatusUrl(): string |
||
411 | |||
412 | /** |
||
413 | * @param string $statusUrl |
||
414 | * @return Message |
||
415 | */ |
||
416 | public function setStatusUrl(string $statusUrl) |
||
421 | |||
422 | /** |
||
423 | * @return string |
||
424 | */ |
||
425 | public function getReturnData(): string |
||
429 | |||
430 | /** |
||
431 | * @param string $returnData |
||
432 | * @return Message |
||
433 | */ |
||
434 | public function setReturnData(string $returnData) |
||
439 | |||
440 | /** |
||
441 | * @return int |
||
442 | */ |
||
443 | public function getClass(): int |
||
447 | |||
448 | /** |
||
449 | * @param int $class |
||
450 | * @return Message |
||
451 | */ |
||
452 | public function setClass(int $class) |
||
457 | |||
458 | /** |
||
459 | * @return \DateTimeInterface |
||
460 | */ |
||
461 | public function getSendTime(): \DateTimeInterface |
||
465 | |||
466 | /** |
||
467 | * @param \DateTimeInterface $sendTime |
||
468 | * @return Message |
||
469 | */ |
||
470 | public function setSendTime(\DateTimeInterface $sendTime) |
||
475 | |||
476 | /** |
||
477 | * @return int |
||
478 | */ |
||
479 | public function getPrice(): int |
||
483 | |||
484 | /** |
||
485 | * @param int $price |
||
486 | * @return Message |
||
487 | */ |
||
488 | public function setPrice(int $price) |
||
493 | |||
494 | /** |
||
495 | * @return bool |
||
496 | */ |
||
497 | public function isCharity(): bool |
||
501 | |||
502 | /** |
||
503 | * @param bool $charity |
||
504 | * @return Message |
||
505 | */ |
||
506 | public function setCharity(bool $charity) |
||
511 | |||
512 | /** |
||
513 | * @return string |
||
514 | */ |
||
515 | public function getInvoiceText(): string |
||
519 | |||
520 | /** |
||
521 | * @param string $invoiceText |
||
522 | * @return Message |
||
523 | */ |
||
524 | public function setInvoiceText(string $invoiceText) |
||
529 | |||
530 | /** |
||
531 | * @return int |
||
532 | */ |
||
533 | public function getValidity(): int |
||
537 | |||
538 | /** |
||
539 | * @param int $validity |
||
540 | * @return Message |
||
541 | */ |
||
542 | public function setValidity(int $validity) |
||
547 | |||
548 | /** |
||
549 | * @return int |
||
550 | */ |
||
551 | public function getContentType(): int |
||
555 | |||
556 | /** |
||
557 | * @param int $contentType |
||
558 | * @return Message |
||
559 | */ |
||
560 | public function setContentType(int $contentType) |
||
565 | |||
566 | /** |
||
567 | * @return string |
||
568 | */ |
||
569 | public function getFormat(): string |
||
573 | |||
574 | /** |
||
575 | * @param string $format |
||
576 | * @return Message |
||
577 | */ |
||
578 | public function setFormat(string $format) |
||
583 | |||
584 | /** |
||
585 | * @return string |
||
586 | */ |
||
587 | public function getUdh(): string |
||
591 | |||
592 | /** |
||
593 | * @param string $udh |
||
594 | * @return Message |
||
595 | */ |
||
596 | public function setUdh(string $udh) |
||
601 | |||
602 | /** |
||
603 | * @return array |
||
604 | */ |
||
605 | public function getAttachment(): array |
||
609 | |||
610 | /** |
||
611 | * @param array $attachment |
||
612 | * @return Message |
||
613 | */ |
||
614 | public function setAttachment(array $attachment) |
||
619 | |||
620 | /** |
||
621 | * @return string |
||
622 | */ |
||
623 | public function getPushUrl(): string |
||
627 | |||
628 | /** |
||
629 | * @param string $pushUrl |
||
630 | * @return Message |
||
631 | */ |
||
632 | public function setPushUrl(string $pushUrl) |
||
637 | |||
638 | /** |
||
639 | * @return string |
||
640 | */ |
||
641 | public function getPushExpire(): string |
||
645 | |||
646 | /** |
||
647 | * @param string $pushExpire |
||
648 | * @return Message |
||
649 | */ |
||
650 | public function setPushExpire(string $pushExpire) |
||
655 | |||
656 | /** |
||
657 | * @return array |
||
658 | */ |
||
659 | public function getFilter(): array |
||
663 | |||
664 | /** |
||
665 | * @param array $filter |
||
666 | * @return Message |
||
667 | */ |
||
668 | public function setFilter(array $filter) |
||
673 | |||
674 | /** |
||
675 | * @return array |
||
676 | */ |
||
677 | public function getSegmentation(): array |
||
681 | |||
682 | /** |
||
683 | * @param array $segmentation |
||
684 | * @return Message |
||
685 | */ |
||
686 | public function setSegmentation(array $segmentation) |
||
691 | |||
692 | /** |
||
693 | * @return int |
||
694 | */ |
||
695 | public function getPid(): int |
||
699 | |||
700 | /** |
||
701 | * @param int $pid |
||
702 | * @return Message |
||
703 | */ |
||
704 | public function setPid(int $pid) |
||
709 | |||
710 | /** |
||
711 | * @return string |
||
712 | */ |
||
713 | public function getAdvanced(): string |
||
717 | |||
718 | /** |
||
719 | * @param string $advanced |
||
720 | * @return Message |
||
721 | */ |
||
722 | public function setAdvanced(string $advanced) |
||
727 | |||
728 | /** |
||
729 | * @return string |
||
730 | */ |
||
731 | public function getProtocol(): string |
||
735 | |||
736 | /** |
||
737 | * @param string $protocol |
||
738 | * @return Message |
||
739 | */ |
||
740 | public function setProtocol(string $protocol) |
||
745 | |||
746 | /** |
||
747 | * @return string |
||
748 | */ |
||
749 | public function getRevenueText(): string |
||
753 | |||
754 | /** |
||
755 | * @param string $revenueText |
||
756 | * @return Message |
||
757 | */ |
||
758 | public function setRevenueText(string $revenueText) |
||
763 | } |
||
764 |