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