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 array Telegram webhook servers IP ranges |
||
25 | * @link https://core.telegram.org/bots/webhooks#the-short-version |
||
26 | */ |
||
27 | public const TELEGRAM_IP_RANGES = ['149.154.160.0/20', '91.108.4.0/22']; |
||
28 | |||
29 | /** |
||
30 | * @var string The output for testing, instead of echoing |
||
31 | */ |
||
32 | private $output = ''; |
||
33 | |||
34 | /** |
||
35 | * @var \Longman\TelegramBot\Telegram |
||
36 | */ |
||
37 | private $telegram; |
||
38 | |||
39 | /** |
||
40 | * @var \TelegramBot\TelegramBotManager\Params Object that manages the parameters. |
||
41 | */ |
||
42 | private $params; |
||
43 | |||
44 | /** |
||
45 | * @var \TelegramBot\TelegramBotManager\Action Object that contains the current action. |
||
46 | */ |
||
47 | private $action; |
||
48 | |||
49 | /** |
||
50 | * @var callable |
||
51 | */ |
||
52 | private $custom_get_updates_callback; |
||
53 | |||
54 | /** |
||
55 | * BotManager constructor. |
||
56 | * |
||
57 | * @param array $params |
||
58 | * |
||
59 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException |
||
60 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidActionException |
||
61 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
62 | * @throws \Exception |
||
63 | */ |
||
64 | public function __construct(array $params) |
||
78 | |||
79 | /** |
||
80 | * Check if we're busy running the PHPUnit tests. |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | public static function inTest(): bool |
||
88 | |||
89 | /** |
||
90 | * Return the Telegram object. |
||
91 | * |
||
92 | * @return \Longman\TelegramBot\Telegram |
||
93 | */ |
||
94 | public function getTelegram(): Telegram |
||
98 | |||
99 | /** |
||
100 | * Get the Params object. |
||
101 | * |
||
102 | * @return \TelegramBot\TelegramBotManager\Params |
||
103 | */ |
||
104 | public function getParams(): Params |
||
108 | |||
109 | /** |
||
110 | * Get the Action object. |
||
111 | * |
||
112 | * @return \TelegramBot\TelegramBotManager\Action |
||
113 | */ |
||
114 | public function getAction(): Action |
||
118 | |||
119 | /** |
||
120 | * Run this thing in all its glory! |
||
121 | * |
||
122 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
123 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
124 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
125 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidWebhookException |
||
126 | * @throws \Exception |
||
127 | */ |
||
128 | public function run(): self |
||
154 | |||
155 | /** |
||
156 | * Initialise all loggers. |
||
157 | * |
||
158 | * @param array $log_paths |
||
159 | * |
||
160 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
161 | * @throws \Exception |
||
162 | */ |
||
163 | public function initLogging(array $log_paths): self |
||
173 | |||
174 | /** |
||
175 | * Make sure the passed secret is valid. |
||
176 | * |
||
177 | * @param bool $force Force validation, even on CLI. |
||
178 | * |
||
179 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
180 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
181 | */ |
||
182 | public function validateSecret(bool $force = false): self |
||
195 | |||
196 | /** |
||
197 | * Make sure the webhook is valid and perform the requested webhook operation. |
||
198 | * |
||
199 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
200 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
201 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidWebhookException |
||
202 | */ |
||
203 | public function validateAndSetWebhook(): self |
||
239 | |||
240 | /** |
||
241 | * Save the test output and echo it if we're not in a test. |
||
242 | * |
||
243 | * @param string $output |
||
244 | * |
||
245 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
246 | */ |
||
247 | private function handleOutput(string $output): self |
||
257 | |||
258 | /** |
||
259 | * Set any extra bot features that have been assigned on construction. |
||
260 | * |
||
261 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
262 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
263 | */ |
||
264 | public function setBotExtras(): self |
||
271 | |||
272 | /** |
||
273 | * Set extra bot parameters for Telegram object. |
||
274 | * |
||
275 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
276 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
277 | */ |
||
278 | protected function setBotExtrasTelegram(): self |
||
312 | |||
313 | /** |
||
314 | * Set extra bot parameters for Request class. |
||
315 | * |
||
316 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
317 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
318 | */ |
||
319 | protected function setBotExtrasRequest(): self |
||
341 | |||
342 | /** |
||
343 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
344 | * |
||
345 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
346 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
347 | */ |
||
348 | public function handleRequest(): self |
||
360 | |||
361 | /** |
||
362 | * Handle cron. |
||
363 | * |
||
364 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
365 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
366 | */ |
||
367 | public function handleCron(): self |
||
379 | |||
380 | /** |
||
381 | * Get the number of seconds the script should loop. |
||
382 | * |
||
383 | * @return int |
||
384 | */ |
||
385 | public function getLoopTime(): int |
||
399 | |||
400 | /** |
||
401 | * Get the number of seconds the script should wait after each getUpdates request. |
||
402 | * |
||
403 | * @return int |
||
404 | */ |
||
405 | public function getLoopInterval(): int |
||
416 | |||
417 | /** |
||
418 | * Loop the getUpdates method for the passed amount of seconds. |
||
419 | * |
||
420 | * @param int $loop_time_in_seconds |
||
421 | * @param int $loop_interval_in_seconds |
||
422 | * |
||
423 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
424 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
425 | */ |
||
426 | public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interval_in_seconds = 2): self |
||
442 | |||
443 | /** |
||
444 | * Set a custom callback for handling the output of the getUpdates results. |
||
445 | * |
||
446 | * @param callable $callback |
||
447 | * |
||
448 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
449 | */ |
||
450 | public function setCustomGetUpdatesCallback(callable $callback): BotManager |
||
455 | |||
456 | /** |
||
457 | * Handle the updates using the getUpdates method. |
||
458 | * |
||
459 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
460 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
461 | */ |
||
462 | public function handleGetUpdates(): self |
||
475 | |||
476 | /** |
||
477 | * Return the default output for getUpdates handling. |
||
478 | * |
||
479 | * @param Entities\ServerResponse $get_updates_response |
||
480 | * |
||
481 | * @return string |
||
482 | */ |
||
483 | protected function defaultGetUpdatesCallback($get_updates_response): string |
||
526 | |||
527 | /** |
||
528 | * Handle the updates using the Webhook method. |
||
529 | * |
||
530 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
531 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
532 | */ |
||
533 | public function handleWebhook(): self |
||
539 | |||
540 | /** |
||
541 | * Return the current test output and clear it. |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | public function getOutput(): string |
||
552 | |||
553 | /** |
||
554 | * Check if this is a valid request coming from a Telegram API IP address. |
||
555 | * |
||
556 | * @link https://core.telegram.org/bots/webhooks#the-short-version |
||
557 | * |
||
558 | * @return bool |
||
559 | */ |
||
560 | public function isValidRequest(): bool |
||
580 | |||
581 | /** |
||
582 | * Make sure this is a valid request. |
||
583 | * |
||
584 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
585 | */ |
||
586 | private function validateRequest() |
||
592 | } |
||
593 |