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 |
||
221 | |||
222 | /** |
||
223 | * Save the test output and echo it if we're not in a test. |
||
224 | * |
||
225 | * @param string $output |
||
226 | * |
||
227 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
228 | */ |
||
229 | private function handleOutput(string $output): self |
||
239 | |||
240 | /** |
||
241 | * Set any extra bot features that have been assigned on construction. |
||
242 | * |
||
243 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
244 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
245 | */ |
||
246 | public function setBotExtras(): self |
||
253 | |||
254 | /** |
||
255 | * Set extra bot parameters for Telegram object. |
||
256 | * |
||
257 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
258 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
259 | */ |
||
260 | protected function setBotExtrasTelegram(): self |
||
292 | |||
293 | /** |
||
294 | * Set extra bot parameters for Request class. |
||
295 | * |
||
296 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
297 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
298 | */ |
||
299 | protected function setBotExtrasRequest(): self |
||
321 | |||
322 | /** |
||
323 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
324 | * |
||
325 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
326 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
327 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
328 | */ |
||
329 | public function handleRequest(): self |
||
343 | |||
344 | /** |
||
345 | * Handle cron. |
||
346 | * |
||
347 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
348 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
349 | */ |
||
350 | public function handleCron(): self |
||
362 | |||
363 | /** |
||
364 | * Get the number of seconds the script should loop. |
||
365 | * |
||
366 | * @return int |
||
367 | */ |
||
368 | View Code Duplication | public function getLoopTime(): int |
|
382 | |||
383 | /** |
||
384 | * Get the number of seconds the script should wait after each getUpdates request. |
||
385 | * |
||
386 | * @return int |
||
387 | */ |
||
388 | View Code Duplication | public function getLoopInterval(): int |
|
399 | |||
400 | /** |
||
401 | * Loop the getUpdates method for the passed amount of seconds. |
||
402 | * |
||
403 | * @param int $loop_time_in_seconds |
||
404 | * @param int $loop_interval_in_seconds |
||
405 | * |
||
406 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
407 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
408 | */ |
||
409 | public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interval_in_seconds = 2): self |
||
425 | |||
426 | /** |
||
427 | * Handle the updates using the getUpdates method. |
||
428 | * |
||
429 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
430 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
431 | */ |
||
432 | public function handleGetUpdates(): self |
||
472 | |||
473 | /** |
||
474 | * Handle the updates using the Webhook method. |
||
475 | * |
||
476 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
477 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
478 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
479 | */ |
||
480 | public function handleWebhook(): self |
||
486 | |||
487 | /** |
||
488 | * Return the current test output and clear it. |
||
489 | * |
||
490 | * @return string |
||
491 | */ |
||
492 | public function getOutput(): string |
||
499 | |||
500 | /** |
||
501 | * Check if this is a valid request coming from a Telegram API IP address. |
||
502 | * |
||
503 | * @link https://core.telegram.org/bots/webhooks#the-short-version |
||
504 | * |
||
505 | * @return bool |
||
506 | */ |
||
507 | public function isValidRequest(): bool |
||
534 | } |
||
535 |