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 | 'getWebhookInfo', |
||
85 | ]; |
||
86 | |||
87 | /** |
||
88 | * Initialize |
||
89 | * |
||
90 | * @param \Longman\TelegramBot\Telegram $telegram |
||
91 | * |
||
92 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
93 | */ |
||
94 | 39 | public static function initialize(Telegram $telegram) |
|
103 | |||
104 | /** |
||
105 | * Set input from custom input or stdin and return it |
||
106 | * |
||
107 | * @return string |
||
108 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
109 | */ |
||
110 | public static function getInput() |
||
128 | |||
129 | /** |
||
130 | * Generate general fake server response |
||
131 | * |
||
132 | * @param array $data Data to add to fake response |
||
133 | * |
||
134 | * @return array Fake response data |
||
135 | */ |
||
136 | 6 | public static function generateGeneralFakeServerResponse(array $data = []) |
|
166 | |||
167 | /** |
||
168 | * Properly set up the request params |
||
169 | * |
||
170 | * If any item of the array is a resource, reformat it to a multipart request. |
||
171 | * Else, just return the passed data as form params. |
||
172 | * |
||
173 | * @param array $data |
||
174 | * |
||
175 | * @return array |
||
176 | */ |
||
177 | private static function setUpRequestParams(array $data) |
||
193 | |||
194 | /** |
||
195 | * Execute HTTP Request |
||
196 | * |
||
197 | * @param string $action Action to execute |
||
198 | * @param array $data Data to attach to the execution |
||
199 | * |
||
200 | * @return string Result of the HTTP Request |
||
201 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
202 | */ |
||
203 | public static function execute($action, array $data = []) |
||
235 | |||
236 | /** |
||
237 | * Download file |
||
238 | * |
||
239 | * @param \Longman\TelegramBot\Entities\File $file |
||
240 | * |
||
241 | * @return boolean |
||
242 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
243 | */ |
||
244 | public static function downloadFile(File $file) |
||
272 | |||
273 | /** |
||
274 | * Encode file |
||
275 | * |
||
276 | * @param string $file |
||
277 | * |
||
278 | * @return resource |
||
279 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
280 | */ |
||
281 | protected static function encodeFile($file) |
||
290 | |||
291 | /** |
||
292 | * Send command |
||
293 | * |
||
294 | * @todo Fake response doesn't need json encoding? |
||
295 | * |
||
296 | * @param string $action |
||
297 | * @param array $data |
||
298 | * |
||
299 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
300 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
301 | */ |
||
302 | 5 | public static function send($action, array $data = []) |
|
324 | |||
325 | /** |
||
326 | * Make sure the data isn't empty, else throw an exception |
||
327 | * |
||
328 | * @param array $data |
||
329 | * |
||
330 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
331 | */ |
||
332 | private static function ensureNonEmptyData(array $data) |
||
338 | |||
339 | /** |
||
340 | * Make sure the action is valid, else throw an exception |
||
341 | * |
||
342 | * @param string $action |
||
343 | * |
||
344 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
345 | */ |
||
346 | 5 | private static function ensureValidAction($action) |
|
352 | |||
353 | /** |
||
354 | * Assign an encoded file to a data array |
||
355 | * |
||
356 | * @param array $data |
||
357 | * @param string $field |
||
358 | * @param string $file |
||
359 | * |
||
360 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
361 | */ |
||
362 | private static function assignEncodedFile(&$data, $field, $file) |
||
368 | |||
369 | /** |
||
370 | * Get me |
||
371 | * |
||
372 | * @return mixed |
||
373 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
374 | */ |
||
375 | public static function getMe() |
||
381 | |||
382 | /** |
||
383 | * Send message |
||
384 | * |
||
385 | * @param array $data |
||
386 | * |
||
387 | * @return mixed |
||
388 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
389 | */ |
||
390 | 5 | public static function sendMessage(array $data) |
|
405 | |||
406 | /** |
||
407 | * Forward message |
||
408 | * |
||
409 | * @param array $data |
||
410 | * |
||
411 | * @return mixed |
||
412 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
413 | */ |
||
414 | public static function forwardMessage(array $data) |
||
418 | |||
419 | /** |
||
420 | * Send photo |
||
421 | * |
||
422 | * @param array $data |
||
423 | * @param string $file |
||
424 | * |
||
425 | * @return mixed |
||
426 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
427 | */ |
||
428 | public static function sendPhoto(array $data, $file = null) |
||
434 | |||
435 | /** |
||
436 | * Send audio |
||
437 | * |
||
438 | * @param array $data |
||
439 | * @param string $file |
||
440 | * |
||
441 | * @return mixed |
||
442 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
443 | */ |
||
444 | public static function sendAudio(array $data, $file = null) |
||
450 | |||
451 | /** |
||
452 | * Send document |
||
453 | * |
||
454 | * @param array $data |
||
455 | * @param string $file |
||
456 | * |
||
457 | * @return mixed |
||
458 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
459 | */ |
||
460 | public static function sendDocument(array $data, $file = null) |
||
466 | |||
467 | /** |
||
468 | * Send sticker |
||
469 | * |
||
470 | * @param array $data |
||
471 | * @param string $file |
||
472 | * |
||
473 | * @return mixed |
||
474 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
475 | */ |
||
476 | public static function sendSticker(array $data, $file = null) |
||
482 | |||
483 | /** |
||
484 | * Send video |
||
485 | * |
||
486 | * @param array $data |
||
487 | * @param string $file |
||
488 | * |
||
489 | * @return mixed |
||
490 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
491 | */ |
||
492 | public static function sendVideo(array $data, $file = null) |
||
498 | |||
499 | /** |
||
500 | * Send voice |
||
501 | * |
||
502 | * @param array $data |
||
503 | * @param string $file |
||
504 | * |
||
505 | * @return mixed |
||
506 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
507 | */ |
||
508 | public static function sendVoice(array $data, $file = null) |
||
514 | |||
515 | /** |
||
516 | * Send location |
||
517 | * |
||
518 | * @param array $data |
||
519 | * |
||
520 | * @return mixed |
||
521 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
522 | */ |
||
523 | public static function sendLocation(array $data) |
||
527 | |||
528 | /** |
||
529 | * Send venue |
||
530 | * |
||
531 | * @param array $data |
||
532 | * |
||
533 | * @return mixed |
||
534 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
535 | */ |
||
536 | public static function sendVenue(array $data) |
||
540 | |||
541 | /** |
||
542 | * Send contact |
||
543 | * |
||
544 | * @param array $data |
||
545 | * |
||
546 | * @return mixed |
||
547 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
548 | */ |
||
549 | public static function sendContact(array $data) |
||
553 | |||
554 | /** |
||
555 | * Send chat action |
||
556 | * |
||
557 | * @param array $data |
||
558 | * |
||
559 | * @return mixed |
||
560 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
561 | */ |
||
562 | public static function sendChatAction(array $data) |
||
566 | |||
567 | /** |
||
568 | * Get user profile photos |
||
569 | * |
||
570 | * @param array $data |
||
571 | * |
||
572 | * @return mixed |
||
573 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
574 | */ |
||
575 | public static function getUserProfilePhotos(array $data) |
||
583 | |||
584 | /** |
||
585 | * Get updates |
||
586 | * |
||
587 | * @param array $data |
||
588 | * |
||
589 | * @return mixed |
||
590 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
591 | */ |
||
592 | public static function getUpdates(array $data) |
||
596 | |||
597 | /** |
||
598 | * Set webhook |
||
599 | * |
||
600 | * @param string $url |
||
601 | * @param string $file |
||
602 | * |
||
603 | * @return mixed |
||
604 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
605 | */ |
||
606 | public static function setWebhook($url = '', $file = null) |
||
614 | |||
615 | /** |
||
616 | * Get file |
||
617 | * |
||
618 | * @param array $data |
||
619 | * |
||
620 | * @return mixed |
||
621 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
622 | */ |
||
623 | public static function getFile(array $data) |
||
627 | |||
628 | /** |
||
629 | * Kick Chat Member |
||
630 | * |
||
631 | * @param array $data |
||
632 | * |
||
633 | * @return mixed |
||
634 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
635 | */ |
||
636 | public static function kickChatMember(array $data) |
||
640 | |||
641 | /** |
||
642 | * Leave Chat |
||
643 | * |
||
644 | * @param array $data |
||
645 | * |
||
646 | * @return mixed |
||
647 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
648 | */ |
||
649 | public static function leaveChat(array $data) |
||
653 | |||
654 | /** |
||
655 | * Unban Chat Member |
||
656 | * |
||
657 | * @param array $data |
||
658 | * |
||
659 | * @return mixed |
||
660 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
661 | */ |
||
662 | public static function unbanChatMember(array $data) |
||
666 | |||
667 | /** |
||
668 | * Get Chat |
||
669 | * |
||
670 | * @todo add get response in ServerResponse.php? |
||
671 | * |
||
672 | * @param array $data |
||
673 | * |
||
674 | * @return mixed |
||
675 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
676 | */ |
||
677 | public static function getChat(array $data) |
||
681 | |||
682 | /** |
||
683 | * Get Chat Administrators |
||
684 | * |
||
685 | * @todo add get response in ServerResponse.php? |
||
686 | * |
||
687 | * @param array $data |
||
688 | * |
||
689 | * @return mixed |
||
690 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
691 | */ |
||
692 | public static function getChatAdministrators(array $data) |
||
696 | |||
697 | /** |
||
698 | * Get Chat Members Count |
||
699 | * |
||
700 | * @todo add get response in ServerResponse.php? |
||
701 | * |
||
702 | * @param array $data |
||
703 | * |
||
704 | * @return mixed |
||
705 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
706 | */ |
||
707 | public static function getChatMembersCount(array $data) |
||
711 | |||
712 | /** |
||
713 | * Get Chat Member |
||
714 | * |
||
715 | * @todo add get response in ServerResponse.php? |
||
716 | * |
||
717 | * @param array $data |
||
718 | * |
||
719 | * @return mixed |
||
720 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
721 | */ |
||
722 | public static function getChatMember(array $data) |
||
726 | |||
727 | /** |
||
728 | * Answer callback query |
||
729 | * |
||
730 | * @param array $data |
||
731 | * |
||
732 | * @return mixed |
||
733 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
734 | */ |
||
735 | public static function answerCallbackQuery(array $data) |
||
739 | |||
740 | /** |
||
741 | * Answer inline query |
||
742 | * |
||
743 | * @param array $data |
||
744 | * |
||
745 | * @return mixed |
||
746 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
747 | */ |
||
748 | public static function answerInlineQuery(array $data) |
||
752 | |||
753 | /** |
||
754 | * Edit message text |
||
755 | * |
||
756 | * @param array $data |
||
757 | * |
||
758 | * @return mixed |
||
759 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
760 | */ |
||
761 | public static function editMessageText(array $data) |
||
765 | |||
766 | /** |
||
767 | * Edit message caption |
||
768 | * |
||
769 | * @param array $data |
||
770 | * |
||
771 | * @return mixed |
||
772 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
773 | */ |
||
774 | public static function editMessageCaption(array $data) |
||
778 | |||
779 | /** |
||
780 | * Edit message reply markup |
||
781 | * |
||
782 | * @param array $data |
||
783 | * |
||
784 | * @return mixed |
||
785 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
786 | */ |
||
787 | public static function editMessageReplyMarkup(array $data) |
||
791 | |||
792 | /** |
||
793 | * Return an empty Server Response |
||
794 | * |
||
795 | * No request to telegram are sent, this function is used in commands that |
||
796 | * don't need to fire a message after execution |
||
797 | * |
||
798 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
799 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
800 | */ |
||
801 | public static function emptyResponse() |
||
805 | |||
806 | /** |
||
807 | * Send message to all active chats |
||
808 | * |
||
809 | * @param string $callback_function |
||
810 | * @param array $data |
||
811 | * @param boolean $send_groups |
||
812 | * @param boolean $send_super_groups |
||
813 | * @param boolean $send_users |
||
814 | * @param string $date_from |
||
815 | * @param string $date_to |
||
816 | * |
||
817 | * @return array |
||
818 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
819 | */ |
||
820 | public static function sendToActiveChats( |
||
846 | |||
847 | /** |
||
848 | * Use this method to get current webhook status. |
||
849 | * |
||
850 | * @return Entities\ServerResponse |
||
851 | */ |
||
852 | public static function getWebhookInfo() |
||
856 | } |
||
857 |