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:
1 | <?php |
||
35 | class BasicBot extends Core\CoreBot |
||
36 | { |
||
37 | use \PhpBotFramework\Commands\CommandHandler; |
||
38 | |||
39 | /** @internal |
||
40 | * \brief True if the bot is using webhook? */ |
||
41 | protected $_is_webhook; |
||
42 | |||
43 | /** |
||
44 | * \brief Construct an empty base bot. |
||
45 | * \details Construct a base bot that can handle updates. |
||
46 | */ |
||
47 | public function __construct(string $token) |
||
56 | |||
57 | /** @} */ |
||
58 | |||
59 | /** |
||
60 | * \addtogroup Bot |
||
61 | * @{ |
||
62 | */ |
||
63 | |||
64 | /** |
||
65 | * \brief Get update and process it. |
||
66 | * \details Call this method if user is using webhook. |
||
67 | * It'll get bot's update from php::\input, check it and then process it using <b>processUpdate</b>. |
||
68 | */ |
||
69 | public function processWebhookUpdate() |
||
76 | |||
77 | /** |
||
78 | * \brief Get updates received by the bot, and hold the offset in $offset. |
||
79 | * \details Get the <code>update_id</code> of the first update to parse, set it in $offset and |
||
80 | * then it start an infinite loop where it processes updates and keep $offset on the update_id of the last update received. |
||
81 | * Each processUpdate() method call is surrounded by a try/catch. |
||
82 | * @see getUpdates |
||
83 | * @param int $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
84 | * @param int $timeout <i>Optional</i>. Timeout in seconds for long polling. |
||
85 | */ |
||
86 | public function getUpdatesLocal(int $limit = 100, int $timeout = 60) |
||
110 | |||
111 | /** @} */ |
||
112 | |||
113 | /** |
||
114 | * @internal |
||
115 | * \brief Dispatch each update to the right method (processMessage, processCallbackQuery, etc). |
||
116 | * \details Set $chat_id for each update, $text, $data and $query are set for each update that contains them. |
||
117 | * @param array $update Reference to the update received. |
||
118 | * @return int The id of the update processed. |
||
119 | */ |
||
120 | protected function processUpdate(array $update) : int |
||
152 | |||
153 | /** |
||
154 | * \brief Called every message received by the bot. |
||
155 | * \details Override it to script the bot answer for each message. |
||
156 | * <code>$chat_id</code> and <code>$text</code>, if the message contains text(use getMessageText() to access it), set inside of this function. |
||
157 | * @param Message $message Reference to the message received. |
||
158 | */ |
||
159 | protected function processMessage(Message $message) |
||
162 | |||
163 | /** |
||
164 | * \brief Called every callback query received by the bot. |
||
165 | * \details Override it to script the bot answer for each callback. |
||
166 | * <code>$chat_id</code> and <code>$data</code>, if set in the callback query(use getCallbackData() to access it) set inside of this function. |
||
167 | * @param CallbackQuery $callback_query Reference to the callback query received. |
||
168 | */ |
||
169 | protected function processCallbackQuery(CallbackQuery $callback_query) |
||
172 | |||
173 | /** |
||
174 | * \brief Called every inline query received by the bot. |
||
175 | * \details Override it to script the bot answer for each inline query. |
||
176 | * $chat_id and $query(use getInlineQuery() to access it) set inside of this function. |
||
177 | * @param InlineQuery $inline_query Reference to the inline query received. |
||
178 | */ |
||
179 | protected function processInlineQuery(InlineQuery $inline_query) |
||
182 | |||
183 | /** |
||
184 | * \brief Called every chosen inline result received by the bot. |
||
185 | * \details Override it to script the bot answer for each chosen inline result. |
||
186 | * <code>$chat_id</code> set inside of this function. |
||
187 | * @param ChosenInlineResult $chosen_inline_result Reference to the chosen inline result received. |
||
188 | */ |
||
189 | protected function processChosenInlineResult(ChosenInlineResult $chosen_inline_result) |
||
192 | |||
193 | /** |
||
194 | * \brief Called every chosen edited message received by the bot. |
||
195 | * \details Override it to script the bot answer for each edited message. |
||
196 | * <code>$chat_id</code> set inside of this function. |
||
197 | * @param Message $edited_message The message edited by the user. |
||
198 | */ |
||
199 | protected function processEditedMessage(Message $edited_message) |
||
202 | |||
203 | /** |
||
204 | * \brief Called every new post in the channel where the bot is in. |
||
205 | * \details Override it to script the bot answer for each post sent in a channel. |
||
206 | * <code>$chat_id</code> set inside of this function. |
||
207 | * @param Message $post The message sent in the channel. |
||
208 | */ |
||
209 | protected function processChannelPost(Message $post) |
||
212 | |||
213 | /** |
||
214 | * \brief Called every time a post get edited in the channel where the bot is in. |
||
215 | * \details Override it to script the bot answer for each post edited in a channel. |
||
216 | * <code>$chat_id</code> set inside of this function. |
||
217 | * @param Message $post The message edited in the channel. |
||
218 | */ |
||
219 | protected function processEditedChannelPost(Message $edited_post) |
||
222 | |||
223 | /** @} */ |
||
224 | } |
||
225 |
This check looks for
while
loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.Consider removing the loop.