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 channelPost(Closure $action) |
||
| 66 | |||
| 67 | public function editedChannelPost(Closure $action) |
||
| 71 | |||
| 72 | public function inlineQuery(Closure $action) |
||
| 76 | |||
| 77 | public function chosenInlineResult(Closure $action) |
||
| 81 | |||
| 82 | public function shippingQuery(Closure $action) |
||
| 86 | |||
| 87 | public function preCheckoutQuery(Closure $action) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Use this method to add an event. |
||
| 94 | * If second closure will return true (or if you are passed null instead of closure), first one will be executed. |
||
| 95 | * |
||
| 96 | * @param \Closure $event |
||
| 97 | * @param \Closure|null $checker |
||
| 98 | * |
||
| 99 | * @return \TelegramBot\Api\Client |
||
| 100 | */ |
||
| 101 | 1 | public function on(Closure $event, Closure $checker = null) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Handle updates |
||
| 110 | * |
||
| 111 | * @param Update[] $updates |
||
| 112 | */ |
||
| 113 | 4 | public function handle(array $updates) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Webhook handler |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | * @throws \TelegramBot\Api\InvalidJsonException |
||
| 126 | */ |
||
| 127 | public function run() |
||
| 133 | |||
| 134 | public function getRawBody() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns event function to handling the command. |
||
| 141 | * |
||
| 142 | * @param \Closure $action |
||
| 143 | * |
||
| 144 | * @return \Closure |
||
| 145 | */ |
||
| 146 | 4 | protected static function getEvent(Closure $action) |
|
| 173 | |||
| 174 | View Code Duplication | protected static function getEditedMessageEvent(Closure $action) |
|
| 186 | |||
| 187 | View Code Duplication | protected static function getChannelPostEvent(Closure $action) |
|
| 199 | |||
| 200 | View Code Duplication | protected static function getEditedChannelPostEvent(Closure $action) |
|
| 212 | |||
| 213 | 4 | View Code Duplication | protected static function getInlineQueryEvent(Closure $action) |
| 225 | |||
| 226 | View Code Duplication | protected static function getChosenInlineResultEvent(Closure $action) |
|
| 238 | |||
| 239 | View Code Duplication | protected static function getShippingQueryEvent(Closure $action) |
|
| 251 | |||
| 252 | View Code Duplication | protected static function getPreCheckoutQueryEvent(Closure $action) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Returns check function to handling the command. |
||
| 267 | * |
||
| 268 | * @param string $name |
||
| 269 | * |
||
| 270 | * @return \Closure |
||
| 271 | */ |
||
| 272 | 4 | protected static function getChecker($name) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Returns check function to handling the edited message. |
||
| 288 | * |
||
| 289 | * @return Closure |
||
| 290 | */ |
||
| 291 | protected static function getEditedMessageChecker() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns check function to handling the channel post. |
||
| 300 | * |
||
| 301 | * @return Closure |
||
| 302 | */ |
||
| 303 | protected static function getChannelPostChecker() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns check function to handling the edited channel post. |
||
| 312 | * |
||
| 313 | * @return Closure |
||
| 314 | */ |
||
| 315 | protected static function getEditedChannelPostChecker() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns check function to handling the chosen inline result. |
||
| 324 | * |
||
| 325 | * @return Closure |
||
| 326 | */ |
||
| 327 | protected static function getChosenInlineResultChecker() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Returns check function to handling the inline queries. |
||
| 336 | * |
||
| 337 | * @return Closure |
||
| 338 | */ |
||
| 339 | 4 | protected static function getInlineQueryChecker() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Returns check function to handling the shipping queries. |
||
| 348 | * |
||
| 349 | * @return Closure |
||
| 350 | */ |
||
| 351 | protected static function getShippingQueryChecker() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Returns check function to handling the pre checkout queries. |
||
| 360 | * |
||
| 361 | * @return Closure |
||
| 362 | */ |
||
| 363 | protected static function getPreCheckoutQueryChecker() |
||
| 369 | |||
| 370 | 1 | public function __call($name, array $arguments) |
|
| 379 | } |
||
| 380 |