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 |
||
24 | class BotManager |
||
25 | { |
||
26 | /** |
||
27 | * @var string The output for testing, instead of echoing |
||
28 | */ |
||
29 | private $output; |
||
30 | |||
31 | /** |
||
32 | * @var \Longman\TelegramBot\Telegram |
||
33 | */ |
||
34 | private $telegram; |
||
35 | |||
36 | /** |
||
37 | * @var \NPM\TelegramBotManager\Params Object that manages the parameters. |
||
38 | */ |
||
39 | private $params; |
||
40 | |||
41 | /** |
||
42 | * @var \NPM\TelegramBotManager\Action Object that contains the current action. |
||
43 | */ |
||
44 | private $action; |
||
45 | |||
46 | /** |
||
47 | * BotManager constructor. |
||
48 | * |
||
49 | * @param array $params |
||
50 | * |
||
51 | * @throws \InvalidArgumentException |
||
52 | */ |
||
53 | public function __construct(array $params) |
||
58 | |||
59 | /** |
||
60 | * Return the Telegram object. |
||
61 | * |
||
62 | * @return \Longman\TelegramBot\Telegram |
||
63 | */ |
||
64 | public function getTelegram() |
||
68 | |||
69 | /** |
||
70 | * Get the Params object. |
||
71 | * |
||
72 | * @return \NPM\TelegramBotManager\Params |
||
73 | */ |
||
74 | public function getParams() |
||
78 | |||
79 | /** |
||
80 | * Get the Action object. |
||
81 | * |
||
82 | * @return \NPM\TelegramBotManager\Action |
||
83 | */ |
||
84 | public function getAction() |
||
88 | |||
89 | /** |
||
90 | * Run this thing in all its glory! |
||
91 | * |
||
92 | * @throws \InvalidArgumentException |
||
93 | */ |
||
94 | public function run() |
||
118 | |||
119 | /** |
||
120 | * Initialise all loggers. |
||
121 | */ |
||
122 | public function initLogging() |
||
136 | |||
137 | /** |
||
138 | * Make sure the passed secret is valid. |
||
139 | * |
||
140 | * @param bool $force Force validation, even on CLI. |
||
141 | * |
||
142 | * @return $this |
||
143 | * @throws \InvalidArgumentException |
||
144 | */ |
||
145 | public function validateSecret($force = false) |
||
158 | |||
159 | /** |
||
160 | * Make sure the webhook is valid and perform the requested webhook operation. |
||
161 | * |
||
162 | * @throws \InvalidArgumentException |
||
163 | */ |
||
164 | public function validateAndSetWebhook() |
||
186 | |||
187 | /** |
||
188 | * Save the test output and echo it if we're not in a test. |
||
189 | * |
||
190 | * @param string $output |
||
191 | */ |
||
192 | private function handleOutput($output) |
||
200 | |||
201 | /** |
||
202 | * Set any extra bot features that have been assigned on construction. |
||
203 | * |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function setBotExtras() |
||
235 | |||
236 | /** |
||
237 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
238 | * |
||
239 | * @throws \InvalidArgumentException |
||
240 | */ |
||
241 | public function handleRequest() |
||
255 | |||
256 | /** |
||
257 | * Get the number of seconds the script should loop. |
||
258 | * |
||
259 | * @return int |
||
260 | */ |
||
261 | public function getLoopTime() |
||
275 | |||
276 | /** |
||
277 | * Loop the getUpdates method for the passed amount of seconds. |
||
278 | * |
||
279 | * @param int $loop_time_in_seconds |
||
280 | * |
||
281 | * @return $this |
||
282 | */ |
||
283 | public function handleGetUpdatesLoop($loop_time_in_seconds) |
||
299 | |||
300 | /** |
||
301 | * Handle the updates using the getUpdates method. |
||
302 | */ |
||
303 | public function handleGetUpdates() |
||
341 | |||
342 | /** |
||
343 | * Handle the updates using the Webhook method. |
||
344 | * |
||
345 | * @throws \InvalidArgumentException |
||
346 | */ |
||
347 | public function handleWebhook() |
||
353 | |||
354 | /** |
||
355 | * Return the current test output and clear it. |
||
356 | * |
||
357 | * @return string |
||
358 | */ |
||
359 | public function getOutput() |
||
366 | } |
||
367 |