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 |
||
19 | class Telegram |
||
20 | { |
||
21 | /** |
||
22 | * Version |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $version = '0.35.0'; |
||
27 | |||
28 | /** |
||
29 | * Telegram API key |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $api_key = ''; |
||
34 | |||
35 | /** |
||
36 | * Telegram Bot name |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $bot_name = ''; |
||
41 | |||
42 | /** |
||
43 | * Raw request data (json) for webhook methods |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $input; |
||
48 | |||
49 | /** |
||
50 | * Custom commands paths |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $commands_paths = []; |
||
55 | |||
56 | /** |
||
57 | * Current Update object |
||
58 | * |
||
59 | * @var \Longman\TelegramBot\Entities\Update |
||
60 | */ |
||
61 | protected $update; |
||
62 | |||
63 | /** |
||
64 | * Upload path |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $upload_path; |
||
69 | |||
70 | /** |
||
71 | * Download path |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $download_path; |
||
76 | |||
77 | /** |
||
78 | * MySQL integration |
||
79 | * |
||
80 | * @var boolean |
||
81 | */ |
||
82 | protected $mysql_enabled = false; |
||
83 | |||
84 | /** |
||
85 | * PDO object |
||
86 | * |
||
87 | * @var \PDO |
||
88 | */ |
||
89 | protected $pdo; |
||
90 | |||
91 | /** |
||
92 | * Commands config |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $commands_config = []; |
||
97 | |||
98 | /** |
||
99 | * Admins list |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | protected $admins_list = []; |
||
104 | |||
105 | /** |
||
106 | * ServerResponse of the last Command execution |
||
107 | * |
||
108 | * @var \Longman\TelegramBot\Entities\ServerResponse |
||
109 | */ |
||
110 | protected $last_command_response; |
||
111 | |||
112 | /** |
||
113 | * Botan.io integration |
||
114 | * |
||
115 | * @var boolean |
||
116 | */ |
||
117 | protected $botan_enabled = false; |
||
118 | |||
119 | /** |
||
120 | * Telegram constructor. |
||
121 | * |
||
122 | * @param $api_key |
||
123 | * @param $bot_name |
||
124 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
125 | */ |
||
126 | 39 | public function __construct($api_key, $bot_name) |
|
148 | |||
149 | /** |
||
150 | * Initialize Database connection |
||
151 | * |
||
152 | * @param array $credential |
||
153 | * @param string $table_prefix |
||
154 | * @param string $encoding |
||
155 | * |
||
156 | * @return \Longman\TelegramBot\Telegram |
||
157 | */ |
||
158 | 9 | public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4') |
|
165 | |||
166 | /** |
||
167 | * Initialize Database external connection |
||
168 | * |
||
169 | * @param /PDO $external_pdo_connection PDO database object |
||
|
|||
170 | * @param string $table_prefix |
||
171 | */ |
||
172 | public function enableExternalMysql($external_pdo_connection, $table_prefix = null) |
||
178 | |||
179 | /** |
||
180 | * Get commands list |
||
181 | * |
||
182 | * @return array $commands |
||
183 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
184 | */ |
||
185 | 10 | public function getCommandsList() |
|
222 | |||
223 | /** |
||
224 | * Get an object instance of the passed command |
||
225 | * |
||
226 | * @param string $command |
||
227 | * |
||
228 | * @return \Longman\TelegramBot\Commands\Command|null |
||
229 | */ |
||
230 | 11 | public function getCommandObject($command) |
|
245 | |||
246 | /** |
||
247 | * Set custom input string for debug purposes |
||
248 | * |
||
249 | * @param string $input (json format) |
||
250 | * |
||
251 | * @return \Longman\TelegramBot\Telegram |
||
252 | */ |
||
253 | public function setCustomInput($input) |
||
258 | |||
259 | /** |
||
260 | * Get custom input string for debug purposes |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | public function getCustomInput() |
||
268 | |||
269 | /** |
||
270 | * Get the ServerResponse of the last Command execution |
||
271 | * |
||
272 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
273 | */ |
||
274 | public function getLastCommandResponse() |
||
278 | |||
279 | /** |
||
280 | * Handle getUpdates method |
||
281 | * |
||
282 | * @param int|null $limit |
||
283 | * @param int|null $timeout |
||
284 | * |
||
285 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
286 | */ |
||
287 | public function handleGetUpdates($limit = null, $timeout = null) |
||
317 | |||
318 | /** |
||
319 | * Handle bot request from webhook |
||
320 | * |
||
321 | * @return bool |
||
322 | * |
||
323 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
324 | */ |
||
325 | public function handle() |
||
343 | |||
344 | /** |
||
345 | * Get the command name from the command type |
||
346 | * |
||
347 | * @param string $type |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | private function getCommandFromType($type) |
||
355 | |||
356 | /** |
||
357 | * Process bot Update request |
||
358 | * |
||
359 | * @param \Longman\TelegramBot\Entities\Update $update |
||
360 | * |
||
361 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
362 | */ |
||
363 | public function processUpdate(Update $update) |
||
410 | |||
411 | /** |
||
412 | * Execute /command |
||
413 | * |
||
414 | * @param string $command |
||
415 | * |
||
416 | * @return mixed |
||
417 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
418 | */ |
||
419 | public function executeCommand($command) |
||
449 | |||
450 | /** |
||
451 | * Sanitize Command |
||
452 | * |
||
453 | * @param string $command |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | 10 | protected function sanitizeCommand($command) |
|
461 | |||
462 | /** |
||
463 | * Enable a single Admin account |
||
464 | * |
||
465 | * @param integer $admin_id Single admin id |
||
466 | * |
||
467 | * @return \Longman\TelegramBot\Telegram |
||
468 | */ |
||
469 | 1 | public function enableAdmin($admin_id) |
|
479 | |||
480 | /** |
||
481 | * Enable a list of Admin Accounts |
||
482 | * |
||
483 | * @param array $admin_ids List of admin ids |
||
484 | * |
||
485 | * @return \Longman\TelegramBot\Telegram |
||
486 | */ |
||
487 | 1 | public function enableAdmins(array $admin_ids) |
|
495 | |||
496 | /** |
||
497 | * Get list of admins |
||
498 | * |
||
499 | * @return array |
||
500 | */ |
||
501 | 1 | public function getAdminList() |
|
505 | |||
506 | /** |
||
507 | * Check if the passed user is an admin |
||
508 | * |
||
509 | * If no user id is passed, the current update is checked for a valid message sender. |
||
510 | * |
||
511 | * @param int|null $user_id |
||
512 | * |
||
513 | * @return bool |
||
514 | */ |
||
515 | 11 | public function isAdmin($user_id = null) |
|
533 | |||
534 | /** |
||
535 | * Check if user required the db connection |
||
536 | * |
||
537 | * @return bool |
||
538 | */ |
||
539 | public function isDbEnabled() |
||
547 | |||
548 | /** |
||
549 | * Add a single custom commands path |
||
550 | * |
||
551 | * @param string $path Custom commands path to add |
||
552 | * @param bool $before If the path should be prepended or appended to the list |
||
553 | * |
||
554 | * @return \Longman\TelegramBot\Telegram |
||
555 | */ |
||
556 | 39 | public function addCommandsPath($path, $before = true) |
|
570 | |||
571 | /** |
||
572 | * Add multiple custom commands paths |
||
573 | * |
||
574 | * @param array $paths Custom commands paths to add |
||
575 | * @param bool $before If the paths should be prepended or appended to the list |
||
576 | * |
||
577 | * @return \Longman\TelegramBot\Telegram |
||
578 | */ |
||
579 | 1 | public function addCommandsPaths(array $paths, $before = true) |
|
587 | |||
588 | /** |
||
589 | * Set custom upload path |
||
590 | * |
||
591 | * @param string $path Custom upload path |
||
592 | * |
||
593 | * @return \Longman\TelegramBot\Telegram |
||
594 | */ |
||
595 | 39 | public function setUploadPath($path) |
|
600 | |||
601 | /** |
||
602 | * Get custom upload path |
||
603 | * |
||
604 | * @return string |
||
605 | */ |
||
606 | public function getUploadPath() |
||
610 | |||
611 | /** |
||
612 | * Set custom download path |
||
613 | * |
||
614 | * @param string $path Custom download path |
||
615 | * |
||
616 | * @return \Longman\TelegramBot\Telegram |
||
617 | */ |
||
618 | 39 | public function setDownloadPath($path) |
|
623 | |||
624 | /** |
||
625 | * Get custom download path |
||
626 | * |
||
627 | * @return string |
||
628 | */ |
||
629 | public function getDownloadPath() |
||
633 | |||
634 | /** |
||
635 | * Set command config |
||
636 | * |
||
637 | * Provide further variables to a particular commands. |
||
638 | * For example you can add the channel name at the command /sendtochannel |
||
639 | * Or you can add the api key for external service. |
||
640 | * |
||
641 | * @param string $command |
||
642 | * @param array $config |
||
643 | * |
||
644 | * @return \Longman\TelegramBot\Telegram |
||
645 | */ |
||
646 | 13 | public function setCommandConfig($command, array $config) |
|
651 | |||
652 | /** |
||
653 | * Get command config |
||
654 | * |
||
655 | * @param string $command |
||
656 | * |
||
657 | * @return array |
||
658 | */ |
||
659 | 24 | public function getCommandConfig($command) |
|
663 | |||
664 | /** |
||
665 | * Get API key |
||
666 | * |
||
667 | * @return string |
||
668 | */ |
||
669 | 1 | public function getApiKey() |
|
673 | |||
674 | /** |
||
675 | * Get Bot name |
||
676 | * |
||
677 | * @return string |
||
678 | */ |
||
679 | 6 | public function getBotName() |
|
683 | |||
684 | /** |
||
685 | * Get Version |
||
686 | * |
||
687 | * @return string |
||
688 | */ |
||
689 | 1 | public function getVersion() |
|
693 | |||
694 | /** |
||
695 | * Set Webhook for bot |
||
696 | * |
||
697 | * @param string $url |
||
698 | * @param string|null $path_certificate |
||
699 | * |
||
700 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
701 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
702 | */ |
||
703 | public function setWebHook($url, $path_certificate = null) |
||
719 | |||
720 | /** |
||
721 | * Unset Webhook for bot |
||
722 | * |
||
723 | * @return mixed |
||
724 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
725 | */ |
||
726 | public function unsetWebHook() |
||
738 | |||
739 | /** |
||
740 | * Replace function `ucwords` for UTF-8 characters in the class definition and commands |
||
741 | * |
||
742 | * @param string $str |
||
743 | * @param string $encoding (default = 'UTF-8') |
||
744 | * |
||
745 | * @return string |
||
746 | */ |
||
747 | 10 | protected function ucwordsUnicode($str, $encoding = 'UTF-8') |
|
751 | |||
752 | /** |
||
753 | * Replace function `ucfirst` for UTF-8 characters in the class definition and commands |
||
754 | * |
||
755 | * @param string $str |
||
756 | * @param string $encoding (default = 'UTF-8') |
||
757 | * |
||
758 | * @return string |
||
759 | */ |
||
760 | 11 | protected function ucfirstUnicode($str, $encoding = 'UTF-8') |
|
766 | |||
767 | /** |
||
768 | * Enable Botan.io integration |
||
769 | * |
||
770 | * @param $token |
||
771 | * @return \Longman\TelegramBot\Telegram |
||
772 | */ |
||
773 | public function enableBotan($token) |
||
779 | } |
||
780 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.