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 |
||
146 | |||
147 | /** |
||
148 | * Initialise all loggers. |
||
149 | * |
||
150 | * @param array $log_paths |
||
151 | * |
||
152 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
153 | * @throws \Exception |
||
154 | */ |
||
155 | public function initLogging(array $log_paths): self |
||
165 | |||
166 | /** |
||
167 | * Make sure the passed secret is valid. |
||
168 | * |
||
169 | * @param bool $force Force validation, even on CLI. |
||
170 | * |
||
171 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
172 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
173 | */ |
||
174 | public function validateSecret(bool $force = false): self |
||
187 | |||
188 | /** |
||
189 | * Make sure the webhook is valid and perform the requested webhook operation. |
||
190 | * |
||
191 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
192 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
193 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidWebhookException |
||
194 | */ |
||
195 | public function validateAndSetWebhook(): self |
||
231 | |||
232 | /** |
||
233 | * Save the test output and echo it if we're not in a test. |
||
234 | * |
||
235 | * @param string $output |
||
236 | * |
||
237 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
238 | */ |
||
239 | private function handleOutput(string $output): self |
||
249 | |||
250 | /** |
||
251 | * Set any extra bot features that have been assigned on construction. |
||
252 | * |
||
253 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
254 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
255 | */ |
||
256 | public function setBotExtras(): self |
||
263 | |||
264 | /** |
||
265 | * Set extra bot parameters for Telegram object. |
||
266 | * |
||
267 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
268 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
269 | */ |
||
270 | protected function setBotExtrasTelegram(): self |
||
302 | |||
303 | /** |
||
304 | * Set extra bot parameters for Request class. |
||
305 | * |
||
306 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
307 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
308 | */ |
||
309 | protected function setBotExtrasRequest(): self |
||
331 | |||
332 | /** |
||
333 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
334 | * |
||
335 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
336 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
337 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
338 | */ |
||
339 | public function handleRequest(): self |
||
351 | |||
352 | /** |
||
353 | * Handle cron. |
||
354 | * |
||
355 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
356 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
357 | */ |
||
358 | public function handleCron(): self |
||
370 | |||
371 | /** |
||
372 | * Get the number of seconds the script should loop. |
||
373 | * |
||
374 | * @return int |
||
375 | */ |
||
376 | View Code Duplication | public function getLoopTime(): int |
|
390 | |||
391 | /** |
||
392 | * Get the number of seconds the script should wait after each getUpdates request. |
||
393 | * |
||
394 | * @return int |
||
395 | */ |
||
396 | View Code Duplication | public function getLoopInterval(): int |
|
407 | |||
408 | /** |
||
409 | * Loop the getUpdates method for the passed amount of seconds. |
||
410 | * |
||
411 | * @param int $loop_time_in_seconds |
||
412 | * @param int $loop_interval_in_seconds |
||
413 | * |
||
414 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
415 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
416 | */ |
||
417 | public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interval_in_seconds = 2): self |
||
433 | |||
434 | /** |
||
435 | * Handle the updates using the getUpdates method. |
||
436 | * |
||
437 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
438 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
439 | */ |
||
440 | public function handleGetUpdates(): self |
||
480 | |||
481 | /** |
||
482 | * Handle the updates using the Webhook method. |
||
483 | * |
||
484 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
485 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
486 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
487 | */ |
||
488 | public function handleWebhook(): self |
||
494 | |||
495 | /** |
||
496 | * Return the current test output and clear it. |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | public function getOutput(): string |
||
507 | |||
508 | /** |
||
509 | * Check if this is a valid request coming from a Telegram API IP address. |
||
510 | * |
||
511 | * @link https://core.telegram.org/bots/webhooks#the-short-version |
||
512 | * |
||
513 | * @return bool |
||
514 | */ |
||
515 | public function isValidRequest(): bool |
||
535 | |||
536 | /** |
||
537 | * Make sure this is a valid request. |
||
538 | * |
||
539 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
540 | */ |
||
541 | private function validateRequest() |
||
547 | } |
||
548 |