Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 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 Telegram $telegram |
||
91 | */ |
||
92 | public static function initialize(Telegram $telegram) |
||
101 | |||
102 | /** |
||
103 | * Set input from custom input or stdin and return it |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | public static function getInput() |
||
124 | |||
125 | /** |
||
126 | * Generate general fake server response |
||
127 | * |
||
128 | * @param array $data Data to add to fake response |
||
129 | * |
||
130 | * @return array Fake response data |
||
131 | */ |
||
132 | public static function generateGeneralFakeServerResponse(array $data = null) |
||
162 | |||
163 | /** |
||
164 | * Execute HTTP Request |
||
165 | * |
||
166 | * @param string $action Action to execute |
||
167 | * @param array|null $data Data to attach to the execution |
||
168 | * |
||
169 | * @return mixed Result of the HTTP Request |
||
170 | */ |
||
171 | public static function execute($action, array $data = null) |
||
221 | |||
222 | /** |
||
223 | * Download file |
||
224 | * |
||
225 | * @param Entities\File $file |
||
226 | * |
||
227 | * @return boolean |
||
228 | */ |
||
229 | public static function downloadFile(File $file) |
||
257 | |||
258 | /** |
||
259 | * Encode file |
||
260 | * |
||
261 | * @param string $file |
||
262 | * |
||
263 | * @return resource |
||
264 | */ |
||
265 | protected static function encodeFile($file) |
||
273 | |||
274 | /** |
||
275 | * Send command |
||
276 | * |
||
277 | * @todo Fake response doesn't need json encoding? |
||
278 | * |
||
279 | * @param string $action |
||
280 | * @param array|null $data |
||
281 | * |
||
282 | * @return Entities\ServerResponse |
||
283 | */ |
||
284 | public static function send($action, array $data = null) |
||
305 | |||
306 | /** |
||
307 | * Get me |
||
308 | * |
||
309 | * @return mixed |
||
310 | */ |
||
311 | public static function getMe() |
||
317 | |||
318 | /** |
||
319 | * Send message |
||
320 | * |
||
321 | * @todo Could do with some cleaner recursion |
||
322 | * |
||
323 | * @param array $data |
||
324 | * |
||
325 | * @return mixed |
||
326 | */ |
||
327 | public static function sendMessage(array $data) |
||
342 | |||
343 | /** |
||
344 | * Forward message |
||
345 | * |
||
346 | * @param array $data |
||
347 | * |
||
348 | * @return mixed |
||
349 | */ |
||
350 | public static function forwardMessage(array $data) |
||
358 | |||
359 | /** |
||
360 | * Send photo |
||
361 | * |
||
362 | * @param array $data |
||
363 | * @param string $file |
||
364 | * |
||
365 | * @return mixed |
||
366 | */ |
||
367 | View Code Duplication | public static function sendPhoto(array $data, $file = null) |
|
379 | |||
380 | /** |
||
381 | * Send audio |
||
382 | * |
||
383 | * @param array $data |
||
384 | * @param string $file |
||
385 | * |
||
386 | * @return mixed |
||
387 | */ |
||
388 | View Code Duplication | public static function sendAudio(array $data, $file = null) |
|
400 | |||
401 | /** |
||
402 | * Send document |
||
403 | * |
||
404 | * @param array $data |
||
405 | * @param string $file |
||
406 | * |
||
407 | * @return mixed |
||
408 | */ |
||
409 | View Code Duplication | public static function sendDocument(array $data, $file = null) |
|
421 | |||
422 | /** |
||
423 | * Send sticker |
||
424 | * |
||
425 | * @param array $data |
||
426 | * @param string $file |
||
427 | * |
||
428 | * @return mixed |
||
429 | */ |
||
430 | View Code Duplication | public static function sendSticker(array $data, $file = null) |
|
442 | |||
443 | /** |
||
444 | * Send video |
||
445 | * |
||
446 | * @param array $data |
||
447 | * @param string $file |
||
448 | * |
||
449 | * @return mixed |
||
450 | */ |
||
451 | View Code Duplication | public static function sendVideo(array $data, $file = null) |
|
463 | |||
464 | /** |
||
465 | * Send voice |
||
466 | * |
||
467 | * @param array $data |
||
468 | * @param string $file |
||
469 | * |
||
470 | * @return mixed |
||
471 | */ |
||
472 | View Code Duplication | public static function sendVoice(array $data, $file = null) |
|
484 | |||
485 | /** |
||
486 | * Send location |
||
487 | * |
||
488 | * @param array $data |
||
489 | * |
||
490 | * @return mixed |
||
491 | */ |
||
492 | public static function sendLocation(array $data) |
||
500 | |||
501 | /** |
||
502 | * Send venue |
||
503 | * |
||
504 | * @param array $data |
||
505 | * |
||
506 | * @return mixed |
||
507 | */ |
||
508 | public static function sendVenue(array $data) |
||
516 | |||
517 | /** |
||
518 | * Send contact |
||
519 | * |
||
520 | * @param array $data |
||
521 | * |
||
522 | * @return mixed |
||
523 | */ |
||
524 | public static function sendContact(array $data) |
||
532 | |||
533 | /** |
||
534 | * Send chat action |
||
535 | * |
||
536 | * @param array $data |
||
537 | * |
||
538 | * @return mixed |
||
539 | */ |
||
540 | public static function sendChatAction(array $data) |
||
548 | |||
549 | /** |
||
550 | * Get user profile photos |
||
551 | * |
||
552 | * @param array $data |
||
553 | * |
||
554 | * @return mixed |
||
555 | */ |
||
556 | public static function getUserProfilePhotos(array $data) |
||
568 | |||
569 | /** |
||
570 | * Get updates |
||
571 | * |
||
572 | * @param array $data |
||
573 | * |
||
574 | * @return mixed |
||
575 | */ |
||
576 | public static function getUpdates(array $data) |
||
580 | |||
581 | /** |
||
582 | * Set webhook |
||
583 | * |
||
584 | * @param string $url |
||
585 | * @param string $file |
||
586 | * |
||
587 | * @return mixed |
||
588 | */ |
||
589 | public static function setWebhook($url = '', $file = null) |
||
599 | |||
600 | /** |
||
601 | * Get file |
||
602 | * |
||
603 | * @param array $data |
||
604 | * |
||
605 | * @return mixed |
||
606 | */ |
||
607 | public static function getFile(array $data) |
||
615 | |||
616 | /** |
||
617 | * Kick Chat Member |
||
618 | * |
||
619 | * @param array $data |
||
620 | * |
||
621 | * @return mixed |
||
622 | */ |
||
623 | public static function kickChatMember(array $data) |
||
631 | |||
632 | /** |
||
633 | * Leave Chat |
||
634 | * |
||
635 | * @param array $data |
||
636 | * |
||
637 | * @return mixed |
||
638 | */ |
||
639 | public static function leaveChat(array $data) |
||
647 | |||
648 | /** |
||
649 | * Unban Chat Member |
||
650 | * |
||
651 | * @param array $data |
||
652 | * |
||
653 | * @return mixed |
||
654 | */ |
||
655 | public static function unbanChatMember(array $data) |
||
663 | |||
664 | /** |
||
665 | * Get Chat |
||
666 | * |
||
667 | * @todo add get response in ServerResponse.php? |
||
668 | * |
||
669 | * @param array $data |
||
670 | * |
||
671 | * @return mixed |
||
672 | */ |
||
673 | 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 | */ |
||
691 | public static function getChatAdministrators(array $data) |
||
699 | |||
700 | /** |
||
701 | * Get Chat Members Count |
||
702 | * |
||
703 | * @todo add get response in ServerResponse.php? |
||
704 | * |
||
705 | * @param array $data |
||
706 | * |
||
707 | * @return mixed |
||
708 | */ |
||
709 | public static function getChatMembersCount(array $data) |
||
717 | |||
718 | /** |
||
719 | * Get Chat Member |
||
720 | * |
||
721 | * @todo add get response in ServerResponse.php? |
||
722 | * |
||
723 | * @param array $data |
||
724 | * |
||
725 | * @return mixed |
||
726 | */ |
||
727 | public static function getChatMember(array $data) |
||
735 | |||
736 | /** |
||
737 | * Answer callback query |
||
738 | * |
||
739 | * @param array $data |
||
740 | * |
||
741 | * @return mixed |
||
742 | */ |
||
743 | public static function answerCallbackQuery(array $data) |
||
751 | |||
752 | /** |
||
753 | * Answer inline query |
||
754 | * |
||
755 | * @param array $data |
||
756 | * |
||
757 | * @return mixed |
||
758 | */ |
||
759 | public static function answerInlineQuery(array $data) |
||
767 | |||
768 | /** |
||
769 | * Edit message text |
||
770 | * |
||
771 | * @param array $data |
||
772 | * |
||
773 | * @return mixed |
||
774 | */ |
||
775 | public static function editMessageText(array $data) |
||
783 | |||
784 | /** |
||
785 | * Edit message caption |
||
786 | * |
||
787 | * @param array $data |
||
788 | * |
||
789 | * @return mixed |
||
790 | */ |
||
791 | public static function editMessageCaption(array $data) |
||
799 | |||
800 | /** |
||
801 | * Edit message reply markup |
||
802 | * |
||
803 | * @param array $data |
||
804 | * |
||
805 | * @return mixed |
||
806 | */ |
||
807 | public static function editMessageReplyMarkup(array $data) |
||
815 | |||
816 | /** |
||
817 | * Return an empty Server Response |
||
818 | * |
||
819 | * No request to telegram are sent, this function is used in commands that |
||
820 | * don't need to fire a message after execution |
||
821 | * |
||
822 | * @return Entities\ServerResponse |
||
823 | */ |
||
824 | public static function emptyResponse() |
||
828 | |||
829 | /** |
||
830 | * Send message to all active chats |
||
831 | * |
||
832 | * @param string $callback_function |
||
833 | * @param array $data |
||
834 | * @param boolean $send_groups |
||
835 | * @param boolean $send_super_groups |
||
836 | * @param boolean $send_users |
||
837 | * @param string $date_from |
||
838 | * @param string $date_to |
||
839 | * |
||
840 | * @return array |
||
841 | */ |
||
842 | public static function sendToActiveChats( |
||
866 | |||
867 | /** |
||
868 | * Use this method to get current webhook status. |
||
869 | * |
||
870 | * @return Entities\ServerResponse |
||
871 | */ |
||
872 | public static function getWebhookInfo() |
||
876 | } |
||
877 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.