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 BotManager 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 BotManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 21 | class BotManager |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var string Telegram post servers IP range |
||
| 25 | */ |
||
| 26 | const TELEGRAM_IP_RANGE = '149.154.167.197-149.154.167.233'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string The output for testing, instead of echoing |
||
| 30 | */ |
||
| 31 | private $output = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var \Longman\TelegramBot\Telegram |
||
| 35 | */ |
||
| 36 | private $telegram; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \TelegramBot\TelegramBotManager\Params Object that manages the parameters. |
||
| 40 | */ |
||
| 41 | private $params; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \TelegramBot\TelegramBotManager\Action Object that contains the current action. |
||
| 45 | */ |
||
| 46 | private $action; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var callable |
||
| 50 | */ |
||
| 51 | private $custom_get_updates_callback; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * BotManager constructor. |
||
| 55 | * |
||
| 56 | * @param array $params |
||
| 57 | * |
||
| 58 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException |
||
| 59 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidActionException |
||
| 60 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 61 | */ |
||
| 62 | public function __construct(array $params) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Check if we're busy running the PHPUnit tests. |
||
| 79 | * |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | public static function inTest(): bool |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Return the Telegram object. |
||
| 89 | * |
||
| 90 | * @return \Longman\TelegramBot\Telegram |
||
| 91 | */ |
||
| 92 | public function getTelegram(): Telegram |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get the Params object. |
||
| 99 | * |
||
| 100 | * @return \TelegramBot\TelegramBotManager\Params |
||
| 101 | */ |
||
| 102 | public function getParams(): Params |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get the Action object. |
||
| 109 | * |
||
| 110 | * @return \TelegramBot\TelegramBotManager\Action |
||
| 111 | */ |
||
| 112 | public function getAction(): Action |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Run this thing in all its glory! |
||
| 119 | * |
||
| 120 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 121 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 122 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
| 123 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidWebhookException |
||
| 124 | * @throws \Exception |
||
| 125 | */ |
||
| 126 | public function run(): self |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Initialise all loggers. |
||
| 154 | * |
||
| 155 | * @param array $log_paths |
||
| 156 | * |
||
| 157 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 158 | * @throws \Exception |
||
| 159 | */ |
||
| 160 | public function initLogging(array $log_paths): self |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Make sure the passed secret is valid. |
||
| 173 | * |
||
| 174 | * @param bool $force Force validation, even on CLI. |
||
| 175 | * |
||
| 176 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 177 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
| 178 | */ |
||
| 179 | public function validateSecret(bool $force = false): self |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Make sure the webhook is valid and perform the requested webhook operation. |
||
| 195 | * |
||
| 196 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 197 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 198 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidWebhookException |
||
| 199 | */ |
||
| 200 | public function validateAndSetWebhook(): self |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Save the test output and echo it if we're not in a test. |
||
| 239 | * |
||
| 240 | * @param string $output |
||
| 241 | * |
||
| 242 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 243 | */ |
||
| 244 | private function handleOutput(string $output): self |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Set any extra bot features that have been assigned on construction. |
||
| 257 | * |
||
| 258 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 259 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 260 | */ |
||
| 261 | public function setBotExtras(): self |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Set extra bot parameters for Telegram object. |
||
| 271 | * |
||
| 272 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 273 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 274 | */ |
||
| 275 | protected function setBotExtrasTelegram(): self |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set extra bot parameters for Request class. |
||
| 310 | * |
||
| 311 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 312 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 313 | */ |
||
| 314 | protected function setBotExtrasRequest(): self |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
| 339 | * |
||
| 340 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 341 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
| 342 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 343 | */ |
||
| 344 | public function handleRequest(): self |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Handle cron. |
||
| 359 | * |
||
| 360 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 361 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 362 | */ |
||
| 363 | public function handleCron(): self |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get the number of seconds the script should loop. |
||
| 378 | * |
||
| 379 | * @return int |
||
| 380 | */ |
||
| 381 | View Code Duplication | public function getLoopTime(): int |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Get the number of seconds the script should wait after each getUpdates request. |
||
| 398 | * |
||
| 399 | * @return int |
||
| 400 | */ |
||
| 401 | View Code Duplication | public function getLoopInterval(): int |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Loop the getUpdates method for the passed amount of seconds. |
||
| 415 | * |
||
| 416 | * @param int $loop_time_in_seconds |
||
| 417 | * @param int $loop_interval_in_seconds |
||
| 418 | * |
||
| 419 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 420 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 421 | */ |
||
| 422 | public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interval_in_seconds = 2): self |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Set a custom callback for handling the output of the getUpdates results. |
||
| 441 | * |
||
| 442 | * @param callable $callback |
||
| 443 | * |
||
| 444 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 445 | */ |
||
| 446 | public function setCustomGetUpdatesCallback(callable $callback): BotManager |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Handle the updates using the getUpdates method. |
||
| 454 | * |
||
| 455 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 456 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 457 | */ |
||
| 458 | public function handleGetUpdates(): self |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Return the default output for getUpdates handling. |
||
| 474 | * |
||
| 475 | * @param Entities\ServerResponse $get_updates_response |
||
| 476 | * |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | protected function defaultGetUpdatesCallback($get_updates_response): string |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Handle the updates using the Webhook method. |
||
| 517 | * |
||
| 518 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 519 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 520 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
| 521 | */ |
||
| 522 | public function handleWebhook(): self |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Return the current test output and clear it. |
||
| 531 | * |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | public function getOutput(): string |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Check if this is a valid request coming from a Telegram API IP address. |
||
| 544 | * |
||
| 545 | * @link https://core.telegram.org/bots/webhooks#the-short-version |
||
| 546 | * |
||
| 547 | * @return bool |
||
| 548 | */ |
||
| 549 | public function isValidRequest(): bool |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Make sure this is a valid request. |
||
| 572 | * |
||
| 573 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
| 574 | */ |
||
| 575 | private function validateRequest() |
||
| 581 | } |
||
| 582 |