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 |
||
16 | class Bot extends Core\CoreBot { |
||
17 | |||
18 | use LongPolling, |
||
19 | MessageCommand, |
||
20 | CallbackCommand, |
||
21 | DatabaseHandler, |
||
22 | BotState, |
||
23 | Localization; |
||
24 | |||
25 | /** |
||
26 | * \addtogroup Bot Bot |
||
27 | * \brief Properties and methods to handle the TelegramBot. |
||
28 | * \details Here are listed all the properties and methods that will help the developer create the basic bot functions. |
||
29 | * @{ |
||
30 | */ |
||
31 | |||
32 | /** \brief Text received in messages */ |
||
33 | protected $_text; |
||
34 | |||
35 | /** \brief Data received in callback query */ |
||
36 | protected $_data; |
||
37 | |||
38 | /** \brief Query sent by the user in the inline query */ |
||
39 | protected $_query; |
||
40 | |||
41 | /** \brief Store the inline keyboard */ |
||
42 | public $keyboard; |
||
43 | |||
44 | /** \brief Pdo reference */ |
||
45 | public $pdo; |
||
46 | |||
47 | /** \brief Redis connection */ |
||
48 | public $redis; |
||
49 | |||
50 | /** @} */ |
||
51 | |||
52 | |||
53 | /** |
||
54 | * \addtogroup Bot |
||
55 | * @{ |
||
56 | */ |
||
57 | |||
58 | /** |
||
59 | * \brief Construct an empy bot. |
||
60 | * \details Construct a bot with commands, multilanguage and status. |
||
61 | */ |
||
62 | public function __construct(string $token) { |
||
74 | |||
75 | /** \brief Descruct the class. */ |
||
76 | public function __destruct() { |
||
85 | |||
86 | /** |
||
87 | * \brief Get the text of the message, if set (for updates of type "message"). |
||
88 | * @return Text of the message, empty string if not set. |
||
89 | */ |
||
90 | public function getMessageText() : string { |
||
101 | |||
102 | /** |
||
103 | * \brief Get the data of callback query, if set (for updates of type "callback_query"). |
||
104 | * @return Data of the callback query, empty string if not set. |
||
105 | */ |
||
106 | public function getCallbackData() : string { |
||
117 | |||
118 | /** |
||
119 | * \brief Get the query received from the inline query (for updates of type "inline_query"). |
||
120 | * @return The query sent by the user, throw exception if the current update is not an inline query. |
||
121 | */ |
||
122 | public function getInlineQuery() : string { |
||
132 | |||
133 | /** |
||
134 | * \brief Get update and process it. |
||
135 | * \details Call this method if you are using webhook. |
||
136 | * It will get update from php::\input, check it and then process it using processUpdate. |
||
137 | */ |
||
138 | public function processWebhookUpdate() { |
||
145 | |||
146 | /** @} */ |
||
147 | |||
148 | /** |
||
149 | * \addtogroup Core Core(Internal) |
||
150 | * @{ |
||
151 | */ |
||
152 | |||
153 | /** |
||
154 | * \brief Init variables to skip parsing commands if there aren't any. |
||
155 | * \details Called internnaly by |
||
156 | * - <code>getUpdatesLocal</code> |
||
157 | * - <code>getUpdatesRedis</code> |
||
158 | * - <code>getUpdatesDatabase</code> |
||
159 | * - <code>processWebhookUpdate</code> |
||
160 | */ |
||
161 | private function initBot() { |
||
170 | |||
171 | /** |
||
172 | * \brief Dispatch each update to the right method (processMessage, processCallbackQuery, etc). |
||
173 | * \details Set $chat_id for each update, $text, $data and $query are set for each update that contains them. |
||
174 | * It also calls commands for each updates, before process methods. |
||
175 | * @param $update Reference to the update received. |
||
176 | * @return The id of the update processed. |
||
177 | */ |
||
178 | public function processUpdate(array $update) : int { |
||
335 | |||
336 | /** @} */ |
||
337 | |||
338 | /** |
||
339 | * \addtogroup Bot Bot |
||
340 | * @{ |
||
341 | */ |
||
342 | |||
343 | /** |
||
344 | * \brief Called every message received by the bot. |
||
345 | * \details Override it to script the bot answer for each message. |
||
346 | * <code>$chat_id</code> and <code>$text</code>, if the message contains text(use getMessageText() to access it), set inside of this function. |
||
347 | * @param $message Reference to the message received. |
||
348 | */ |
||
349 | protected function processMessage($message) { |
||
352 | |||
353 | /** |
||
354 | * \brief Called every callback query received by the bot. |
||
355 | * \details Override it to script the bot answer for each callback. |
||
356 | * <code>$chat_id</code> and <code>$data</code>, if set in the callback query(use getCallbackData() to access it) set inside of this function. |
||
357 | * @param $callback_query Reference to the callback query received. |
||
358 | */ |
||
359 | protected function processCallbackQuery($callback_query) { |
||
362 | |||
363 | /** |
||
364 | * \brief Called every inline query received by the bot. |
||
365 | * \details Override it to script the bot answer for each inline query. |
||
366 | * $chat_id and $query(use getInlineQuery() to access it) set inside of this function. |
||
367 | * @param $inline_query Reference to the inline query received. |
||
368 | */ |
||
369 | protected function processInlineQuery($inline_query) { |
||
372 | |||
373 | /** |
||
374 | * \brief Called every chosen inline result received by the bot. |
||
375 | * \details Override it to script the bot answer for each chosen inline result. |
||
376 | * <code>$chat_id</code> set inside of this function. |
||
377 | * @param $chosen_inline_result Reference to the chosen inline result received. |
||
378 | */ |
||
379 | protected function processChosenInlineResult($chosen_inline_result) { |
||
382 | |||
383 | /** |
||
384 | * \brief Called every chosen edited message received by the bot. |
||
385 | * \details Override it to script the bot answer for each edited message. |
||
386 | * <code>$chat_id</code> set inside of this function. |
||
387 | * @param $edited_message The message edited by the user. |
||
388 | */ |
||
389 | protected function processEditedMessage($edited_message) { |
||
392 | |||
393 | /** |
||
394 | * \brief Called every new post in the channel where the bot is in. |
||
395 | * \details Override it to script the bot answer for each post sent in a channel. |
||
396 | * <code>$chat_id</code> set inside of this function. |
||
397 | * @param $post The message sent in the channel. |
||
398 | */ |
||
399 | protected function processChannelPost($post) { |
||
402 | |||
403 | /** |
||
404 | * \brief Called every time a post get edited in the channel where the bot is in. |
||
405 | * \details Override it to script the bot answer for each post edited in a channel. |
||
406 | * <code>$chat_id</code> set inside of this function. |
||
407 | * @param $post The message edited in the channel. |
||
408 | */ |
||
409 | protected function processEditedChannelPost($edited_post) { |
||
412 | |||
413 | /** @} */ |
||
414 | |||
415 | } |
||
416 |