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 |
||
149 | |||
150 | /** |
||
151 | * Initialise all loggers. |
||
152 | * |
||
153 | * @param array $log_paths |
||
154 | * |
||
155 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
156 | * @throws \Exception |
||
157 | */ |
||
158 | public function initLogging(array $log_paths): self |
||
168 | |||
169 | /** |
||
170 | * Make sure the passed secret is valid. |
||
171 | * |
||
172 | * @param bool $force Force validation, even on CLI. |
||
173 | * |
||
174 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
175 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
176 | */ |
||
177 | public function validateSecret(bool $force = false): self |
||
190 | |||
191 | /** |
||
192 | * Make sure the webhook is valid and perform the requested webhook operation. |
||
193 | * |
||
194 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
195 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
196 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidWebhookException |
||
197 | */ |
||
198 | public function validateAndSetWebhook(): self |
||
234 | |||
235 | /** |
||
236 | * Save the test output and echo it if we're not in a test. |
||
237 | * |
||
238 | * @param string $output |
||
239 | * |
||
240 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
241 | */ |
||
242 | private function handleOutput(string $output): self |
||
252 | |||
253 | /** |
||
254 | * Set any extra bot features that have been assigned on construction. |
||
255 | * |
||
256 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
257 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
258 | */ |
||
259 | public function setBotExtras(): self |
||
266 | |||
267 | /** |
||
268 | * Set extra bot parameters for Telegram object. |
||
269 | * |
||
270 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
271 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
272 | */ |
||
273 | protected function setBotExtrasTelegram(): self |
||
305 | |||
306 | /** |
||
307 | * Set extra bot parameters for Request class. |
||
308 | * |
||
309 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
310 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
311 | */ |
||
312 | protected function setBotExtrasRequest(): self |
||
334 | |||
335 | /** |
||
336 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
337 | * |
||
338 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
339 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
340 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
341 | */ |
||
342 | public function handleRequest(): self |
||
354 | |||
355 | /** |
||
356 | * Handle cron. |
||
357 | * |
||
358 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
359 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
360 | */ |
||
361 | public function handleCron(): self |
||
373 | |||
374 | /** |
||
375 | * Get the number of seconds the script should loop. |
||
376 | * |
||
377 | * @return int |
||
378 | */ |
||
379 | View Code Duplication | public function getLoopTime(): int |
|
393 | |||
394 | /** |
||
395 | * Get the number of seconds the script should wait after each getUpdates request. |
||
396 | * |
||
397 | * @return int |
||
398 | */ |
||
399 | View Code Duplication | public function getLoopInterval(): int |
|
410 | |||
411 | /** |
||
412 | * Loop the getUpdates method for the passed amount of seconds. |
||
413 | * |
||
414 | * @param int $loop_time_in_seconds |
||
415 | * @param int $loop_interval_in_seconds |
||
416 | * |
||
417 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
418 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
419 | */ |
||
420 | public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interval_in_seconds = 2): self |
||
436 | |||
437 | /** |
||
438 | * Handle the updates using the getUpdates method. |
||
439 | * |
||
440 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
441 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
442 | */ |
||
443 | public function handleGetUpdates(): self |
||
483 | |||
484 | /** |
||
485 | * Handle the updates using the Webhook method. |
||
486 | * |
||
487 | * @return \TelegramBot\TelegramBotManager\BotManager |
||
488 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
489 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
490 | */ |
||
491 | public function handleWebhook(): self |
||
497 | |||
498 | /** |
||
499 | * Return the current test output and clear it. |
||
500 | * |
||
501 | * @return string |
||
502 | */ |
||
503 | public function getOutput(): string |
||
510 | |||
511 | /** |
||
512 | * Check if this is a valid request coming from a Telegram API IP address. |
||
513 | * |
||
514 | * @link https://core.telegram.org/bots/webhooks#the-short-version |
||
515 | * |
||
516 | * @return bool |
||
517 | */ |
||
518 | public function isValidRequest(): bool |
||
538 | |||
539 | /** |
||
540 | * Make sure this is a valid request. |
||
541 | * |
||
542 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException |
||
543 | */ |
||
544 | private function validateRequest() |
||
550 | } |
||
551 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.