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) |
|
39 | { |
||
40 | 7 | $this->api = new BotApi($token); |
|
41 | 7 | $this->events = new EventCollection($trackerToken); |
|
42 | 7 | } |
|
43 | |||
44 | /** |
||
45 | * Use this method to add command. Parameters will be automatically parsed and passed to closure. |
||
46 | * |
||
47 | * @param string $name |
||
48 | * @param \Closure $action |
||
49 | * |
||
50 | * @return \TelegramBot\Api\Client |
||
51 | */ |
||
52 | public function command($name, Closure $action) |
||
56 | |||
57 | public function editedMessage(Closure $action) |
||
61 | |||
62 | public function callbackQuery(Closure $action) |
||
66 | |||
67 | public function channelPost(Closure $action) |
||
71 | |||
72 | public function editedChannelPost(Closure $action) |
||
76 | |||
77 | public function inlineQuery(Closure $action) |
||
81 | |||
82 | public function chosenInlineResult(Closure $action) |
||
86 | |||
87 | public function shippingQuery(Closure $action) |
||
91 | |||
92 | public function preCheckoutQuery(Closure $action) |
||
96 | |||
97 | /** |
||
98 | * Use this method to add an event. |
||
99 | * If second closure will return true (or if you are passed null instead of closure), first one will be executed. |
||
100 | * |
||
101 | * @param \Closure $event |
||
102 | * @param \Closure|null $checker |
||
103 | * |
||
104 | * @return \TelegramBot\Api\Client |
||
105 | */ |
||
106 | 1 | public function on(Closure $event, Closure $checker = null) |
|
112 | |||
113 | /** |
||
114 | * Handle updates |
||
115 | * |
||
116 | * @param Update[] $updates |
||
117 | */ |
||
118 | 4 | public function handle(array $updates) |
|
125 | |||
126 | /** |
||
127 | * Webhook handler |
||
128 | * |
||
129 | * @return array |
||
130 | * @throws \TelegramBot\Api\InvalidJsonException |
||
131 | */ |
||
132 | public function run() |
||
138 | |||
139 | public function getRawBody() |
||
143 | |||
144 | /** |
||
145 | * Returns event function to handling the command. |
||
146 | * |
||
147 | * @param \Closure $action |
||
148 | * |
||
149 | * @return \Closure |
||
150 | */ |
||
151 | 4 | protected static function getEvent(Closure $action) |
|
178 | |||
179 | View Code Duplication | protected static function getEditedMessageEvent(Closure $action) |
|
191 | |||
192 | View Code Duplication | protected static function getChannelPostEvent(Closure $action) |
|
204 | |||
205 | View Code Duplication | protected static function getCallbackQueryEvent(Closure $action) |
|
217 | |||
218 | View Code Duplication | protected static function getEditedChannelPostEvent(Closure $action) |
|
230 | |||
231 | 4 | View Code Duplication | protected static function getInlineQueryEvent(Closure $action) |
243 | |||
244 | View Code Duplication | protected static function getChosenInlineResultEvent(Closure $action) |
|
256 | |||
257 | View Code Duplication | protected static function getShippingQueryEvent(Closure $action) |
|
269 | |||
270 | View Code Duplication | protected static function getPreCheckoutQueryEvent(Closure $action) |
|
282 | |||
283 | /** |
||
284 | * Returns check function to handling the command. |
||
285 | * |
||
286 | * @param string $name |
||
287 | * |
||
288 | * @return \Closure |
||
289 | */ |
||
290 | 4 | protected static function getChecker($name) |
|
303 | |||
304 | /** |
||
305 | * Returns check function to handling the edited message. |
||
306 | * |
||
307 | * @return Closure |
||
308 | */ |
||
309 | protected static function getEditedMessageChecker() |
||
315 | |||
316 | /** |
||
317 | * Returns check function to handling the channel post. |
||
318 | * |
||
319 | * @return Closure |
||
320 | */ |
||
321 | protected static function getChannelPostChecker() |
||
327 | |||
328 | /** |
||
329 | * Returns check function to handling the callbackQuery. |
||
330 | * |
||
331 | * @return Closure |
||
332 | */ |
||
333 | protected static function getCallbackQueryChecker() |
||
339 | |||
340 | /** |
||
341 | * Returns check function to handling the edited channel post. |
||
342 | * |
||
343 | * @return Closure |
||
344 | */ |
||
345 | protected static function getEditedChannelPostChecker() |
||
351 | |||
352 | /** |
||
353 | * Returns check function to handling the chosen inline result. |
||
354 | * |
||
355 | * @return Closure |
||
356 | */ |
||
357 | protected static function getChosenInlineResultChecker() |
||
363 | |||
364 | /** |
||
365 | * Returns check function to handling the inline queries. |
||
366 | * |
||
367 | * @return Closure |
||
368 | */ |
||
369 | 4 | protected static function getInlineQueryChecker() |
|
375 | |||
376 | /** |
||
377 | * Returns check function to handling the shipping queries. |
||
378 | * |
||
379 | * @return Closure |
||
380 | */ |
||
381 | protected static function getShippingQueryChecker() |
||
387 | |||
388 | /** |
||
389 | * Returns check function to handling the pre checkout queries. |
||
390 | * |
||
391 | * @return Closure |
||
392 | */ |
||
393 | protected static function getPreCheckoutQueryChecker() |
||
399 | |||
400 | 1 | public function __call($name, array $arguments) |
|
409 | } |
||
410 |