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.35.0'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Telegram API key |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $api_key = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Telegram Bot name |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $bot_name = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Raw request data (json) for webhook methods |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $input; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Custom commands paths |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $commands_paths = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Current Update object |
||
| 65 | * |
||
| 66 | * @var \Longman\TelegramBot\Entities\Update |
||
| 67 | */ |
||
| 68 | protected $update; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Upload path |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $upload_path; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Download path |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $download_path; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * MySQL integration |
||
| 86 | * |
||
| 87 | * @var boolean |
||
| 88 | */ |
||
| 89 | protected $mysql_enabled = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * PDO object |
||
| 93 | * |
||
| 94 | * @var \PDO |
||
| 95 | */ |
||
| 96 | protected $pdo; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Commands config |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | protected $commands_config = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Admins list |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | protected $admins_list = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * ServerResponse of the last Command execution |
||
| 114 | * |
||
| 115 | * @var \Longman\TelegramBot\Entities\ServerResponse |
||
| 116 | */ |
||
| 117 | protected $last_command_response; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Botan.io integration |
||
| 121 | * |
||
| 122 | * @var boolean |
||
| 123 | */ |
||
| 124 | protected $botan_enabled = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Telegram constructor. |
||
| 128 | * |
||
| 129 | * @param string $api_key |
||
| 130 | * @param string $bot_name |
||
| 131 | * |
||
| 132 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 133 | */ |
||
| 134 | 39 | public function __construct($api_key, $bot_name) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Initialize Database connection |
||
| 159 | * |
||
| 160 | * @param array $credential |
||
| 161 | * @param string $table_prefix |
||
| 162 | * @param string $encoding |
||
| 163 | * |
||
| 164 | * @return \Longman\TelegramBot\Telegram |
||
| 165 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 166 | */ |
||
| 167 | 9 | public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4') |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Initialize Database external connection |
||
| 178 | * |
||
| 179 | * @param PDO $external_pdo_connection PDO database object |
||
| 180 | * @param string $table_prefix |
||
| 181 | * |
||
| 182 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 183 | */ |
||
| 184 | public function enableExternalMysql($external_pdo_connection, $table_prefix = null) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get commands list |
||
| 193 | * |
||
| 194 | * @return array $commands |
||
| 195 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 196 | */ |
||
| 197 | 10 | public function getCommandsList() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get an object instance of the passed command |
||
| 237 | * |
||
| 238 | * @param string $command |
||
| 239 | * |
||
| 240 | * @return \Longman\TelegramBot\Commands\Command|null |
||
| 241 | */ |
||
| 242 | 11 | public function getCommandObject($command) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Set custom input string for debug purposes |
||
| 260 | * |
||
| 261 | * @param string $input (json format) |
||
| 262 | * |
||
| 263 | * @return \Longman\TelegramBot\Telegram |
||
| 264 | */ |
||
| 265 | public function setCustomInput($input) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get custom input string for debug purposes |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function getCustomInput() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get the ServerResponse of the last Command execution |
||
| 284 | * |
||
| 285 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
| 286 | */ |
||
| 287 | public function getLastCommandResponse() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Handle getUpdates method |
||
| 294 | * |
||
| 295 | * @param int|null $limit |
||
| 296 | * @param int|null $timeout |
||
| 297 | * |
||
| 298 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
| 299 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 300 | */ |
||
| 301 | public function handleGetUpdates($limit = null, $timeout = null) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Handle bot request from webhook |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | * |
||
| 344 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 345 | */ |
||
| 346 | public function handle() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get the command name from the command type |
||
| 368 | * |
||
| 369 | * @param string $type |
||
| 370 | * |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | private function getCommandFromType($type) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Process bot Update request |
||
| 380 | * |
||
| 381 | * @param \Longman\TelegramBot\Entities\Update $update |
||
| 382 | * |
||
| 383 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
| 384 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 385 | */ |
||
| 386 | public function processUpdate(Update $update) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Execute /command |
||
| 436 | * |
||
| 437 | * @param string $command |
||
| 438 | * |
||
| 439 | * @return mixed |
||
| 440 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 441 | */ |
||
| 442 | public function executeCommand($command) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Sanitize Command |
||
| 475 | * |
||
| 476 | * @param string $command |
||
| 477 | * |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | 10 | protected function sanitizeCommand($command) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Enable a single Admin account |
||
| 487 | * |
||
| 488 | * @param integer $admin_id Single admin id |
||
| 489 | * |
||
| 490 | * @return \Longman\TelegramBot\Telegram |
||
| 491 | */ |
||
| 492 | 1 | public function enableAdmin($admin_id) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Enable a list of Admin Accounts |
||
| 505 | * |
||
| 506 | * @param array $admin_ids List of admin ids |
||
| 507 | * |
||
| 508 | * @return \Longman\TelegramBot\Telegram |
||
| 509 | */ |
||
| 510 | 1 | public function enableAdmins(array $admin_ids) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Get list of admins |
||
| 521 | * |
||
| 522 | * @return array |
||
| 523 | */ |
||
| 524 | 1 | public function getAdminList() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Check if the passed user is an admin |
||
| 531 | * |
||
| 532 | * If no user id is passed, the current update is checked for a valid message sender. |
||
| 533 | * |
||
| 534 | * @param int|null $user_id |
||
| 535 | * |
||
| 536 | * @return bool |
||
| 537 | */ |
||
| 538 | 11 | public function isAdmin($user_id = null) |
|
| 556 | |||
| 557 | /** |
||
| 558 | * Check if user required the db connection |
||
| 559 | * |
||
| 560 | * @return bool |
||
| 561 | */ |
||
| 562 | public function isDbEnabled() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Add a single custom commands path |
||
| 573 | * |
||
| 574 | * @param string $path Custom commands path to add |
||
| 575 | * @param bool $before If the path should be prepended or appended to the list |
||
| 576 | * |
||
| 577 | * @return \Longman\TelegramBot\Telegram |
||
| 578 | */ |
||
| 579 | 39 | public function addCommandsPath($path, $before = true) |
|
| 593 | |||
| 594 | /** |
||
| 595 | * Add multiple custom commands paths |
||
| 596 | * |
||
| 597 | * @param array $paths Custom commands paths to add |
||
| 598 | * @param bool $before If the paths should be prepended or appended to the list |
||
| 599 | * |
||
| 600 | * @return \Longman\TelegramBot\Telegram |
||
| 601 | */ |
||
| 602 | 1 | public function addCommandsPaths(array $paths, $before = true) |
|
| 610 | |||
| 611 | /** |
||
| 612 | * Set custom upload path |
||
| 613 | * |
||
| 614 | * @param string $path Custom upload path |
||
| 615 | * |
||
| 616 | * @return \Longman\TelegramBot\Telegram |
||
| 617 | */ |
||
| 618 | 39 | public function setUploadPath($path) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Get custom upload path |
||
| 627 | * |
||
| 628 | * @return string |
||
| 629 | */ |
||
| 630 | public function getUploadPath() |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Set custom download path |
||
| 637 | * |
||
| 638 | * @param string $path Custom download path |
||
| 639 | * |
||
| 640 | * @return \Longman\TelegramBot\Telegram |
||
| 641 | */ |
||
| 642 | 39 | public function setDownloadPath($path) |
|
| 648 | |||
| 649 | /** |
||
| 650 | * Get custom download path |
||
| 651 | * |
||
| 652 | * @return string |
||
| 653 | */ |
||
| 654 | public function getDownloadPath() |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Set command config |
||
| 661 | * |
||
| 662 | * Provide further variables to a particular commands. |
||
| 663 | * For example you can add the channel name at the command /sendtochannel |
||
| 664 | * Or you can add the api key for external service. |
||
| 665 | * |
||
| 666 | * @param string $command |
||
| 667 | * @param array $config |
||
| 668 | * |
||
| 669 | * @return \Longman\TelegramBot\Telegram |
||
| 670 | */ |
||
| 671 | 13 | public function setCommandConfig($command, array $config) |
|
| 677 | |||
| 678 | /** |
||
| 679 | * Get command config |
||
| 680 | * |
||
| 681 | * @param string $command |
||
| 682 | * |
||
| 683 | * @return array |
||
| 684 | */ |
||
| 685 | 24 | public function getCommandConfig($command) |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Get API key |
||
| 692 | * |
||
| 693 | * @return string |
||
| 694 | */ |
||
| 695 | 1 | public function getApiKey() |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Get Bot name |
||
| 702 | * |
||
| 703 | * @return string |
||
| 704 | */ |
||
| 705 | 6 | public function getBotName() |
|
| 709 | |||
| 710 | /** |
||
| 711 | * Get Version |
||
| 712 | * |
||
| 713 | * @return string |
||
| 714 | */ |
||
| 715 | 1 | public function getVersion() |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Set Webhook for bot |
||
| 722 | * |
||
| 723 | * @param string $url |
||
| 724 | * @param string|null $path_certificate |
||
| 725 | * |
||
| 726 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
| 727 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 728 | */ |
||
| 729 | public function setWebHook($url, $path_certificate = null) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Unset Webhook for bot |
||
| 748 | * |
||
| 749 | * @return mixed |
||
| 750 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 751 | */ |
||
| 752 | public function unsetWebHook() |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Replace function `ucwords` for UTF-8 characters in the class definition and commands |
||
| 767 | * |
||
| 768 | * @param string $str |
||
| 769 | * @param string $encoding (default = 'UTF-8') |
||
| 770 | * |
||
| 771 | * @return string |
||
| 772 | */ |
||
| 773 | 10 | protected function ucwordsUnicode($str, $encoding = 'UTF-8') |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Replace function `ucfirst` for UTF-8 characters in the class definition and commands |
||
| 780 | * |
||
| 781 | * @param string $str |
||
| 782 | * @param string $encoding (default = 'UTF-8') |
||
| 783 | * |
||
| 784 | * @return string |
||
| 785 | */ |
||
| 786 | 11 | protected function ucfirstUnicode($str, $encoding = 'UTF-8') |
|
| 792 | |||
| 793 | /** |
||
| 794 | * Enable Botan.io integration |
||
| 795 | * |
||
| 796 | * @param $token |
||
| 797 | * |
||
| 798 | * @return \Longman\TelegramBot\Telegram |
||
| 799 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 800 | */ |
||
| 801 | public function enableBotan($token) |
||
| 808 | } |
||
| 809 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.