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 | * @return int Max Update Id |
||
| 119 | */ |
||
| 120 | 4 | public function handle(array $updates) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Webhook handler |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | * @throws \TelegramBot\Api\InvalidJsonException |
||
| 140 | */ |
||
| 141 | public function run() |
||
| 147 | |||
| 148 | public function getRawBody() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns event function to handling the command. |
||
| 155 | * |
||
| 156 | * @param \Closure $action |
||
| 157 | * |
||
| 158 | * @return \Closure |
||
| 159 | */ |
||
| 160 | 4 | protected static function getEvent(Closure $action) |
|
| 187 | |||
| 188 | View Code Duplication | protected static function getEditedMessageEvent(Closure $action) |
|
| 200 | |||
| 201 | View Code Duplication | protected static function getChannelPostEvent(Closure $action) |
|
| 213 | |||
| 214 | View Code Duplication | protected static function getCallbackQueryEvent(Closure $action) |
|
| 226 | |||
| 227 | View Code Duplication | protected static function getEditedChannelPostEvent(Closure $action) |
|
| 239 | |||
| 240 | 4 | View Code Duplication | protected static function getInlineQueryEvent(Closure $action) |
| 252 | |||
| 253 | View Code Duplication | protected static function getChosenInlineResultEvent(Closure $action) |
|
| 265 | |||
| 266 | View Code Duplication | protected static function getShippingQueryEvent(Closure $action) |
|
| 278 | |||
| 279 | View Code Duplication | protected static function getPreCheckoutQueryEvent(Closure $action) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Returns check function to handling the command. |
||
| 294 | * |
||
| 295 | * @param string $name |
||
| 296 | * |
||
| 297 | * @return \Closure |
||
| 298 | */ |
||
| 299 | 4 | protected static function getChecker($name) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Returns check function to handling the edited message. |
||
| 315 | * |
||
| 316 | * @return Closure |
||
| 317 | */ |
||
| 318 | protected static function getEditedMessageChecker() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Returns check function to handling the channel post. |
||
| 327 | * |
||
| 328 | * @return Closure |
||
| 329 | */ |
||
| 330 | protected static function getChannelPostChecker() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Returns check function to handling the callbackQuery. |
||
| 339 | * |
||
| 340 | * @return Closure |
||
| 341 | */ |
||
| 342 | protected static function getCallbackQueryChecker() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Returns check function to handling the edited channel post. |
||
| 351 | * |
||
| 352 | * @return Closure |
||
| 353 | */ |
||
| 354 | protected static function getEditedChannelPostChecker() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Returns check function to handling the chosen inline result. |
||
| 363 | * |
||
| 364 | * @return Closure |
||
| 365 | */ |
||
| 366 | protected static function getChosenInlineResultChecker() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Returns check function to handling the inline queries. |
||
| 375 | * |
||
| 376 | * @return Closure |
||
| 377 | */ |
||
| 378 | 4 | protected static function getInlineQueryChecker() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Returns check function to handling the shipping queries. |
||
| 387 | * |
||
| 388 | * @return Closure |
||
| 389 | */ |
||
| 390 | protected static function getShippingQueryChecker() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Returns check function to handling the pre checkout queries. |
||
| 399 | * |
||
| 400 | * @return Closure |
||
| 401 | */ |
||
| 402 | protected static function getPreCheckoutQueryChecker() |
||
| 408 | |||
| 409 | 1 | public function __call($name, array $arguments) |
|
| 418 | } |
||
| 419 |