Complex classes like Bot 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 Bot, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Bot extends CoreBot { |
||
| 13 | |||
| 14 | use LongPolling, |
||
| 15 | MessageCommand, |
||
| 16 | CallbackCommand, |
||
| 17 | DatabaseHandler, |
||
| 18 | BotState, |
||
| 19 | Localization; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * \addtogroup Bot Bot |
||
| 23 | * \brief Properties and methods to handle the TelegramBot. |
||
| 24 | * \details Here are listed all the properties and methods that will help the developer create the basic bot functions. |
||
| 25 | * @{ |
||
| 26 | */ |
||
| 27 | |||
| 28 | /** \brief Text received in messages */ |
||
| 29 | protected $_text; |
||
| 30 | |||
| 31 | /** \brief Data received in callback query */ |
||
| 32 | protected $_data; |
||
| 33 | |||
| 34 | /** \brief Query sent by the user in the inline query */ |
||
| 35 | protected $_query; |
||
| 36 | |||
| 37 | /** \brief Store the inline keyboard */ |
||
| 38 | public $keyboard; |
||
| 39 | |||
| 40 | /** \brief Pdo reference */ |
||
| 41 | public $pdo; |
||
| 42 | |||
| 43 | /** \brief Redis connection */ |
||
| 44 | public $redis; |
||
| 45 | |||
| 46 | /** @} */ |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * \addtogroup Bot |
||
| 51 | * @{ |
||
| 52 | */ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * \brief Construct an empy bot. |
||
| 56 | * \details Construct a bot with commands, multilanguage and status. |
||
| 57 | */ |
||
| 58 | public function __construct(string $token) { |
||
| 70 | |||
| 71 | /** \brief Descruct the class. */ |
||
| 72 | public function __destruct() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * \brief Get the text of the message, if set (for updates of type "message"). |
||
| 84 | * @return Text of the message, empty string if not set. |
||
| 85 | */ |
||
| 86 | public function getMessageText() : string { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * \brief Get the data of callback query, if set (for updates of type "callback_query"). |
||
| 100 | * @return Data of the callback query, empty string if not set. |
||
| 101 | */ |
||
| 102 | public function getCallbackData() : string { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * \brief Get the query received from the inline query (for updates of type "inline_query"). |
||
| 116 | * @return The query sent by the user, throw exception if the current update is not an inline query. |
||
| 117 | */ |
||
| 118 | public function getInlineQuery() : string { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * \brief Get update and process it. |
||
| 131 | * \details Call this method if you are using webhook. |
||
| 132 | * It will get update from php::\input, check it and then process it using processUpdate. |
||
| 133 | */ |
||
| 134 | public function processWebhookUpdate() { |
||
| 141 | |||
| 142 | /** @} */ |
||
| 143 | |||
| 144 | /** |
||
| 145 | * \addtogroup Core Core(Internal) |
||
| 146 | * @{ |
||
| 147 | */ |
||
| 148 | |||
| 149 | /** |
||
| 150 | * \brief Init variables to skip parsing commands if there aren't any. |
||
| 151 | * \details Called internnaly by |
||
| 152 | * - <code>getUpdatesLocal</code> |
||
| 153 | * - <code>getUpdatesRedis</code> |
||
| 154 | * - <code>getUpdatesDatabase</code> |
||
| 155 | * - <code>processWebhookUpdate</code> |
||
| 156 | */ |
||
| 157 | private function initBot() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * \brief Dispatch each update to the right method (processMessage, processCallbackQuery, etc). |
||
| 169 | * \details Set $chat_id for each update, $text, $data and $query are set for each update that contains them. |
||
| 170 | * It also calls commands for each updates, before process methods. |
||
| 171 | * @param $update Reference to the update received. |
||
| 172 | * @return The id of the update processed. |
||
| 173 | */ |
||
| 174 | public function processUpdate(array $update) : int { |
||
| 331 | |||
| 332 | /** @} */ |
||
| 333 | |||
| 334 | /** |
||
| 335 | * \addtogroup Bot Bot |
||
| 336 | * @{ |
||
| 337 | */ |
||
| 338 | |||
| 339 | /** |
||
| 340 | * \brief Called every message received by the bot. |
||
| 341 | * \details Override it to script the bot answer for each message. |
||
| 342 | * <code>$chat_id</code> and <code>$text</code>, if the message contains text(use getMessageText() to access it), set inside of this function. |
||
| 343 | * @param $message Reference to the message received. |
||
| 344 | */ |
||
| 345 | protected function processMessage($message) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * \brief Called every callback query received by the bot. |
||
| 351 | * \details Override it to script the bot answer for each callback. |
||
| 352 | * <code>$chat_id</code> and <code>$data</code>, if set in the callback query(use getCallbackData() to access it) set inside of this function. |
||
| 353 | * @param $callback_query Reference to the callback query received. |
||
| 354 | */ |
||
| 355 | protected function processCallbackQuery($callback_query) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * \brief Called every inline query received by the bot. |
||
| 361 | * \details Override it to script the bot answer for each inline query. |
||
| 362 | * $chat_id and $query(use getInlineQuery() to access it) set inside of this function. |
||
| 363 | * @param $inline_query Reference to the inline query received. |
||
| 364 | */ |
||
| 365 | protected function processInlineQuery($inline_query) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * \brief Called every chosen inline result received by the bot. |
||
| 371 | * \details Override it to script the bot answer for each chosen inline result. |
||
| 372 | * <code>$chat_id</code> set inside of this function. |
||
| 373 | * @param $chosen_inline_result Reference to the chosen inline result received. |
||
| 374 | */ |
||
| 375 | protected function processChosenInlineResult($chosen_inline_result) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * \brief Called every chosen edited message received by the bot. |
||
| 381 | * \details Override it to script the bot answer for each edited message. |
||
| 382 | * <code>$chat_id</code> set inside of this function. |
||
| 383 | * @param $edited_message The message edited by the user. |
||
| 384 | */ |
||
| 385 | protected function processEditedMessage($edited_message) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * \brief Called every new post in the channel where the bot is in. |
||
| 391 | * \details Override it to script the bot answer for each post sent in a channel. |
||
| 392 | * <code>$chat_id</code> set inside of this function. |
||
| 393 | * @param $post The message sent in the channel. |
||
| 394 | */ |
||
| 395 | protected function processChannelPost($post) { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * \brief Called every time a post get edited in the channel where the bot is in. |
||
| 401 | * \details Override it to script the bot answer for each post edited in a channel. |
||
| 402 | * <code>$chat_id</code> set inside of this function. |
||
| 403 | * @param $post The message edited in the channel. |
||
| 404 | */ |
||
| 405 | protected function processEditedChannelPost($edited_post) { |
||
| 408 | |||
| 409 | /** @} */ |
||
| 410 | |||
| 411 | } |
||
| 412 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.