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 Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Client |
||
16 | { |
||
17 | /** |
||
18 | * RegExp for bot commands |
||
19 | */ |
||
20 | const REGEXP = '/^(?:@\w+\s)?\/([^\s@]+)(@\S+)?\s?(.*)$/'; |
||
21 | |||
22 | /** |
||
23 | * @var \TelegramBot\Api\BotApi |
||
24 | */ |
||
25 | protected $api; |
||
26 | |||
27 | /** |
||
28 | * @var \TelegramBot\Api\Events\EventCollection |
||
29 | */ |
||
30 | protected $events; |
||
31 | |||
32 | /** |
||
33 | * Client constructor |
||
34 | * |
||
35 | * @param string $token Telegram Bot API token |
||
36 | * @param string|null $trackerToken Yandex AppMetrica application api_key |
||
37 | */ |
||
38 | 7 | public function __construct($token, $trackerToken = null) |
|
43 | |||
44 | /** |
||
45 | * @param Closure $action |
||
46 | * @return $this |
||
47 | */ |
||
48 | public function anyUpdate(Closure $action) |
||
64 | |||
65 | /** |
||
66 | * Use this method to add command. Parameters will be automatically parsed and passed to closure. |
||
67 | * |
||
68 | * @param string $name |
||
69 | * @param \Closure $action |
||
70 | * @return \TelegramBot\Api\Client |
||
71 | */ |
||
72 | public function command($name, Closure $action) |
||
76 | |||
77 | public function message(Closure $action) |
||
81 | |||
82 | public function editedMessage(Closure $action) |
||
86 | |||
87 | public function callbackQuery(Closure $action) |
||
91 | |||
92 | public function channelPost(Closure $action) |
||
96 | |||
97 | public function editedChannelPost(Closure $action) |
||
101 | |||
102 | public function inlineQuery(Closure $action) |
||
106 | |||
107 | public function chosenInlineResult(Closure $action) |
||
111 | |||
112 | public function shippingQuery(Closure $action) |
||
116 | |||
117 | public function preCheckoutQuery(Closure $action) |
||
121 | |||
122 | /** |
||
123 | * Use this method to add an event. |
||
124 | * If second closure will return true (or if you are passed null instead of closure), first one will be executed. |
||
125 | * |
||
126 | * @param \Closure $event |
||
127 | * @param \Closure|null $checker |
||
128 | * @return \TelegramBot\Api\Client |
||
129 | */ |
||
130 | 1 | public function on(Closure $event, Closure $checker = null) |
|
136 | |||
137 | /** |
||
138 | * Handle updates |
||
139 | * |
||
140 | * @param Update[] $updates |
||
141 | */ |
||
142 | 4 | public function handle(array $updates) |
|
149 | |||
150 | /** |
||
151 | * Webhook handler |
||
152 | * |
||
153 | * @return array |
||
154 | * @throws \TelegramBot\Api\InvalidJsonException |
||
155 | */ |
||
156 | public function run() |
||
162 | |||
163 | public function getRawBody() |
||
167 | |||
168 | /** |
||
169 | * Returns event function to handling the command. |
||
170 | * |
||
171 | * @param \Closure $action |
||
172 | * @return \Closure |
||
173 | */ |
||
174 | 4 | protected static function getEvent(Closure $action) |
|
201 | |||
202 | View Code Duplication | protected static function getMessageEvent(Closure $action) |
|
214 | |||
215 | View Code Duplication | protected static function getEditedMessageEvent(Closure $action) |
|
227 | |||
228 | View Code Duplication | protected static function getChannelPostEvent(Closure $action) |
|
240 | |||
241 | View Code Duplication | protected static function getCallbackQueryEvent(Closure $action) |
|
253 | |||
254 | View Code Duplication | protected static function getEditedChannelPostEvent(Closure $action) |
|
266 | |||
267 | 4 | View Code Duplication | protected static function getInlineQueryEvent(Closure $action) |
279 | |||
280 | View Code Duplication | protected static function getChosenInlineResultEvent(Closure $action) |
|
292 | |||
293 | View Code Duplication | protected static function getShippingQueryEvent(Closure $action) |
|
305 | |||
306 | View Code Duplication | protected static function getPreCheckoutQueryEvent(Closure $action) |
|
318 | |||
319 | View Code Duplication | protected static function getPollEvent(Closure $action) |
|
331 | |||
332 | View Code Duplication | protected static function getPollAnswerEvent(Closure $action) |
|
344 | |||
345 | /** |
||
346 | * Returns check function to handling the command. |
||
347 | * |
||
348 | * @param string $name |
||
349 | * @return \Closure |
||
350 | */ |
||
351 | 4 | protected static function getChecker($name) |
|
364 | |||
365 | /** |
||
366 | * Returns check function to handling the message. |
||
367 | * |
||
368 | * @return Closure |
||
369 | */ |
||
370 | protected static function getMessageChecker() |
||
376 | |||
377 | /** |
||
378 | * Returns check function to handling the edited message. |
||
379 | * |
||
380 | * @return Closure |
||
381 | */ |
||
382 | protected static function getEditedMessageChecker() |
||
388 | |||
389 | /** |
||
390 | * Returns check function to handling the channel post. |
||
391 | * |
||
392 | * @return Closure |
||
393 | */ |
||
394 | protected static function getChannelPostChecker() |
||
400 | |||
401 | /** |
||
402 | * Returns check function to handling the callbackQuery. |
||
403 | * |
||
404 | * @return Closure |
||
405 | */ |
||
406 | protected static function getCallbackQueryChecker() |
||
412 | |||
413 | /** |
||
414 | * Returns check function to handling the edited channel post. |
||
415 | * |
||
416 | * @return Closure |
||
417 | */ |
||
418 | protected static function getEditedChannelPostChecker() |
||
424 | |||
425 | /** |
||
426 | * Returns check function to handling the chosen inline result. |
||
427 | * |
||
428 | * @return Closure |
||
429 | */ |
||
430 | protected static function getChosenInlineResultChecker() |
||
436 | |||
437 | /** |
||
438 | * Returns check function to handling the inline queries. |
||
439 | * |
||
440 | * @return Closure |
||
441 | */ |
||
442 | 4 | protected static function getInlineQueryChecker() |
|
448 | |||
449 | /** |
||
450 | * Returns check function to handling the shipping queries. |
||
451 | * |
||
452 | * @return Closure |
||
453 | */ |
||
454 | protected static function getShippingQueryChecker() |
||
460 | |||
461 | /** |
||
462 | * Returns check function to handling the pre checkout queries. |
||
463 | * |
||
464 | * @return Closure |
||
465 | */ |
||
466 | protected static function getPreCheckoutQueryChecker() |
||
472 | |||
473 | /** |
||
474 | * Returns check function to handling the message. |
||
475 | * |
||
476 | * @return Closure |
||
477 | */ |
||
478 | protected static function getPollChecker() |
||
484 | |||
485 | /** |
||
486 | * Returns check function to handling the message. |
||
487 | * |
||
488 | * @return Closure |
||
489 | */ |
||
490 | protected static function getPollAnswerChecker() |
||
496 | |||
497 | 1 | public function __call($name, array $arguments) |
|
507 | } |
||
508 |