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 |
||
175 | |||
176 | /** |
||
177 | * Make sure the passed secret is valid. |
||
178 | * |
||
179 | * @param bool $force Force validation, even on CLI. |
||
180 | * |
||
181 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
182 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
183 | */ |
||
184 | public function validateSecret(bool $force = false): self |
||
197 | |||
198 | /** |
||
199 | * Make sure the webhook is valid and perform the requested webhook operation. |
||
200 | * |
||
201 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
202 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
203 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidWebhookException |
||
204 | */ |
||
205 | public function validateAndSetWebhook(): self |
||
241 | |||
242 | /** |
||
243 | * Save the test output and echo it if we're not in a test. |
||
244 | * |
||
245 | * @param string $output |
||
246 | * |
||
247 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
248 | */ |
||
249 | private function handleOutput(string $output): self |
||
259 | |||
260 | /** |
||
261 | * Set any extra bot features that have been assigned on construction. |
||
262 | * |
||
263 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
264 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
265 | */ |
||
266 | public function setBotExtras(): self |
||
273 | |||
274 | /** |
||
275 | * Set extra bot parameters for Telegram object. |
||
276 | * |
||
277 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
278 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
279 | */ |
||
280 | protected function setBotExtrasTelegram(): self |
||
320 | |||
321 | /** |
||
322 | * Set extra bot parameters for Request class. |
||
323 | * |
||
324 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
325 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
326 | */ |
||
327 | protected function setBotExtrasRequest(): self |
||
349 | |||
350 | /** |
||
351 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
352 | * |
||
353 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
354 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
355 | */ |
||
356 | public function handleRequest(): self |
||
368 | |||
369 | /** |
||
370 | * Handle cron. |
||
371 | * |
||
372 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
373 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
374 | */ |
||
375 | public function handleCron(): self |
||
387 | |||
388 | /** |
||
389 | * Get the number of seconds the script should loop. |
||
390 | * |
||
391 | * @return int |
||
392 | */ |
||
393 | public function getLoopTime(): int |
||
407 | |||
408 | /** |
||
409 | * Get the number of seconds the script should wait after each getUpdates request. |
||
410 | * |
||
411 | * @return int |
||
412 | */ |
||
413 | public function getLoopInterval(): int |
||
424 | |||
425 | /** |
||
426 | * Loop the getUpdates method for the passed amount of seconds. |
||
427 | * |
||
428 | * @param int $loop_time_in_seconds |
||
429 | * @param int $loop_interval_in_seconds |
||
430 | * |
||
431 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
432 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
433 | */ |
||
434 | public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interval_in_seconds = 2): self |
||
450 | |||
451 | /** |
||
452 | * Set a custom callback for handling the output of the getUpdates results. |
||
453 | * |
||
454 | * @param callable $callback |
||
455 | * |
||
456 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
457 | */ |
||
458 | public function setCustomGetUpdatesCallback(callable $callback): BotManager |
||
463 | |||
464 | /** |
||
465 | * Handle the updates using the getUpdates method. |
||
466 | * |
||
467 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
468 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
469 | */ |
||
470 | public function handleGetUpdates(): self |
||
483 | |||
484 | /** |
||
485 | * Return the default output for getUpdates handling. |
||
486 | * |
||
487 | * @param Entities\ServerResponse $get_updates_response |
||
488 | * |
||
489 | * @return string |
||
490 | */ |
||
491 | protected function defaultGetUpdatesCallback($get_updates_response): string |
||
534 | |||
535 | /** |
||
536 | * Handle the updates using the Webhook method. |
||
537 | * |
||
538 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
539 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
540 | */ |
||
541 | public function handleWebhook(): self |
||
547 | |||
548 | /** |
||
549 | * Return the current test output and clear it. |
||
550 | * |
||
551 | * @return string |
||
552 | */ |
||
553 | public function getOutput(): string |
||
560 | |||
561 | /** |
||
562 | * Check if this is a valid request coming from a Telegram API IP address. |
||
563 | * |
||
564 | * @link https://core.telegram.org/bots/webhooks#the-short-version |
||
565 | * |
||
566 | * @return bool |
||
567 | */ |
||
568 | public function isValidRequest(): bool |
||
588 | |||
589 | /** |
||
590 | * Make sure this is a valid request. |
||
591 | * |
||
592 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
593 | */ |
||
594 | private function validateRequest() |
||
600 | } |
||
601 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.