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:
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 inlineQuery(Closure $action) |
||
61 | |||
62 | public function shippingQuery(Closure $action) |
||
66 | |||
67 | public function preCheckoutQuery(Closure $action) |
||
71 | |||
72 | /** |
||
73 | * Use this method to add an event. |
||
74 | * If second closure will return true (or if you are passed null instead of closure), first one will be executed. |
||
75 | * |
||
76 | * @param \Closure $event |
||
77 | * @param \Closure|null $checker |
||
78 | * |
||
79 | * @return \TelegramBot\Api\Client |
||
80 | */ |
||
81 | 1 | public function on(Closure $event, Closure $checker = null) |
|
87 | |||
88 | /** |
||
89 | * Handle updates |
||
90 | * |
||
91 | * @param Update[] $updates |
||
92 | */ |
||
93 | 4 | public function handle(array $updates) |
|
100 | |||
101 | /** |
||
102 | * Webhook handler |
||
103 | * |
||
104 | * @return array |
||
105 | * @throws \TelegramBot\Api\InvalidJsonException |
||
106 | */ |
||
107 | public function run() |
||
113 | |||
114 | public function getRawBody() |
||
118 | |||
119 | /** |
||
120 | * Returns event function to handling the command. |
||
121 | * |
||
122 | * @param \Closure $action |
||
123 | * |
||
124 | * @return \Closure |
||
125 | */ |
||
126 | 4 | protected static function getEvent(Closure $action) |
|
153 | |||
154 | 4 | View Code Duplication | protected static function getInlineQueryEvent(Closure $action) |
166 | |||
167 | View Code Duplication | protected static function getShippingQueryEvent(Closure $action) |
|
179 | |||
180 | View Code Duplication | protected static function getPreCheckoutQueryEvent(Closure $action) |
|
192 | |||
193 | /** |
||
194 | * Returns check function to handling the command. |
||
195 | * |
||
196 | * @param string $name |
||
197 | * |
||
198 | * @return \Closure |
||
199 | */ |
||
200 | 4 | protected static function getChecker($name) |
|
213 | |||
214 | /** |
||
215 | * Returns check function to handling the inline queries. |
||
216 | * |
||
217 | * @return Closure |
||
218 | */ |
||
219 | 4 | protected static function getInlineQueryChecker() |
|
225 | |||
226 | /** |
||
227 | * Returns check function to handling the shipping queries. |
||
228 | * |
||
229 | * @return Closure |
||
230 | */ |
||
231 | protected static function getShippingQueryChecker() |
||
237 | |||
238 | /** |
||
239 | * Returns check function to handling the pre checkout queries. |
||
240 | * |
||
241 | * @return Closure |
||
242 | */ |
||
243 | protected static function getPreCheckoutQueryChecker() |
||
249 | |||
250 | 1 | public function __call($name, array $arguments) |
|
259 | } |
||
260 |