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 | * BotManager constructor. |
||
| 50 | * |
||
| 51 | * @param array $params |
||
| 52 | * |
||
| 53 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException |
||
| 54 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidActionException |
||
| 55 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 56 | */ |
||
| 57 | public function __construct(array $params) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Check if we're busy running the PHPUnit tests. |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public static function inTest(): bool |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Return the Telegram object. |
||
| 84 | * |
||
| 85 | * @return \Longman\TelegramBot\Telegram |
||
| 86 | */ |
||
| 87 | public function getTelegram(): Telegram |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get the Params object. |
||
| 94 | * |
||
| 95 | * @return \TelegramBot\TelegramBotManager\Params |
||
| 96 | */ |
||
| 97 | public function getParams(): Params |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the Action object. |
||
| 104 | * |
||
| 105 | * @return \TelegramBot\TelegramBotManager\Action |
||
| 106 | */ |
||
| 107 | public function getAction(): Action |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Run this thing in all its glory! |
||
| 114 | * |
||
| 115 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 116 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 117 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
| 118 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidWebhookException |
||
| 119 | * @throws \Exception |
||
| 120 | */ |
||
| 121 | public function run(): self |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Initialise all loggers. |
||
| 145 | * |
||
| 146 | * @param array $log_paths |
||
| 147 | * |
||
| 148 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 149 | * @throws \Exception |
||
| 150 | */ |
||
| 151 | public function initLogging(array $log_paths): self |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Make sure the passed secret is valid. |
||
| 164 | * |
||
| 165 | * @param bool $force Force validation, even on CLI. |
||
| 166 | * |
||
| 167 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 168 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
| 169 | */ |
||
| 170 | public function validateSecret(bool $force = false): self |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Make sure the webhook is valid and perform the requested webhook operation. |
||
| 186 | * |
||
| 187 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 188 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 189 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidWebhookException |
||
| 190 | */ |
||
| 191 | public function validateAndSetWebhook(): self |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Save the test output and echo it if we're not in a test. |
||
| 230 | * |
||
| 231 | * @param string $output |
||
| 232 | * |
||
| 233 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 234 | */ |
||
| 235 | private function handleOutput(string $output): self |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Set any extra bot features that have been assigned on construction. |
||
| 248 | * |
||
| 249 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 250 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 251 | */ |
||
| 252 | public function setBotExtras(): self |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set extra bot parameters for Telegram object. |
||
| 262 | * |
||
| 263 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 264 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 265 | */ |
||
| 266 | protected function setBotExtrasTelegram(): self |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set extra bot parameters for Request class. |
||
| 301 | * |
||
| 302 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 303 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 304 | */ |
||
| 305 | protected function setBotExtrasRequest(): self |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
| 330 | * |
||
| 331 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 332 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
| 333 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 334 | */ |
||
| 335 | public function handleRequest(): self |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Handle cron. |
||
| 352 | * |
||
| 353 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 354 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 355 | */ |
||
| 356 | public function handleCron(): self |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get the number of seconds the script should loop. |
||
| 371 | * |
||
| 372 | * @return int |
||
| 373 | */ |
||
| 374 | View Code Duplication | public function getLoopTime(): int |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Get the number of seconds the script should wait after each getUpdates request. |
||
| 391 | * |
||
| 392 | * @return int |
||
| 393 | */ |
||
| 394 | View Code Duplication | public function getLoopInterval(): int |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Loop the getUpdates method for the passed amount of seconds. |
||
| 408 | * |
||
| 409 | * @param int $loop_time_in_seconds |
||
| 410 | * @param int $loop_interval_in_seconds |
||
| 411 | * |
||
| 412 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 413 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 414 | */ |
||
| 415 | public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interval_in_seconds = 2): self |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Handle the updates using the getUpdates method. |
||
| 434 | * |
||
| 435 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 436 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 437 | */ |
||
| 438 | public function handleGetUpdates(): self |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Handle the updates using the Webhook method. |
||
| 481 | * |
||
| 482 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
| 483 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 484 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
| 485 | */ |
||
| 486 | public function handleWebhook(): self |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Return the current test output and clear it. |
||
| 495 | * |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | public function getOutput(): string |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Check if this is a valid request coming from a Telegram API IP address. |
||
| 508 | * |
||
| 509 | * @link https://core.telegram.org/bots/webhooks#the-short-version |
||
| 510 | * |
||
| 511 | * @return bool |
||
| 512 | */ |
||
| 513 | public function isValidRequest(): bool |
||
| 533 | } |
||
| 534 |