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 Telegram 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 Telegram, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Telegram |
||
27 | { |
||
28 | /** |
||
29 | * Version |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $version = '0.43.0'; |
||
34 | |||
35 | /** |
||
36 | * Telegram API key |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $api_key = ''; |
||
41 | |||
42 | /** |
||
43 | * Telegram Bot username |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $bot_username = ''; |
||
48 | |||
49 | /** |
||
50 | * Telegram Bot id |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $bot_id = ''; |
||
55 | |||
56 | /** |
||
57 | * Raw request data (json) for webhook methods |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $input; |
||
62 | |||
63 | /** |
||
64 | * Custom commands paths |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $commands_paths = []; |
||
69 | |||
70 | /** |
||
71 | * Current Update object |
||
72 | * |
||
73 | * @var \Longman\TelegramBot\Entities\Update |
||
74 | */ |
||
75 | protected $update; |
||
76 | |||
77 | /** |
||
78 | * Upload path |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $upload_path; |
||
83 | |||
84 | /** |
||
85 | * Download path |
||
86 | * |
||
87 | * @var string |
||
88 | */ |
||
89 | protected $download_path; |
||
90 | |||
91 | /** |
||
92 | * MySQL integration |
||
93 | * |
||
94 | * @var boolean |
||
95 | */ |
||
96 | protected $mysql_enabled = false; |
||
97 | |||
98 | /** |
||
99 | * PDO object |
||
100 | * |
||
101 | * @var \PDO |
||
102 | */ |
||
103 | protected $pdo; |
||
104 | |||
105 | /** |
||
106 | * Commands config |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | protected $commands_config = []; |
||
111 | |||
112 | /** |
||
113 | * Admins list |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | protected $admins_list = []; |
||
118 | |||
119 | /** |
||
120 | * ServerResponse of the last Command execution |
||
121 | * |
||
122 | * @var \Longman\TelegramBot\Entities\ServerResponse |
||
123 | */ |
||
124 | protected $last_command_response; |
||
125 | |||
126 | /** |
||
127 | * Botan.io integration |
||
128 | * |
||
129 | * @var boolean |
||
130 | */ |
||
131 | protected $botan_enabled = false; |
||
132 | |||
133 | /** |
||
134 | * Check if runCommands() is running in this session |
||
135 | * |
||
136 | * @var boolean |
||
137 | */ |
||
138 | protected $run_commands = false; |
||
139 | |||
140 | /** |
||
141 | * Telegram constructor. |
||
142 | * |
||
143 | * @param string $api_key |
||
144 | * @param string $bot_username |
||
145 | * |
||
146 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
147 | */ |
||
148 | 31 | public function __construct($api_key, $bot_username) |
|
174 | |||
175 | /** |
||
176 | * Initialize Database connection |
||
177 | * |
||
178 | * @param array $credential |
||
179 | * @param string $table_prefix |
||
180 | * @param string $encoding |
||
181 | * |
||
182 | * @return \Longman\TelegramBot\Telegram |
||
183 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
184 | */ |
||
185 | 9 | View Code Duplication | public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4') |
193 | |||
194 | /** |
||
195 | * Initialize Database external connection |
||
196 | * |
||
197 | * @param PDO $external_pdo_connection PDO database object |
||
198 | * @param string $table_prefix |
||
199 | * |
||
200 | * @return \Longman\TelegramBot\Telegram |
||
201 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
202 | */ |
||
203 | View Code Duplication | public function enableExternalMySql($external_pdo_connection, $table_prefix = null) |
|
211 | |||
212 | /** |
||
213 | * Get commands list |
||
214 | * |
||
215 | * @return array $commands |
||
216 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
217 | */ |
||
218 | 1 | public function getCommandsList() |
|
255 | |||
256 | /** |
||
257 | * Get an object instance of the passed command |
||
258 | * |
||
259 | * @param string $command |
||
260 | * |
||
261 | * @return \Longman\TelegramBot\Commands\Command|null |
||
262 | */ |
||
263 | 1 | public function getCommandObject($command) |
|
278 | |||
279 | /** |
||
280 | * Set custom input string for debug purposes |
||
281 | * |
||
282 | * @param string $input (json format) |
||
283 | * |
||
284 | * @return \Longman\TelegramBot\Telegram |
||
285 | */ |
||
286 | public function setCustomInput($input) |
||
292 | |||
293 | /** |
||
294 | * Get custom input string for debug purposes |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | public function getCustomInput() |
||
302 | |||
303 | /** |
||
304 | * Get the ServerResponse of the last Command execution |
||
305 | * |
||
306 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
307 | */ |
||
308 | public function getLastCommandResponse() |
||
312 | |||
313 | /** |
||
314 | * Handle getUpdates method |
||
315 | * |
||
316 | * @param int|null $limit |
||
317 | * @param int|null $timeout |
||
318 | * |
||
319 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
320 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
321 | */ |
||
322 | public function handleGetUpdates($limit = null, $timeout = null) |
||
359 | |||
360 | /** |
||
361 | * Handle bot request from webhook |
||
362 | * |
||
363 | * @return bool |
||
364 | * |
||
365 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
366 | */ |
||
367 | public function handle() |
||
386 | |||
387 | /** |
||
388 | * Get the command name from the command type |
||
389 | * |
||
390 | * @param string $type |
||
391 | * |
||
392 | * @return string |
||
393 | */ |
||
394 | protected function getCommandFromType($type) |
||
398 | |||
399 | /** |
||
400 | * Process bot Update request |
||
401 | * |
||
402 | * @param \Longman\TelegramBot\Entities\Update $update |
||
403 | * |
||
404 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
405 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
406 | */ |
||
407 | public function processUpdate(Update $update) |
||
454 | |||
455 | /** |
||
456 | * Execute /command |
||
457 | * |
||
458 | * @param string $command |
||
459 | * |
||
460 | * @return mixed |
||
461 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
462 | */ |
||
463 | public function executeCommand($command) |
||
493 | |||
494 | /** |
||
495 | * Sanitize Command |
||
496 | * |
||
497 | * @param string $command |
||
498 | * |
||
499 | * @return string |
||
500 | */ |
||
501 | 1 | protected function sanitizeCommand($command) |
|
505 | |||
506 | /** |
||
507 | * Enable a single Admin account |
||
508 | * |
||
509 | * @param integer $admin_id Single admin id |
||
510 | * |
||
511 | * @return \Longman\TelegramBot\Telegram |
||
512 | */ |
||
513 | 1 | public function enableAdmin($admin_id) |
|
523 | |||
524 | /** |
||
525 | * Enable a list of Admin Accounts |
||
526 | * |
||
527 | * @param array $admin_ids List of admin ids |
||
528 | * |
||
529 | * @return \Longman\TelegramBot\Telegram |
||
530 | */ |
||
531 | 1 | public function enableAdmins(array $admin_ids) |
|
539 | |||
540 | /** |
||
541 | * Get list of admins |
||
542 | * |
||
543 | * @return array |
||
544 | */ |
||
545 | 1 | public function getAdminList() |
|
549 | |||
550 | /** |
||
551 | * Check if the passed user is an admin |
||
552 | * |
||
553 | * If no user id is passed, the current update is checked for a valid message sender. |
||
554 | * |
||
555 | * @param int|null $user_id |
||
556 | * |
||
557 | * @return bool |
||
558 | */ |
||
559 | 1 | public function isAdmin($user_id = null) |
|
583 | |||
584 | /** |
||
585 | * Check if user required the db connection |
||
586 | * |
||
587 | * @return bool |
||
588 | */ |
||
589 | public function isDbEnabled() |
||
597 | |||
598 | /** |
||
599 | * Add a single custom commands path |
||
600 | * |
||
601 | * @param string $path Custom commands path to add |
||
602 | * @param bool $before If the path should be prepended or appended to the list |
||
603 | * |
||
604 | * @return \Longman\TelegramBot\Telegram |
||
605 | */ |
||
606 | 31 | public function addCommandsPath($path, $before = true) |
|
620 | |||
621 | /** |
||
622 | * Add multiple custom commands paths |
||
623 | * |
||
624 | * @param array $paths Custom commands paths to add |
||
625 | * @param bool $before If the paths should be prepended or appended to the list |
||
626 | * |
||
627 | * @return \Longman\TelegramBot\Telegram |
||
628 | */ |
||
629 | 1 | public function addCommandsPaths(array $paths, $before = true) |
|
637 | |||
638 | /** |
||
639 | * Return the list of commands paths |
||
640 | * |
||
641 | * @return array |
||
642 | */ |
||
643 | 1 | public function getCommandsPaths() |
|
647 | |||
648 | /** |
||
649 | * Set custom upload path |
||
650 | * |
||
651 | * @param string $path Custom upload path |
||
652 | * |
||
653 | * @return \Longman\TelegramBot\Telegram |
||
654 | */ |
||
655 | 31 | public function setUploadPath($path) |
|
661 | |||
662 | /** |
||
663 | * Get custom upload path |
||
664 | * |
||
665 | * @return string |
||
666 | */ |
||
667 | public function getUploadPath() |
||
671 | |||
672 | /** |
||
673 | * Set custom download path |
||
674 | * |
||
675 | * @param string $path Custom download path |
||
676 | * |
||
677 | * @return \Longman\TelegramBot\Telegram |
||
678 | */ |
||
679 | 31 | public function setDownloadPath($path) |
|
685 | |||
686 | /** |
||
687 | * Get custom download path |
||
688 | * |
||
689 | * @return string |
||
690 | */ |
||
691 | public function getDownloadPath() |
||
695 | |||
696 | /** |
||
697 | * Set command config |
||
698 | * |
||
699 | * Provide further variables to a particular commands. |
||
700 | * For example you can add the channel name at the command /sendtochannel |
||
701 | * Or you can add the api key for external service. |
||
702 | * |
||
703 | * @param string $command |
||
704 | * @param array $config |
||
705 | * |
||
706 | * @return \Longman\TelegramBot\Telegram |
||
707 | */ |
||
708 | 14 | public function setCommandConfig($command, array $config) |
|
714 | |||
715 | /** |
||
716 | * Get command config |
||
717 | * |
||
718 | * @param string $command |
||
719 | * |
||
720 | * @return array |
||
721 | */ |
||
722 | 15 | public function getCommandConfig($command) |
|
726 | |||
727 | /** |
||
728 | * Get API key |
||
729 | * |
||
730 | * @return string |
||
731 | */ |
||
732 | 1 | public function getApiKey() |
|
736 | |||
737 | /** |
||
738 | * Get Bot name |
||
739 | * |
||
740 | * @return string |
||
741 | */ |
||
742 | 1 | public function getBotUsername() |
|
746 | |||
747 | /** |
||
748 | * Get Bot name |
||
749 | * |
||
750 | * @todo: Left for backwards compatibility, remove in the future |
||
751 | * |
||
752 | * @return string |
||
753 | */ |
||
754 | public function getBotName() |
||
759 | |||
760 | /** |
||
761 | * Get Bot Id |
||
762 | * |
||
763 | * @return string |
||
764 | */ |
||
765 | public function getBotId() |
||
769 | |||
770 | /** |
||
771 | * Get Version |
||
772 | * |
||
773 | * @return string |
||
774 | */ |
||
775 | public function getVersion() |
||
779 | |||
780 | /** |
||
781 | * Set Webhook for bot |
||
782 | * |
||
783 | * @param string $url |
||
784 | * @param array $data Optional parameters. |
||
785 | * |
||
786 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
787 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
788 | */ |
||
789 | public function setWebhook($url, array $data = []) |
||
805 | |||
806 | /** |
||
807 | * Deprecated alias for deleteWebhook |
||
808 | * |
||
809 | * This is kept for backwards compatibility! |
||
810 | * |
||
811 | * @return mixed |
||
812 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
813 | */ |
||
814 | public function unsetWebhook() |
||
818 | |||
819 | /** |
||
820 | * Delete any assigned webhook |
||
821 | * |
||
822 | * @return mixed |
||
823 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
824 | */ |
||
825 | public function deleteWebhook() |
||
837 | |||
838 | /** |
||
839 | * Replace function `ucwords` for UTF-8 characters in the class definition and commands |
||
840 | * |
||
841 | * @param string $str |
||
842 | * @param string $encoding (default = 'UTF-8') |
||
843 | * |
||
844 | * @return string |
||
845 | */ |
||
846 | 1 | protected function ucwordsUnicode($str, $encoding = 'UTF-8') |
|
850 | |||
851 | /** |
||
852 | * Replace function `ucfirst` for UTF-8 characters in the class definition and commands |
||
853 | * |
||
854 | * @param string $str |
||
855 | * @param string $encoding (default = 'UTF-8') |
||
856 | * |
||
857 | * @return string |
||
858 | */ |
||
859 | 1 | protected function ucfirstUnicode($str, $encoding = 'UTF-8') |
|
865 | |||
866 | /** |
||
867 | * Enable Botan.io integration |
||
868 | * |
||
869 | * @param string $token |
||
870 | * @param array $options |
||
871 | * |
||
872 | * @return \Longman\TelegramBot\Telegram |
||
873 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
874 | */ |
||
875 | public function enableBotan($token, array $options = []) |
||
882 | |||
883 | /** |
||
884 | * Enable requests limiter |
||
885 | * |
||
886 | * @param array $options |
||
887 | * |
||
888 | * @return \Longman\TelegramBot\Telegram |
||
889 | */ |
||
890 | public function enableLimiter(array $options = []) |
||
896 | |||
897 | /** |
||
898 | * Run provided commands |
||
899 | * |
||
900 | * @param array $commands |
||
901 | * |
||
902 | * @throws TelegramException |
||
903 | */ |
||
904 | public function runCommands($commands) |
||
950 | |||
951 | /** |
||
952 | * Is this session initiated by runCommands() |
||
953 | * |
||
954 | * @return bool |
||
955 | */ |
||
956 | public function isRunCommands() |
||
960 | } |
||
961 |