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