Conditions | 22 |
Paths | 22 |
Total Lines | 157 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
178 | public function processUpdate(array $update) : int { |
||
179 | |||
180 | if (isset($update['message'])) { |
||
181 | |||
182 | // Set data from the message |
||
183 | $this->_chat_id = $update['message']['chat']['id']; |
||
184 | |||
185 | // If the message contains text |
||
186 | if (isset($update['message']['text'])) { |
||
187 | |||
188 | $this->_text = $update['message']['text']; |
||
189 | |||
190 | } |
||
191 | |||
192 | // If there are commands set by the user |
||
193 | // and there are bot commands in the message, checking message entities |
||
194 | if ($this->_message_commands_set && isset($update['message']['entities']) && $update['message']['entities'][0]['type'] === 'bot_command') { |
||
195 | |||
196 | // The lenght of the command |
||
197 | $length = $update['message']['entities'][0]['length']; |
||
198 | |||
199 | // Offset of the command |
||
200 | $offset = $update['message']['entities'][0]['offset']; |
||
201 | |||
202 | // For each command added by the user |
||
203 | foreach ($this->_message_commands as $trigger) { |
||
204 | |||
205 | // If the current command is a regex |
||
206 | if ($trigger['regex_active']) { |
||
207 | |||
208 | // Use preg_match to check if it is true |
||
209 | $matched = preg_match('/' . $trigger['regex_rule'] . '/', substr($update['message']['text'], $offset + 1, $length)); |
||
210 | |||
211 | // else check if the command sent by the user is the same as the one we are expecting |
||
212 | } else if ($trigger['length'] == $length && mb_strpos($trigger['command'], $update['message']['text'], $offset) !== false) { |
||
213 | |||
214 | // We found a valid command |
||
215 | $matched = true; |
||
216 | |||
217 | } else { |
||
218 | |||
219 | // We did not |
||
220 | $matched = false; |
||
221 | |||
222 | } |
||
223 | |||
224 | // Check the results for the current command |
||
225 | if ($matched) { |
||
226 | |||
227 | // Execute script, |
||
228 | $trigger['script']($this, $update['message']); |
||
229 | |||
230 | // clear text variable |
||
231 | unset($this->_text); |
||
232 | |||
233 | // and return the id of the current update to stop processing this update |
||
234 | return $update['update_id']; |
||
235 | |||
236 | } |
||
237 | |||
238 | } |
||
239 | |||
240 | } |
||
241 | |||
242 | // And process it |
||
243 | $this->processMessage($update['message']); |
||
244 | |||
245 | // clear text variable |
||
246 | unset($this->_text); |
||
247 | |||
248 | // If the update is a callback query |
||
249 | } elseif (isset($update['callback_query'])) { |
||
250 | |||
251 | // Set variables |
||
252 | $this->_chat_id = $update['callback_query']['message']['chat']['id']; |
||
253 | $this->_callback_query_id = $update['callback_query']['id']; |
||
254 | |||
255 | // If data is set for the current callback query |
||
256 | if (isset($update['callback_query']['data'])) { |
||
257 | |||
258 | $this->_data = $update['callback_query']['data']; |
||
259 | |||
260 | } |
||
261 | |||
262 | // Check for callback commands |
||
263 | if (isset($this->_data) && $this->_callback_commands_set) { |
||
264 | |||
265 | // Parse all commands |
||
266 | foreach ($this->_callback_commands as $trigger) { |
||
267 | |||
268 | // If command is found in callback data |
||
269 | if (strpos($trigger['data'], $this->_data) !== false) { |
||
270 | |||
271 | // Trigger the script |
||
272 | $trigger['script']($this, $update['callback_query']); |
||
273 | |||
274 | // Clear data |
||
275 | unset($this->_data); |
||
276 | unset($this->_callback_query_id); |
||
277 | |||
278 | // and return the id of the current update |
||
279 | return $update['update_id']; |
||
280 | |||
281 | } |
||
282 | |||
283 | } |
||
284 | |||
285 | } |
||
286 | |||
287 | // Process the callback query through processCallbackQuery |
||
288 | $this->processCallbackQuery($update['callback_query']); |
||
289 | |||
290 | // Unset callback query variables |
||
291 | unset($this->_callback_query_id); |
||
292 | unset($this->_data); |
||
293 | |||
294 | } elseif (isset($update['inline_query'])) { |
||
295 | |||
296 | $this->_chat_id = $update['inline_query']['from']['id']; |
||
297 | $this->_query = $update['inline_query']['query']; |
||
298 | $this->_inline_query_id = $update['inline_query']['id']; |
||
299 | |||
300 | $this->processInlineQuery($update['inline_query']); |
||
301 | |||
302 | unset($this->_query); |
||
303 | unset($this->_inline_query_id); |
||
304 | |||
305 | } elseif (isset($update['channel_post'])) { |
||
306 | |||
307 | // Set data from the post |
||
308 | $this->_chat_id = $update['channel_post']['chat']['id']; |
||
309 | |||
310 | $this->processChannelPost($update['channel_post']); |
||
311 | |||
312 | } elseif (isset($update['edited_message'])) { |
||
313 | |||
314 | $this->_chat_id = $update['edited_message']['chat']['id']; |
||
315 | |||
316 | $this->processEditedMessage($update['edited_message']); |
||
317 | |||
318 | } elseif (isset($update['edited_channel_post'])) { |
||
319 | |||
320 | $this->_chat_id = $update['edited_channel_post']['chat']['id']; |
||
321 | |||
322 | $this->processEditedChannelPost($update['edited_channel_post']); |
||
323 | |||
324 | } elseif (isset($update['chosen_inline_result'])) { |
||
325 | |||
326 | $this->_chat_id = $update['chosen_inline_result']['chat']['id']; |
||
327 | |||
328 | $this->processChosenInlineResult($update['chosen_inline_result']); |
||
329 | |||
330 | } |
||
331 | |||
332 | return $update['update_id']; |
||
333 | |||
334 | } |
||
335 | |||
416 |