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 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Unique message identifier |
||
| 55 | * |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | protected $messageId; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Optional. Sender name. Can be empty for messages sent to channels |
||
| 62 | * |
||
| 63 | * @var \TelegramBot\Api\Types\User |
||
| 64 | */ |
||
| 65 | protected $from; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Date the message was sent in Unix time |
||
| 69 | * |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | protected $date; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Conversation the message belongs to — user in case of a private message, GroupChat in case of a group |
||
| 76 | * |
||
| 77 | * @var \TelegramBot\Api\Types\Chat |
||
| 78 | */ |
||
| 79 | protected $chat; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Optional. For forwarded messages, sender of the original message |
||
| 83 | * |
||
| 84 | * @var \TelegramBot\Api\Types\User |
||
| 85 | */ |
||
| 86 | protected $forwardFrom; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Optional. For forwarded messages, date the original message was sent in Unix time |
||
| 90 | * |
||
| 91 | * @var int |
||
| 92 | */ |
||
| 93 | protected $forwardDate; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Optional. For replies, the original message. Note that the Message object in this field will not contain further |
||
| 97 | * reply_to_message fields even if it itself is a reply. |
||
| 98 | * |
||
| 99 | * @var \TelegramBot\Api\Types\Message |
||
| 100 | */ |
||
| 101 | protected $replyToMessage; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Optional. For text messages, the actual UTF-8 text of the message |
||
| 105 | * |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | protected $text; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text. |
||
| 112 | * array of \TelegramBot\Api\Types\MessageEntity |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $entities; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Optional. Message is an audio file, information about the file |
||
| 120 | * |
||
| 121 | * @var \TelegramBot\Api\Types\Audio |
||
| 122 | */ |
||
| 123 | protected $audio; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Optional. Message is a general file, information about the file |
||
| 127 | * |
||
| 128 | * @var \TelegramBot\Api\Types\Document |
||
| 129 | */ |
||
| 130 | protected $document; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Optional. Message is a photo, available sizes of the photo |
||
| 134 | * array of \TelegramBot\Api\Types\Photo |
||
| 135 | * |
||
| 136 | * @var array |
||
| 137 | */ |
||
| 138 | protected $photo; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Optional. Message is a sticker, information about the sticker |
||
| 142 | * |
||
| 143 | * @var \TelegramBot\Api\Types\Sticker |
||
| 144 | */ |
||
| 145 | protected $sticker; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Optional. Message is a video, information about the video |
||
| 149 | * |
||
| 150 | * @var \TelegramBot\Api\Types\Video |
||
| 151 | */ |
||
| 152 | protected $video; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Optional. Message is a voice message, information about the file |
||
| 156 | * |
||
| 157 | * @var \TelegramBot\Api\Types\Voice |
||
| 158 | */ |
||
| 159 | protected $voice; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Optional. Message is a shared contact, information about the contact |
||
| 163 | * |
||
| 164 | * @var \TelegramBot\Api\Types\Contact |
||
| 165 | */ |
||
| 166 | protected $contact; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Optional. Message is a shared location, information about the location |
||
| 170 | * |
||
| 171 | * @var \TelegramBot\Api\Types\Location |
||
| 172 | */ |
||
| 173 | protected $location; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Optional. A new member was added to the group, information about them (this member may be bot itself) |
||
| 177 | * |
||
| 178 | * @var \TelegramBot\Api\Types\User |
||
| 179 | */ |
||
| 180 | protected $newChatMember; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Optional. A member was removed from the group, information about them (this member may be bot itself) |
||
| 184 | * |
||
| 185 | * @var \TelegramBot\Api\Types\User |
||
| 186 | */ |
||
| 187 | protected $leftChatMember; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Optional. A group title was changed to this value |
||
| 191 | * |
||
| 192 | * @var string |
||
| 193 | */ |
||
| 194 | protected $newChatTitle; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Optional. A group photo was change to this value |
||
| 198 | * |
||
| 199 | * @var mixed |
||
| 200 | */ |
||
| 201 | protected $newChatPhoto; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Optional. Informs that the group photo was deleted |
||
| 205 | * |
||
| 206 | * @var bool |
||
| 207 | */ |
||
| 208 | protected $deleteChatPhoto; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Optional. Informs that the group has been created |
||
| 212 | * |
||
| 213 | * @var bool |
||
| 214 | */ |
||
| 215 | protected $groupChatCreated; |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Optional. Text description of the video (usually empty) |
||
| 219 | * |
||
| 220 | * @var string |
||
| 221 | */ |
||
| 222 | protected $caption; |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * Optional. Service message: the supergroup has been created |
||
| 227 | * |
||
| 228 | * @var bool |
||
| 229 | */ |
||
| 230 | protected $supergroupChatCreated; |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Optional. Service message: the channel has been created |
||
| 234 | * |
||
| 235 | * @var bool |
||
| 236 | */ |
||
| 237 | protected $channelChatCreated; |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Optional. The group has been migrated to a supergroup with the specified identifier, |
||
| 241 | * not exceeding 1e13 by absolute value |
||
| 242 | * |
||
| 243 | * @var int |
||
| 244 | */ |
||
| 245 | protected $migrateToChatId; |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Optional. The supergroup has been migrated from a group with the specified identifier, |
||
| 249 | * not exceeding 1e13 by absolute value |
||
| 250 | * |
||
| 251 | * @var int |
||
| 252 | */ |
||
| 253 | protected $migrateFromChatId; |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | 1 | public function getCaption() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $caption |
||
| 265 | */ |
||
| 266 | 2 | public function setCaption($caption) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @return Audio |
||
| 273 | */ |
||
| 274 | 1 | public function getAudio() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param Audio $audio |
||
| 281 | */ |
||
| 282 | 2 | public function setAudio(Audio $audio) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @return TypeInterface |
||
| 289 | */ |
||
| 290 | 2 | public function getChat() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param Chat $chat |
||
| 297 | */ |
||
| 298 | 13 | public function setChat(Chat $chat) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @return Contact |
||
| 305 | */ |
||
| 306 | 1 | public function getContact() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @param Contact $contact |
||
| 313 | */ |
||
| 314 | 2 | public function setContact(Contact $contact) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @return int |
||
| 321 | */ |
||
| 322 | 1 | public function getDate() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @param int $date |
||
| 329 | * |
||
| 330 | * @throws InvalidArgumentException |
||
| 331 | */ |
||
| 332 | 12 | public function setDate($date) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @return boolean |
||
| 343 | */ |
||
| 344 | 1 | public function isDeleteChatPhoto() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param boolean $deleteChatPhoto |
||
| 351 | */ |
||
| 352 | 2 | public function setDeleteChatPhoto($deleteChatPhoto) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @return Document |
||
| 359 | */ |
||
| 360 | 1 | public function getDocument() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @param Document $document |
||
| 367 | */ |
||
| 368 | 2 | public function setDocument($document) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @return int |
||
| 375 | */ |
||
| 376 | 1 | public function getForwardDate() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @param int $forwardDate |
||
| 383 | * |
||
| 384 | * @throws InvalidArgumentException |
||
| 385 | */ |
||
| 386 | 3 | public function setForwardDate($forwardDate) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @return User |
||
| 397 | */ |
||
| 398 | 1 | public function getForwardFrom() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @param User $forwardFrom |
||
| 405 | */ |
||
| 406 | 2 | public function setForwardFrom(User $forwardFrom) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * @return boolean |
||
| 413 | */ |
||
| 414 | 1 | public function isGroupChatCreated() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @param boolean $groupChatCreated |
||
| 421 | */ |
||
| 422 | 2 | public function setGroupChatCreated($groupChatCreated) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @return User |
||
| 429 | */ |
||
| 430 | 1 | public function getLeftChatMember() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @param User $leftChatMember |
||
| 437 | */ |
||
| 438 | 2 | public function setLeftChatMember($leftChatMember) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * @return Location |
||
| 445 | */ |
||
| 446 | 1 | public function getLocation() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @param Location $location |
||
| 453 | */ |
||
| 454 | 2 | public function setLocation(Location $location) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * @return int |
||
| 461 | */ |
||
| 462 | 1 | public function getMessageId() |
|
| 466 | |||
| 467 | /** |
||
| 468 | * @param int $messageId |
||
| 469 | * |
||
| 470 | * @throws InvalidArgumentException |
||
| 471 | */ |
||
| 472 | 12 | public function setMessageId($messageId) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * @return User |
||
| 483 | */ |
||
| 484 | 1 | public function getNewChatMember() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * @param User $newChatMember |
||
| 491 | */ |
||
| 492 | 2 | public function setNewChatMember($newChatMember) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * @return array |
||
| 499 | */ |
||
| 500 | 1 | public function getNewChatPhoto() |
|
| 504 | |||
| 505 | /** |
||
| 506 | * @param array $newChatPhoto |
||
| 507 | */ |
||
| 508 | 2 | public function setNewChatPhoto($newChatPhoto) |
|
| 512 | |||
| 513 | /** |
||
| 514 | * @return string |
||
| 515 | */ |
||
| 516 | 1 | public function getNewChatTitle() |
|
| 520 | |||
| 521 | /** |
||
| 522 | * @param string $newChatTitle |
||
| 523 | */ |
||
| 524 | 2 | public function setNewChatTitle($newChatTitle) |
|
| 528 | |||
| 529 | /** |
||
| 530 | * @return array |
||
| 531 | */ |
||
| 532 | 1 | public function getPhoto() |
|
| 536 | |||
| 537 | /** |
||
| 538 | * @param array $photo |
||
| 539 | */ |
||
| 540 | 2 | public function setPhoto(array $photo) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * @return Message |
||
| 547 | */ |
||
| 548 | 1 | public function getReplyToMessage() |
|
| 552 | |||
| 553 | /** |
||
| 554 | * @param Message $replyToMessage |
||
| 555 | */ |
||
| 556 | 2 | public function setReplyToMessage(Message $replyToMessage) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * @return Sticker |
||
| 563 | */ |
||
| 564 | 1 | public function getSticker() |
|
| 568 | |||
| 569 | /** |
||
| 570 | * @param Sticker $sticker |
||
| 571 | */ |
||
| 572 | 2 | public function setSticker(Sticker $sticker) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * @return string |
||
| 579 | */ |
||
| 580 | 7 | public function getText() |
|
| 584 | |||
| 585 | /** |
||
| 586 | * @param string $text |
||
| 587 | */ |
||
| 588 | 5 | public function setText($text) |
|
| 592 | |||
| 593 | /** |
||
| 594 | * @return array |
||
| 595 | */ |
||
| 596 | public function getEntities() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @param array $entities |
||
| 603 | */ |
||
| 604 | public function setEntities($entities) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @return User |
||
| 611 | */ |
||
| 612 | 1 | public function getFrom() |
|
| 616 | |||
| 617 | /** |
||
| 618 | * @param User $from |
||
| 619 | */ |
||
| 620 | 11 | public function setFrom(User $from) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * @return Video |
||
| 627 | */ |
||
| 628 | 1 | public function getVideo() |
|
| 632 | |||
| 633 | /** |
||
| 634 | * @param Video $video |
||
| 635 | */ |
||
| 636 | 2 | public function setVideo(Video $video) |
|
| 640 | |||
| 641 | /** |
||
| 642 | * @return Voice |
||
| 643 | */ |
||
| 644 | 1 | public function getVoice() |
|
| 648 | |||
| 649 | /** |
||
| 650 | * @param Voice $voice |
||
| 651 | */ |
||
| 652 | 2 | public function setVoice($voice) |
|
| 656 | |||
| 657 | /** |
||
| 658 | * @param boolean $supergroupChatCreated |
||
| 659 | */ |
||
| 660 | 2 | public function setSupergroupChatCreated($supergroupChatCreated) |
|
| 664 | |||
| 665 | /** |
||
| 666 | * @return boolean |
||
| 667 | */ |
||
| 668 | 1 | public function isSupergroupChatCreated() |
|
| 672 | |||
| 673 | /** |
||
| 674 | * @param boolean $channelChatCreated |
||
| 675 | */ |
||
| 676 | 2 | public function setChannelChatCreated($channelChatCreated) |
|
| 680 | |||
| 681 | /** |
||
| 682 | * @return boolean |
||
| 683 | */ |
||
| 684 | 1 | public function isChannelChatCreated() |
|
| 688 | |||
| 689 | /** |
||
| 690 | * @param int $migrateToChatId |
||
| 691 | */ |
||
| 692 | 2 | public function setMigrateToChatId($migrateToChatId) |
|
| 696 | |||
| 697 | /** |
||
| 698 | * @return int |
||
| 699 | */ |
||
| 700 | 1 | public function getMigrateToChatId() |
|
| 704 | |||
| 705 | /** |
||
| 706 | * @param int $migrateFromChatId |
||
| 707 | */ |
||
| 708 | 2 | public function setMigrateFromChatId($migrateFromChatId) |
|
| 712 | |||
| 713 | /** |
||
| 714 | * @return int |
||
| 715 | */ |
||
| 716 | 1 | public function getMigrateFromChatId() |
|
| 720 | } |
||
| 721 |