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:
Complex classes like Telegram 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 Telegram, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Telegram { |
||
13 | |||
14 | const PARSE_MARKDOWN = 'Markdown'; |
||
15 | const PARSE_HTML = 'HTML'; |
||
16 | |||
17 | const ACTION_TYPING = 'typing'; |
||
18 | const ACTION_UPLOAD_PHOTO = 'upload_photo'; |
||
19 | const ACTION_RECORD_VIDEO = 'record_video'; |
||
20 | const ACTION_UPLOAD_VIDEO = 'upload_video'; |
||
21 | const ACTION_RECORD_AUDIO = 'record_audio'; |
||
22 | const ACTION_UPLOAD_AUDIO = 'upload_audio'; |
||
23 | const ACTION_UPLOAD_DOC = 'upload_document'; |
||
24 | const ACTION_FIND_LOCATION = 'find_location'; |
||
25 | |||
26 | public $api = 'https://api.telegram.org/bot'; |
||
27 | |||
28 | /** |
||
29 | * returned json from telegram api parse to object and save to result |
||
30 | * @var |
||
31 | */ |
||
32 | public $result; |
||
33 | |||
34 | /** |
||
35 | * @name State of bot |
||
36 | * state of bot |
||
37 | * @var object |
||
38 | */ |
||
39 | public $state; |
||
40 | |||
41 | /** |
||
42 | * commands in regex and callback |
||
43 | * @var array |
||
44 | */ |
||
45 | private $commands = []; |
||
46 | |||
47 | /** |
||
48 | * InlineQuery in regex and callback |
||
49 | * @var array |
||
50 | */ |
||
51 | private $inlines = []; |
||
52 | |||
53 | /** |
||
54 | * callbacks |
||
55 | * @var array |
||
56 | */ |
||
57 | private $callbacks = []; |
||
|
|||
58 | |||
59 | /** |
||
60 | * available telegram bot commands |
||
61 | * @var array |
||
62 | */ |
||
63 | private $available_commands = [ |
||
64 | 'getMe', |
||
65 | 'sendMessage', |
||
66 | 'forwardMessage', |
||
67 | 'sendPhoto', |
||
68 | 'sendAudio', |
||
69 | 'sendDocument', |
||
70 | 'sendSticker', |
||
71 | 'sendVideo', |
||
72 | 'sendLocation', |
||
73 | 'sendChatAction', |
||
74 | 'getUserProfilePhotos', |
||
75 | 'answerInlineQuery', |
||
76 | 'getUpdates', |
||
77 | 'setWebhook', |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * pre patterns you can use in regex |
||
82 | * @var array |
||
83 | */ |
||
84 | private $patterns = [ |
||
85 | ':any' => '.*', |
||
86 | ':num' => '[0-9]{0,}', |
||
87 | ':str' => '[a-zA-z]{0,}', |
||
88 | ]; |
||
89 | |||
90 | /** |
||
91 | * |
||
92 | * @param String $token Telegram api token , taken by botfather |
||
93 | */ |
||
94 | public function __construct($token) { |
||
97 | |||
98 | /** |
||
99 | * add new command to the bot |
||
100 | * @param String $cmd |
||
101 | * @param \Closure $func |
||
102 | */ |
||
103 | public function cmd($cmd, $func) { |
||
106 | |||
107 | |||
108 | public function PDO(){ |
||
125 | |||
126 | /** |
||
127 | * add new InlineQuery to the bot |
||
128 | * @param String $cmd |
||
129 | * @param \Closure $func |
||
130 | */ |
||
131 | public function inlineQuery($cmd, $func) { |
||
134 | |||
135 | /** |
||
136 | * this method check for received payload(command, inlinequery and so on) and |
||
137 | * then execute the correct function |
||
138 | * |
||
139 | * @param bool $sleep |
||
140 | */ |
||
141 | public function run($sleep = false) { |
||
153 | |||
154 | /** |
||
155 | * this method used for setWebhook sended payload |
||
156 | */ |
||
157 | public function process($payload) { |
||
162 | |||
163 | private function processPayload($result) { |
||
207 | |||
208 | /** |
||
209 | * get arguments part in regex |
||
210 | * @param $pattern |
||
211 | * @param $payload |
||
212 | * @return mixed|null |
||
213 | */ |
||
214 | private function getArgs(&$pattern, $payload) { |
||
238 | |||
239 | /** |
||
240 | * execute telegram api commands |
||
241 | * @param $command |
||
242 | * @param array $params |
||
243 | */ |
||
244 | private function exec($command, $params = []) { |
||
254 | |||
255 | private function convertToObject($jsonObject , $webhook = false) { |
||
273 | |||
274 | /** |
||
275 | * get the $url content with CURL |
||
276 | * @param $url |
||
277 | * @param $params |
||
278 | * @return mixed |
||
279 | */ |
||
280 | private function curl_get_contents($url, $params) { |
||
296 | |||
297 | /** |
||
298 | * Get current chat id |
||
299 | * @param null $chat_id |
||
300 | * @return int |
||
301 | */ |
||
302 | public function getChatId($chat_id = null) { |
||
318 | |||
319 | /** |
||
320 | * @param null $offset |
||
321 | * @param int $limit |
||
322 | * @param int $timeout |
||
323 | */ |
||
324 | public function getUpdates($offset = null, $limit = 1, $timeout = 1) { |
||
331 | |||
332 | /** |
||
333 | * send message |
||
334 | * @param $text |
||
335 | * @param $chat_id |
||
336 | * @param bool $disable_web_page_preview |
||
337 | * @param null $reply_to_message_id |
||
338 | * @param null $reply_markup |
||
339 | */ |
||
340 | public function sendMessage($text, $chat_id = null, $disable_web_page_preview = false, $reply_to_message_id = null, $reply_markup = null) { |
||
350 | |||
351 | /** |
||
352 | * Get me |
||
353 | */ |
||
354 | public function getMe() { |
||
357 | |||
358 | /** |
||
359 | * @param $from_id |
||
360 | * @param $message_id |
||
361 | * @param null $chat_id |
||
362 | */ |
||
363 | public function forwardMessage($from_id, $message_id, $chat_id = null) { |
||
370 | |||
371 | /** |
||
372 | * @param $photo photo file patch |
||
373 | * @param null $chat_id |
||
374 | * @param null $caption |
||
375 | * @param null $reply_to_message_id |
||
376 | * @param null $reply_markup |
||
377 | */ |
||
378 | View Code Duplication | public function sendPhoto($photo, $chat_id = null, $caption = null, $reply_to_message_id = null, $reply_markup = null) { |
|
389 | |||
390 | /** |
||
391 | * @param $video video file path |
||
392 | * @param null $chat_id |
||
393 | * @param null $reply_to_message_id |
||
394 | * @param null $reply_markup |
||
395 | */ |
||
396 | public function sendVideo($video, $chat_id = null, $reply_to_message_id = null, $reply_markup = null) { |
||
406 | |||
407 | /** |
||
408 | * |
||
409 | * @param $sticker |
||
410 | * @param null $chat_id |
||
411 | * @param null $reply_to_message_id |
||
412 | * @param null $reply_markup |
||
413 | */ |
||
414 | public function sendSticker($sticker, $chat_id = null, $reply_to_message_id = null, $reply_markup = null) { |
||
425 | |||
426 | /** |
||
427 | * @param $latitude |
||
428 | * @param $longitude |
||
429 | * @param null $chat_id |
||
430 | * @param null $reply_to_message_id |
||
431 | * @param null $reply_markup |
||
432 | */ |
||
433 | View Code Duplication | public function sendLocation($latitude, $longitude, $chat_id = null, $reply_to_message_id = null, $reply_markup = null) { |
|
444 | |||
445 | /** |
||
446 | * @param $document |
||
447 | * @param null $chat_id |
||
448 | * @param null $reply_to_message_id |
||
449 | * @param null $reply_markup |
||
450 | */ |
||
451 | public function sendDocument($document, $chat_id = null, $reply_to_message_id = null, $reply_markup = null) { |
||
461 | |||
462 | public function sendAudio($audio, $chat_id = null, $reply_to_message_id = null, $reply_markup = null) { |
||
472 | |||
473 | /** |
||
474 | * send chat action : Telegram::ACTION_TYPING , ... |
||
475 | * @param $action |
||
476 | * @param null $chat_id |
||
477 | */ |
||
478 | public function sendChatAction($action, $chat_id = null) { |
||
486 | |||
487 | /** |
||
488 | * @param $user_id |
||
489 | * @param null $offset |
||
490 | * @param null $limit |
||
491 | */ |
||
492 | public function getUserProfilePhotos($user_id, $offset = null, $limit = null) { |
||
501 | |||
502 | |||
503 | public function answerInlineQuery($inline_query_id, $results, $cache_time = 0, $is_personal = false, $next_offset = '', $switch_pm_text = '', $switch_pm_parameter = '') { |
||
516 | |||
517 | /** |
||
518 | * @param $url |
||
519 | */ |
||
520 | public function setWebhook($url) { |
||
527 | |||
528 | } |
||
529 |
This check marks private properties in classes that are never used. Those properties can be removed.