Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Request |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Telegram object |
||
| 23 | * |
||
| 24 | * @var \Longman\TelegramBot\Telegram |
||
| 25 | */ |
||
| 26 | private static $telegram; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * URI of the Telegram API |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private static $api_base_uri = 'https://api.telegram.org'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Guzzle Client object |
||
| 37 | * |
||
| 38 | * @var \GuzzleHttp\Client |
||
| 39 | */ |
||
| 40 | private static $client; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Input value of the request |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private static $input; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Available actions to send |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private static $actions = [ |
||
| 55 | 'getUpdates', |
||
| 56 | 'setWebhook', |
||
| 57 | 'getMe', |
||
| 58 | 'sendMessage', |
||
| 59 | 'forwardMessage', |
||
| 60 | 'sendPhoto', |
||
| 61 | 'sendAudio', |
||
| 62 | 'sendDocument', |
||
| 63 | 'sendSticker', |
||
| 64 | 'sendVideo', |
||
| 65 | 'sendVoice', |
||
| 66 | 'sendLocation', |
||
| 67 | 'sendVenue', |
||
| 68 | 'sendContact', |
||
| 69 | 'sendChatAction', |
||
| 70 | 'getUserProfilePhotos', |
||
| 71 | 'getFile', |
||
| 72 | 'kickChatMember', |
||
| 73 | 'leaveChat', |
||
| 74 | 'unbanChatMember', |
||
| 75 | 'getChat', |
||
| 76 | 'getChatAdministrators', |
||
| 77 | 'getChatMember', |
||
| 78 | 'getChatMembersCount', |
||
| 79 | 'answerCallbackQuery', |
||
| 80 | 'answerInlineQuery', |
||
| 81 | 'editMessageText', |
||
| 82 | 'editMessageCaption', |
||
| 83 | 'editMessageReplyMarkup', |
||
| 84 | ]; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Initialize |
||
| 88 | * |
||
| 89 | * @param \Longman\TelegramBot\Telegram $telegram |
||
| 90 | * |
||
| 91 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 92 | */ |
||
| 93 | 39 | public static function initialize(Telegram $telegram) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Set input from custom input or stdin and return it |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 108 | */ |
||
| 109 | public static function getInput() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Generate general fake server response |
||
| 130 | * |
||
| 131 | * @param array $data Data to add to fake response |
||
| 132 | * |
||
| 133 | * @return array Fake response data |
||
| 134 | */ |
||
| 135 | 6 | public static function generateGeneralFakeServerResponse(array $data = []) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Properly set up the request params |
||
| 168 | * |
||
| 169 | * If any item of the array is a resource, reformat it to a multipart request. |
||
| 170 | * Else, just return the passed data as form params. |
||
| 171 | * |
||
| 172 | * @param array $data |
||
| 173 | * |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | private static function setUpRequestParams(array $data) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Execute HTTP Request |
||
| 195 | * |
||
| 196 | * @param string $action Action to execute |
||
| 197 | * @param array $data Data to attach to the execution |
||
| 198 | * |
||
| 199 | * @return mixed Result of the HTTP Request |
||
| 200 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 201 | */ |
||
| 202 | public static function execute($action, array $data = []) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Download file |
||
| 237 | * |
||
| 238 | * @param \Longman\TelegramBot\Entities\File $file |
||
| 239 | * |
||
| 240 | * @return boolean |
||
| 241 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 242 | */ |
||
| 243 | public static function downloadFile(File $file) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Encode file |
||
| 274 | * |
||
| 275 | * @param string $file |
||
| 276 | * |
||
| 277 | * @return resource |
||
| 278 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 279 | */ |
||
| 280 | protected static function encodeFile($file) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Send command |
||
| 292 | * |
||
| 293 | * @todo Fake response doesn't need json encoding? |
||
| 294 | * |
||
| 295 | * @param string $action |
||
| 296 | * @param array $data |
||
| 297 | * |
||
| 298 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
| 299 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 300 | */ |
||
| 301 | 5 | public static function send($action, array $data = []) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Make sure the data isn't empty, else throw an exception |
||
| 326 | * |
||
| 327 | * @param array $data |
||
| 328 | * |
||
| 329 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 330 | */ |
||
| 331 | private static function ensureNonEmptyData(array $data) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Make sure the action is valid, else throw an exception |
||
| 340 | * |
||
| 341 | * @param string $action |
||
| 342 | * |
||
| 343 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 344 | */ |
||
| 345 | 5 | private static function ensureValidAction($action) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Assign an encoded file to a data array |
||
| 354 | * |
||
| 355 | * @param array $data |
||
| 356 | * @param string $field |
||
| 357 | * @param string $file |
||
| 358 | * |
||
| 359 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 360 | */ |
||
| 361 | private static function assignEncodedFile(&$data, $field, $file) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get me |
||
| 370 | * |
||
| 371 | * @return mixed |
||
| 372 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 373 | */ |
||
| 374 | public static function getMe() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Send message |
||
| 383 | * |
||
| 384 | * @param array $data |
||
| 385 | * |
||
| 386 | * @return mixed |
||
| 387 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 388 | */ |
||
| 389 | 5 | public static function sendMessage(array $data) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Forward message |
||
| 407 | * |
||
| 408 | * @param array $data |
||
| 409 | * |
||
| 410 | * @return mixed |
||
| 411 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 412 | */ |
||
| 413 | public static function forwardMessage(array $data) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Send photo |
||
| 420 | * |
||
| 421 | * @param array $data |
||
| 422 | * @param string $file |
||
| 423 | * |
||
| 424 | * @return mixed |
||
| 425 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 426 | */ |
||
| 427 | public static function sendPhoto(array $data, $file = null) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Send audio |
||
| 436 | * |
||
| 437 | * @param array $data |
||
| 438 | * @param string $file |
||
| 439 | * |
||
| 440 | * @return mixed |
||
| 441 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 442 | */ |
||
| 443 | public static function sendAudio(array $data, $file = null) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Send document |
||
| 452 | * |
||
| 453 | * @param array $data |
||
| 454 | * @param string $file |
||
| 455 | * |
||
| 456 | * @return mixed |
||
| 457 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 458 | */ |
||
| 459 | public static function sendDocument(array $data, $file = null) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Send sticker |
||
| 468 | * |
||
| 469 | * @param array $data |
||
| 470 | * @param string $file |
||
| 471 | * |
||
| 472 | * @return mixed |
||
| 473 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 474 | */ |
||
| 475 | public static function sendSticker(array $data, $file = null) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Send video |
||
| 484 | * |
||
| 485 | * @param array $data |
||
| 486 | * @param string $file |
||
| 487 | * |
||
| 488 | * @return mixed |
||
| 489 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 490 | */ |
||
| 491 | public static function sendVideo(array $data, $file = null) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Send voice |
||
| 500 | * |
||
| 501 | * @param array $data |
||
| 502 | * @param string $file |
||
| 503 | * |
||
| 504 | * @return mixed |
||
| 505 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 506 | */ |
||
| 507 | public static function sendVoice(array $data, $file = null) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Send location |
||
| 516 | * |
||
| 517 | * @param array $data |
||
| 518 | * |
||
| 519 | * @return mixed |
||
| 520 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 521 | */ |
||
| 522 | public static function sendLocation(array $data) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Send venue |
||
| 529 | * |
||
| 530 | * @param array $data |
||
| 531 | * |
||
| 532 | * @return mixed |
||
| 533 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 534 | */ |
||
| 535 | public static function sendVenue(array $data) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Send contact |
||
| 542 | * |
||
| 543 | * @param array $data |
||
| 544 | * |
||
| 545 | * @return mixed |
||
| 546 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 547 | */ |
||
| 548 | public static function sendContact(array $data) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Send chat action |
||
| 555 | * |
||
| 556 | * @param array $data |
||
| 557 | * |
||
| 558 | * @return mixed |
||
| 559 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 560 | */ |
||
| 561 | public static function sendChatAction(array $data) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Get user profile photos |
||
| 568 | * |
||
| 569 | * @param array $data |
||
| 570 | * |
||
| 571 | * @return mixed |
||
| 572 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 573 | */ |
||
| 574 | public static function getUserProfilePhotos(array $data) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Get updates |
||
| 585 | * |
||
| 586 | * @param array $data |
||
| 587 | * |
||
| 588 | * @return mixed |
||
| 589 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 590 | */ |
||
| 591 | public static function getUpdates(array $data) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Set webhook |
||
| 598 | * |
||
| 599 | * @param string $url |
||
| 600 | * @param string $file |
||
| 601 | * |
||
| 602 | * @return mixed |
||
| 603 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 604 | */ |
||
| 605 | public static function setWebhook($url = '', $file = null) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Get file |
||
| 616 | * |
||
| 617 | * @param array $data |
||
| 618 | * |
||
| 619 | * @return mixed |
||
| 620 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 621 | */ |
||
| 622 | public static function getFile(array $data) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Kick Chat Member |
||
| 629 | * |
||
| 630 | * @param array $data |
||
| 631 | * |
||
| 632 | * @return mixed |
||
| 633 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 634 | */ |
||
| 635 | public static function kickChatMember(array $data) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Leave Chat |
||
| 642 | * |
||
| 643 | * @param array $data |
||
| 644 | * |
||
| 645 | * @return mixed |
||
| 646 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 647 | */ |
||
| 648 | public static function leaveChat(array $data) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Unban Chat Member |
||
| 655 | * |
||
| 656 | * @param array $data |
||
| 657 | * |
||
| 658 | * @return mixed |
||
| 659 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 660 | */ |
||
| 661 | public static function unbanChatMember(array $data) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get Chat |
||
| 668 | * |
||
| 669 | * @todo add get response in ServerResponse.php? |
||
| 670 | * |
||
| 671 | * @param array $data |
||
| 672 | * |
||
| 673 | * @return mixed |
||
| 674 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 675 | */ |
||
| 676 | public static function getChat(array $data) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Get Chat Administrators |
||
| 683 | * |
||
| 684 | * @todo add get response in ServerResponse.php? |
||
| 685 | * |
||
| 686 | * @param array $data |
||
| 687 | * |
||
| 688 | * @return mixed |
||
| 689 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 690 | */ |
||
| 691 | public static function getChatAdministrators(array $data) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Get Chat Members Count |
||
| 698 | * |
||
| 699 | * @todo add get response in ServerResponse.php? |
||
| 700 | * |
||
| 701 | * @param array $data |
||
| 702 | * |
||
| 703 | * @return mixed |
||
| 704 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 705 | */ |
||
| 706 | public static function getChatMembersCount(array $data) |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Get Chat Member |
||
| 713 | * |
||
| 714 | * @todo add get response in ServerResponse.php? |
||
| 715 | * |
||
| 716 | * @param array $data |
||
| 717 | * |
||
| 718 | * @return mixed |
||
| 719 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 720 | */ |
||
| 721 | public static function getChatMember(array $data) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Answer callback query |
||
| 728 | * |
||
| 729 | * @param array $data |
||
| 730 | * |
||
| 731 | * @return mixed |
||
| 732 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 733 | */ |
||
| 734 | public static function answerCallbackQuery(array $data) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Answer inline query |
||
| 741 | * |
||
| 742 | * @param array $data |
||
| 743 | * |
||
| 744 | * @return mixed |
||
| 745 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 746 | */ |
||
| 747 | public static function answerInlineQuery(array $data) |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Edit message text |
||
| 754 | * |
||
| 755 | * @param array $data |
||
| 756 | * |
||
| 757 | * @return mixed |
||
| 758 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 759 | */ |
||
| 760 | public static function editMessageText(array $data) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Edit message caption |
||
| 767 | * |
||
| 768 | * @param array $data |
||
| 769 | * |
||
| 770 | * @return mixed |
||
| 771 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 772 | */ |
||
| 773 | public static function editMessageCaption(array $data) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Edit message reply markup |
||
| 780 | * |
||
| 781 | * @param array $data |
||
| 782 | * |
||
| 783 | * @return mixed |
||
| 784 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 785 | */ |
||
| 786 | public static function editMessageReplyMarkup(array $data) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Return an empty Server Response |
||
| 793 | * |
||
| 794 | * No request to telegram are sent, this function is used in commands that |
||
| 795 | * don't need to fire a message after execution |
||
| 796 | * |
||
| 797 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
| 798 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 799 | */ |
||
| 800 | public static function emptyResponse() |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Send message to all active chats |
||
| 807 | * |
||
| 808 | * @param string $callback_function |
||
| 809 | * @param array $data |
||
| 810 | * @param boolean $send_groups |
||
| 811 | * @param boolean $send_super_groups |
||
| 812 | * @param boolean $send_users |
||
| 813 | * @param string $date_from |
||
| 814 | * @param string $date_to |
||
| 815 | * |
||
| 816 | * @return array |
||
| 817 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 818 | */ |
||
| 819 | public static function sendToActiveChats( |
||
| 845 | } |
||
| 846 |