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 |
||
8 | class Message extends BaseType implements TypeInterface |
||
9 | { |
||
10 | /** |
||
11 | * {@inheritdoc} |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | static protected $requiredParams = ['message_id', 'date', 'chat']; |
||
16 | |||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | static protected $map = [ |
||
23 | 'message_id' => true, |
||
24 | 'from' => User::class, |
||
25 | 'date' => true, |
||
26 | 'chat' => Chat::class, |
||
27 | 'forward_from' => User::class, |
||
28 | 'forward_date' => true, |
||
29 | 'reply_to_message' => Message::class, |
||
30 | 'text' => true, |
||
31 | 'entities' => ArrayOfMessageEntity::class, |
||
32 | 'audio' => Audio::class, |
||
33 | 'document' => Document::class, |
||
34 | 'photo' => ArrayOfPhotoSize::class, |
||
35 | 'sticker' => Sticker::class, |
||
36 | 'video' => Video::class, |
||
37 | 'voice' => Voice::class, |
||
38 | 'caption' => true, |
||
39 | 'contact' => Contact::class, |
||
40 | 'location' => Location::class, |
||
41 | 'new_chat_member' => User::class, |
||
42 | 'left_chat_member' => User::class, |
||
43 | 'new_chat_title' => true, |
||
44 | 'new_chat_photo' => ArrayOfPhotoSize::class, |
||
45 | 'delete_chat_photo' => true, |
||
46 | 'group_chat_created' => true, |
||
47 | 'supergroup_chat_created' => true, |
||
48 | 'channel_chat_created' => true, |
||
49 | 'migrate_to_chat_id' => true, |
||
50 | 'migrate_from_chat_id' => true, |
||
51 | 'pinned_message' => Message::class, |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * Unique message identifier |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $messageId; |
||
60 | |||
61 | /** |
||
62 | * Optional. Sender name. Can be empty for messages sent to channels |
||
63 | * |
||
64 | * @var \TelegramBot\Api\Types\User |
||
65 | */ |
||
66 | protected $from; |
||
67 | |||
68 | /** |
||
69 | * Date the message was sent in Unix time |
||
70 | * |
||
71 | * @var int |
||
72 | */ |
||
73 | protected $date; |
||
74 | |||
75 | /** |
||
76 | * Conversation the message belongs to — user in case of a private message, GroupChat in case of a group |
||
77 | * |
||
78 | * @var \TelegramBot\Api\Types\Chat |
||
79 | */ |
||
80 | protected $chat; |
||
81 | |||
82 | /** |
||
83 | * Optional. For forwarded messages, sender of the original message |
||
84 | * |
||
85 | * @var \TelegramBot\Api\Types\User |
||
86 | */ |
||
87 | protected $forwardFrom; |
||
88 | |||
89 | /** |
||
90 | * Optional. For forwarded messages, date the original message was sent in Unix time |
||
91 | * |
||
92 | * @var int |
||
93 | */ |
||
94 | protected $forwardDate; |
||
95 | |||
96 | /** |
||
97 | * Optional. For replies, the original message. Note that the Message object in this field will not contain further |
||
98 | * reply_to_message fields even if it itself is a reply. |
||
99 | * |
||
100 | * @var \TelegramBot\Api\Types\Message |
||
101 | */ |
||
102 | protected $replyToMessage; |
||
103 | |||
104 | /** |
||
105 | * Optional. For text messages, the actual UTF-8 text of the message |
||
106 | * |
||
107 | * @var string |
||
108 | */ |
||
109 | protected $text; |
||
110 | |||
111 | /** |
||
112 | * Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text. |
||
113 | * array of \TelegramBot\Api\Types\MessageEntity |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | protected $entities; |
||
118 | |||
119 | /** |
||
120 | * Optional. Message is an audio file, information about the file |
||
121 | * |
||
122 | * @var \TelegramBot\Api\Types\Audio |
||
123 | */ |
||
124 | protected $audio; |
||
125 | |||
126 | /** |
||
127 | * Optional. Message is a general file, information about the file |
||
128 | * |
||
129 | * @var \TelegramBot\Api\Types\Document |
||
130 | */ |
||
131 | protected $document; |
||
132 | |||
133 | /** |
||
134 | * Optional. Message is a photo, available sizes of the photo |
||
135 | * array of \TelegramBot\Api\Types\Photo |
||
136 | * |
||
137 | * @var array |
||
138 | */ |
||
139 | protected $photo; |
||
140 | |||
141 | /** |
||
142 | * Optional. Message is a sticker, information about the sticker |
||
143 | * |
||
144 | * @var \TelegramBot\Api\Types\Sticker |
||
145 | */ |
||
146 | protected $sticker; |
||
147 | |||
148 | /** |
||
149 | * Optional. Message is a video, information about the video |
||
150 | * |
||
151 | * @var \TelegramBot\Api\Types\Video |
||
152 | */ |
||
153 | protected $video; |
||
154 | |||
155 | /** |
||
156 | * Optional. Message is a voice message, information about the file |
||
157 | * |
||
158 | * @var \TelegramBot\Api\Types\Voice |
||
159 | */ |
||
160 | protected $voice; |
||
161 | |||
162 | /** |
||
163 | * Optional. Message is a shared contact, information about the contact |
||
164 | * |
||
165 | * @var \TelegramBot\Api\Types\Contact |
||
166 | */ |
||
167 | protected $contact; |
||
168 | |||
169 | /** |
||
170 | * Optional. Message is a shared location, information about the location |
||
171 | * |
||
172 | * @var \TelegramBot\Api\Types\Location |
||
173 | */ |
||
174 | protected $location; |
||
175 | |||
176 | /** |
||
177 | * Optional. A new member was added to the group, information about them (this member may be bot itself) |
||
178 | * |
||
179 | * @var \TelegramBot\Api\Types\User |
||
180 | */ |
||
181 | protected $newChatMember; |
||
182 | |||
183 | /** |
||
184 | * Optional. A member was removed from the group, information about them (this member may be bot itself) |
||
185 | * |
||
186 | * @var \TelegramBot\Api\Types\User |
||
187 | */ |
||
188 | protected $leftChatMember; |
||
189 | |||
190 | /** |
||
191 | * Optional. A group title was changed to this value |
||
192 | * |
||
193 | * @var string |
||
194 | */ |
||
195 | protected $newChatTitle; |
||
196 | |||
197 | /** |
||
198 | * Optional. A group photo was change to this value |
||
199 | * |
||
200 | * @var mixed |
||
201 | */ |
||
202 | protected $newChatPhoto; |
||
203 | |||
204 | /** |
||
205 | * Optional. Informs that the group photo was deleted |
||
206 | * |
||
207 | * @var bool |
||
208 | */ |
||
209 | protected $deleteChatPhoto; |
||
210 | |||
211 | /** |
||
212 | * Optional. Informs that the group has been created |
||
213 | * |
||
214 | * @var bool |
||
215 | */ |
||
216 | protected $groupChatCreated; |
||
217 | |||
218 | /** |
||
219 | * Optional. Text description of the video (usually empty) |
||
220 | * |
||
221 | * @var string |
||
222 | */ |
||
223 | protected $caption; |
||
224 | |||
225 | |||
226 | /** |
||
227 | * Optional. Service message: the supergroup has been created |
||
228 | * |
||
229 | * @var bool |
||
230 | */ |
||
231 | protected $supergroupChatCreated; |
||
232 | |||
233 | /** |
||
234 | * Optional. Service message: the channel has been created |
||
235 | * |
||
236 | * @var bool |
||
237 | */ |
||
238 | protected $channelChatCreated; |
||
239 | |||
240 | /** |
||
241 | * Optional. The group has been migrated to a supergroup with the specified identifier, |
||
242 | * not exceeding 1e13 by absolute value |
||
243 | * |
||
244 | * @var int |
||
245 | */ |
||
246 | protected $migrateToChatId; |
||
247 | |||
248 | /** |
||
249 | * Optional. The supergroup has been migrated from a group with the specified identifier, |
||
250 | * not exceeding 1e13 by absolute value |
||
251 | * |
||
252 | * @var int |
||
253 | */ |
||
254 | protected $migrateFromChatId; |
||
255 | |||
256 | /** |
||
257 | * Optional. Specified message was pinned.Note that the Message object in this field |
||
258 | * will not contain further reply_to_message fields even if it is itself a reply. |
||
259 | * |
||
260 | * @var Message |
||
261 | */ |
||
262 | protected $pinnedMessage; |
||
263 | |||
264 | /** |
||
265 | * @return string |
||
266 | */ |
||
267 | 1 | public function getCaption() |
|
271 | |||
272 | /** |
||
273 | * @param string $caption |
||
274 | */ |
||
275 | 2 | public function setCaption($caption) |
|
279 | |||
280 | /** |
||
281 | * @return Audio |
||
282 | */ |
||
283 | 1 | public function getAudio() |
|
287 | |||
288 | /** |
||
289 | * @param Audio $audio |
||
290 | */ |
||
291 | 2 | public function setAudio(Audio $audio) |
|
295 | |||
296 | /** |
||
297 | * @return TypeInterface |
||
298 | */ |
||
299 | 2 | public function getChat() |
|
303 | |||
304 | /** |
||
305 | * @param Chat $chat |
||
306 | */ |
||
307 | 13 | public function setChat(Chat $chat) |
|
311 | |||
312 | /** |
||
313 | * @return Contact |
||
314 | */ |
||
315 | 1 | public function getContact() |
|
319 | |||
320 | /** |
||
321 | * @param Contact $contact |
||
322 | */ |
||
323 | 2 | public function setContact(Contact $contact) |
|
327 | |||
328 | /** |
||
329 | * @return int |
||
330 | */ |
||
331 | 1 | public function getDate() |
|
335 | |||
336 | /** |
||
337 | * @param int $date |
||
338 | * |
||
339 | * @throws InvalidArgumentException |
||
340 | */ |
||
341 | 12 | public function setDate($date) |
|
349 | |||
350 | /** |
||
351 | * @return boolean |
||
352 | */ |
||
353 | 1 | public function isDeleteChatPhoto() |
|
357 | |||
358 | /** |
||
359 | * @param boolean $deleteChatPhoto |
||
360 | */ |
||
361 | 2 | public function setDeleteChatPhoto($deleteChatPhoto) |
|
365 | |||
366 | /** |
||
367 | * @return Document |
||
368 | */ |
||
369 | 1 | public function getDocument() |
|
373 | |||
374 | /** |
||
375 | * @param Document $document |
||
376 | */ |
||
377 | 2 | public function setDocument($document) |
|
381 | |||
382 | /** |
||
383 | * @return int |
||
384 | */ |
||
385 | 1 | public function getForwardDate() |
|
389 | |||
390 | /** |
||
391 | * @param int $forwardDate |
||
392 | * |
||
393 | * @throws InvalidArgumentException |
||
394 | */ |
||
395 | 3 | public function setForwardDate($forwardDate) |
|
403 | |||
404 | /** |
||
405 | * @return User |
||
406 | */ |
||
407 | 1 | public function getForwardFrom() |
|
411 | |||
412 | /** |
||
413 | * @param User $forwardFrom |
||
414 | */ |
||
415 | 2 | public function setForwardFrom(User $forwardFrom) |
|
419 | |||
420 | /** |
||
421 | * @return boolean |
||
422 | */ |
||
423 | 1 | public function isGroupChatCreated() |
|
427 | |||
428 | /** |
||
429 | * @param boolean $groupChatCreated |
||
430 | */ |
||
431 | 2 | public function setGroupChatCreated($groupChatCreated) |
|
435 | |||
436 | /** |
||
437 | * @return User |
||
438 | */ |
||
439 | 1 | public function getLeftChatMember() |
|
443 | |||
444 | /** |
||
445 | * @param User $leftChatMember |
||
446 | */ |
||
447 | 2 | public function setLeftChatMember($leftChatMember) |
|
451 | |||
452 | /** |
||
453 | * @return Location |
||
454 | */ |
||
455 | 1 | public function getLocation() |
|
459 | |||
460 | /** |
||
461 | * @param Location $location |
||
462 | */ |
||
463 | 2 | public function setLocation(Location $location) |
|
467 | |||
468 | /** |
||
469 | * @return int |
||
470 | */ |
||
471 | 1 | public function getMessageId() |
|
475 | |||
476 | /** |
||
477 | * @param int $messageId |
||
478 | * |
||
479 | * @throws InvalidArgumentException |
||
480 | */ |
||
481 | 12 | public function setMessageId($messageId) |
|
489 | |||
490 | /** |
||
491 | * @return User |
||
492 | */ |
||
493 | 1 | public function getNewChatMember() |
|
497 | |||
498 | /** |
||
499 | * @param User $newChatMember |
||
500 | */ |
||
501 | 2 | public function setNewChatMember($newChatMember) |
|
505 | |||
506 | /** |
||
507 | * @return array |
||
508 | */ |
||
509 | 1 | public function getNewChatPhoto() |
|
513 | |||
514 | /** |
||
515 | * @param array $newChatPhoto |
||
516 | */ |
||
517 | 2 | public function setNewChatPhoto($newChatPhoto) |
|
521 | |||
522 | /** |
||
523 | * @return string |
||
524 | */ |
||
525 | 1 | public function getNewChatTitle() |
|
529 | |||
530 | /** |
||
531 | * @param string $newChatTitle |
||
532 | */ |
||
533 | 2 | public function setNewChatTitle($newChatTitle) |
|
537 | |||
538 | /** |
||
539 | * @return array |
||
540 | */ |
||
541 | 1 | public function getPhoto() |
|
545 | |||
546 | /** |
||
547 | * @param array $photo |
||
548 | */ |
||
549 | 2 | public function setPhoto(array $photo) |
|
553 | |||
554 | /** |
||
555 | * @return Message |
||
556 | */ |
||
557 | 1 | public function getReplyToMessage() |
|
561 | |||
562 | /** |
||
563 | * @param Message $replyToMessage |
||
564 | */ |
||
565 | 2 | public function setReplyToMessage(Message $replyToMessage) |
|
569 | |||
570 | /** |
||
571 | * @return Sticker |
||
572 | */ |
||
573 | 1 | public function getSticker() |
|
577 | |||
578 | /** |
||
579 | * @param Sticker $sticker |
||
580 | */ |
||
581 | 2 | public function setSticker(Sticker $sticker) |
|
585 | |||
586 | /** |
||
587 | * @return string |
||
588 | */ |
||
589 | 7 | public function getText() |
|
593 | |||
594 | /** |
||
595 | * @param string $text |
||
596 | */ |
||
597 | 5 | public function setText($text) |
|
601 | |||
602 | /** |
||
603 | * @return array |
||
604 | */ |
||
605 | public function getEntities() |
||
609 | |||
610 | /** |
||
611 | * @param array $entities |
||
612 | */ |
||
613 | public function setEntities($entities) |
||
617 | |||
618 | /** |
||
619 | * @return User |
||
620 | */ |
||
621 | 1 | public function getFrom() |
|
625 | |||
626 | /** |
||
627 | * @param User $from |
||
628 | */ |
||
629 | 11 | public function setFrom(User $from) |
|
633 | |||
634 | /** |
||
635 | * @return Video |
||
636 | */ |
||
637 | 1 | public function getVideo() |
|
641 | |||
642 | /** |
||
643 | * @param Video $video |
||
644 | */ |
||
645 | 2 | public function setVideo(Video $video) |
|
649 | |||
650 | /** |
||
651 | * @return Voice |
||
652 | */ |
||
653 | 1 | public function getVoice() |
|
657 | |||
658 | /** |
||
659 | * @param Voice $voice |
||
660 | */ |
||
661 | 2 | public function setVoice($voice) |
|
665 | |||
666 | /** |
||
667 | * @param boolean $supergroupChatCreated |
||
668 | */ |
||
669 | 2 | public function setSupergroupChatCreated($supergroupChatCreated) |
|
673 | |||
674 | /** |
||
675 | * @return boolean |
||
676 | */ |
||
677 | 1 | public function isSupergroupChatCreated() |
|
681 | |||
682 | /** |
||
683 | * @param boolean $channelChatCreated |
||
684 | */ |
||
685 | 2 | public function setChannelChatCreated($channelChatCreated) |
|
689 | |||
690 | /** |
||
691 | * @return boolean |
||
692 | */ |
||
693 | 1 | public function isChannelChatCreated() |
|
697 | |||
698 | /** |
||
699 | * @param int $migrateToChatId |
||
700 | */ |
||
701 | 2 | public function setMigrateToChatId($migrateToChatId) |
|
705 | |||
706 | /** |
||
707 | * @return int |
||
708 | */ |
||
709 | 1 | public function getMigrateToChatId() |
|
713 | |||
714 | /** |
||
715 | * @param int $migrateFromChatId |
||
716 | */ |
||
717 | 2 | public function setMigrateFromChatId($migrateFromChatId) |
|
721 | |||
722 | /** |
||
723 | * @return int |
||
724 | */ |
||
725 | 1 | public function getMigrateFromChatId() |
|
729 | |||
730 | /** |
||
731 | * @return Message |
||
732 | */ |
||
733 | public function getPinnedMessage() |
||
737 | |||
738 | /** |
||
739 | * @param Message $pinnedMessage |
||
740 | */ |
||
741 | public function setPinnedMessage($pinnedMessage) |
||
745 | } |
||
746 |