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