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 = null) |
|
165 | |||
166 | /** |
||
167 | * Execute HTTP Request |
||
168 | * |
||
169 | * @param string $action Action to execute |
||
170 | * @param array|null $data Data to attach to the execution |
||
171 | * |
||
172 | * @return mixed Result of the HTTP Request |
||
173 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
174 | */ |
||
175 | public static function execute($action, array $data = null) |
||
224 | |||
225 | /** |
||
226 | * Download file |
||
227 | * |
||
228 | * @param \Longman\TelegramBot\Entities\File $file |
||
229 | * |
||
230 | * @return boolean |
||
231 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
232 | */ |
||
233 | public static function downloadFile(File $file) |
||
261 | |||
262 | /** |
||
263 | * Encode file |
||
264 | * |
||
265 | * @param string $file |
||
266 | * |
||
267 | * @return resource |
||
268 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
269 | */ |
||
270 | protected static function encodeFile($file) |
||
279 | |||
280 | /** |
||
281 | * Send command |
||
282 | * |
||
283 | * @todo Fake response doesn't need json encoding? |
||
284 | * |
||
285 | * @param string $action |
||
286 | * @param array|null $data |
||
287 | * |
||
288 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
289 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
290 | */ |
||
291 | 5 | public static function send($action, array $data = null) |
|
313 | |||
314 | /** |
||
315 | * Make sure the data isn't empty, else throw an exception |
||
316 | * |
||
317 | * @param array $data |
||
318 | * |
||
319 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
320 | */ |
||
321 | private static function ensureNonEmptyData(array $data) |
||
327 | |||
328 | /** |
||
329 | * Make sure the action is valid, else throw an exception |
||
330 | * |
||
331 | * @param string $action |
||
332 | * |
||
333 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
334 | */ |
||
335 | 5 | private static function ensureValidAction($action) |
|
341 | |||
342 | /** |
||
343 | * Assign an encoded file to a data array |
||
344 | * |
||
345 | * @param array $data |
||
346 | * @param string $field |
||
347 | * @param string $file |
||
348 | * |
||
349 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
350 | */ |
||
351 | private static function assignEncodedFile(&$data, $field, $file) |
||
357 | |||
358 | /** |
||
359 | * Get me |
||
360 | * |
||
361 | * @return mixed |
||
362 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
363 | */ |
||
364 | public static function getMe() |
||
370 | |||
371 | /** |
||
372 | * Send message |
||
373 | * |
||
374 | * @todo Could do with some cleaner recursion |
||
375 | * |
||
376 | * @param array $data |
||
377 | * |
||
378 | * @return mixed |
||
379 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
380 | */ |
||
381 | 5 | public static function sendMessage(array $data) |
|
395 | |||
396 | /** |
||
397 | * Forward message |
||
398 | * |
||
399 | * @param array $data |
||
400 | * |
||
401 | * @return mixed |
||
402 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
403 | */ |
||
404 | public static function forwardMessage(array $data) |
||
408 | |||
409 | /** |
||
410 | * Send photo |
||
411 | * |
||
412 | * @param array $data |
||
413 | * @param string $file |
||
414 | * |
||
415 | * @return mixed |
||
416 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
417 | */ |
||
418 | public static function sendPhoto(array $data, $file = null) |
||
424 | |||
425 | /** |
||
426 | * Send audio |
||
427 | * |
||
428 | * @param array $data |
||
429 | * @param string $file |
||
430 | * |
||
431 | * @return mixed |
||
432 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
433 | */ |
||
434 | public static function sendAudio(array $data, $file = null) |
||
440 | |||
441 | /** |
||
442 | * Send document |
||
443 | * |
||
444 | * @param array $data |
||
445 | * @param string $file |
||
446 | * |
||
447 | * @return mixed |
||
448 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
449 | */ |
||
450 | public static function sendDocument(array $data, $file = null) |
||
456 | |||
457 | /** |
||
458 | * Send sticker |
||
459 | * |
||
460 | * @param array $data |
||
461 | * @param string $file |
||
462 | * |
||
463 | * @return mixed |
||
464 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
465 | */ |
||
466 | public static function sendSticker(array $data, $file = null) |
||
472 | |||
473 | /** |
||
474 | * Send video |
||
475 | * |
||
476 | * @param array $data |
||
477 | * @param string $file |
||
478 | * |
||
479 | * @return mixed |
||
480 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
481 | */ |
||
482 | public static function sendVideo(array $data, $file = null) |
||
488 | |||
489 | /** |
||
490 | * Send voice |
||
491 | * |
||
492 | * @param array $data |
||
493 | * @param string $file |
||
494 | * |
||
495 | * @return mixed |
||
496 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
497 | */ |
||
498 | public static function sendVoice(array $data, $file = null) |
||
504 | |||
505 | /** |
||
506 | * Send location |
||
507 | * |
||
508 | * @param array $data |
||
509 | * |
||
510 | * @return mixed |
||
511 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
512 | */ |
||
513 | public static function sendLocation(array $data) |
||
517 | |||
518 | /** |
||
519 | * Send venue |
||
520 | * |
||
521 | * @param array $data |
||
522 | * |
||
523 | * @return mixed |
||
524 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
525 | */ |
||
526 | public static function sendVenue(array $data) |
||
530 | |||
531 | /** |
||
532 | * Send contact |
||
533 | * |
||
534 | * @param array $data |
||
535 | * |
||
536 | * @return mixed |
||
537 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
538 | */ |
||
539 | public static function sendContact(array $data) |
||
543 | |||
544 | /** |
||
545 | * Send chat action |
||
546 | * |
||
547 | * @param array $data |
||
548 | * |
||
549 | * @return mixed |
||
550 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
551 | */ |
||
552 | public static function sendChatAction(array $data) |
||
556 | |||
557 | /** |
||
558 | * Get user profile photos |
||
559 | * |
||
560 | * @param array $data |
||
561 | * |
||
562 | * @return mixed |
||
563 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
564 | */ |
||
565 | public static function getUserProfilePhotos(array $data) |
||
573 | |||
574 | /** |
||
575 | * Get updates |
||
576 | * |
||
577 | * @param array $data |
||
578 | * |
||
579 | * @return mixed |
||
580 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
581 | */ |
||
582 | public static function getUpdates(array $data) |
||
586 | |||
587 | /** |
||
588 | * Set webhook |
||
589 | * |
||
590 | * @param string $url |
||
591 | * @param string $file |
||
592 | * |
||
593 | * @return mixed |
||
594 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
595 | */ |
||
596 | public static function setWebhook($url = '', $file = null) |
||
604 | |||
605 | /** |
||
606 | * Get file |
||
607 | * |
||
608 | * @param array $data |
||
609 | * |
||
610 | * @return mixed |
||
611 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
612 | */ |
||
613 | public static function getFile(array $data) |
||
617 | |||
618 | /** |
||
619 | * Kick Chat Member |
||
620 | * |
||
621 | * @param array $data |
||
622 | * |
||
623 | * @return mixed |
||
624 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
625 | */ |
||
626 | public static function kickChatMember(array $data) |
||
630 | |||
631 | /** |
||
632 | * Leave Chat |
||
633 | * |
||
634 | * @param array $data |
||
635 | * |
||
636 | * @return mixed |
||
637 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
638 | */ |
||
639 | public static function leaveChat(array $data) |
||
643 | |||
644 | /** |
||
645 | * Unban Chat Member |
||
646 | * |
||
647 | * @param array $data |
||
648 | * |
||
649 | * @return mixed |
||
650 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
651 | */ |
||
652 | public static function unbanChatMember(array $data) |
||
656 | |||
657 | /** |
||
658 | * Get Chat |
||
659 | * |
||
660 | * @todo add get response in ServerResponse.php? |
||
661 | * |
||
662 | * @param array $data |
||
663 | * |
||
664 | * @return mixed |
||
665 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
666 | */ |
||
667 | public static function getChat(array $data) |
||
671 | |||
672 | /** |
||
673 | * Get Chat Administrators |
||
674 | * |
||
675 | * @todo add get response in ServerResponse.php? |
||
676 | * |
||
677 | * @param array $data |
||
678 | * |
||
679 | * @return mixed |
||
680 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
681 | */ |
||
682 | public static function getChatAdministrators(array $data) |
||
686 | |||
687 | /** |
||
688 | * Get Chat Members Count |
||
689 | * |
||
690 | * @todo add get response in ServerResponse.php? |
||
691 | * |
||
692 | * @param array $data |
||
693 | * |
||
694 | * @return mixed |
||
695 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
696 | */ |
||
697 | public static function getChatMembersCount(array $data) |
||
701 | |||
702 | /** |
||
703 | * Get Chat Member |
||
704 | * |
||
705 | * @todo add get response in ServerResponse.php? |
||
706 | * |
||
707 | * @param array $data |
||
708 | * |
||
709 | * @return mixed |
||
710 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
711 | */ |
||
712 | public static function getChatMember(array $data) |
||
716 | |||
717 | /** |
||
718 | * Answer callback query |
||
719 | * |
||
720 | * @param array $data |
||
721 | * |
||
722 | * @return mixed |
||
723 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
724 | */ |
||
725 | public static function answerCallbackQuery(array $data) |
||
729 | |||
730 | /** |
||
731 | * Answer inline query |
||
732 | * |
||
733 | * @param array $data |
||
734 | * |
||
735 | * @return mixed |
||
736 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
737 | */ |
||
738 | public static function answerInlineQuery(array $data) |
||
742 | |||
743 | /** |
||
744 | * Edit message text |
||
745 | * |
||
746 | * @param array $data |
||
747 | * |
||
748 | * @return mixed |
||
749 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
750 | */ |
||
751 | public static function editMessageText(array $data) |
||
755 | |||
756 | /** |
||
757 | * Edit message caption |
||
758 | * |
||
759 | * @param array $data |
||
760 | * |
||
761 | * @return mixed |
||
762 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
763 | */ |
||
764 | public static function editMessageCaption(array $data) |
||
768 | |||
769 | /** |
||
770 | * Edit message reply markup |
||
771 | * |
||
772 | * @param array $data |
||
773 | * |
||
774 | * @return mixed |
||
775 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
776 | */ |
||
777 | public static function editMessageReplyMarkup(array $data) |
||
781 | |||
782 | /** |
||
783 | * Return an empty Server Response |
||
784 | * |
||
785 | * No request to telegram are sent, this function is used in commands that |
||
786 | * don't need to fire a message after execution |
||
787 | * |
||
788 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
789 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
790 | */ |
||
791 | public static function emptyResponse() |
||
795 | |||
796 | /** |
||
797 | * Send message to all active chats |
||
798 | * |
||
799 | * @param string $callback_function |
||
800 | * @param array $data |
||
801 | * @param boolean $send_groups |
||
802 | * @param boolean $send_super_groups |
||
803 | * @param boolean $send_users |
||
804 | * @param string $date_from |
||
805 | * @param string $date_to |
||
806 | * |
||
807 | * @return array |
||
808 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
809 | */ |
||
810 | public static function sendToActiveChats( |
||
836 | } |
||
837 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.