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() |
||
226 | |||
227 | /** |
||
228 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
229 | * |
||
230 | * @throws \InvalidArgumentException |
||
231 | */ |
||
232 | public function handleRequest() |
||
246 | |||
247 | /** |
||
248 | * Get the number of seconds the script should loop. |
||
249 | * |
||
250 | * @return int |
||
251 | */ |
||
252 | public function getLoopTime() |
||
266 | |||
267 | /** |
||
268 | * Loop the getUpdates method for the passed amount of seconds. |
||
269 | * |
||
270 | * @param int $loop_time_in_seconds |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function handleGetUpdatesLoop($loop_time_in_seconds) |
||
290 | |||
291 | /** |
||
292 | * Handle the updates using the getUpdates method. |
||
293 | */ |
||
294 | public function handleGetUpdates() |
||
332 | |||
333 | /** |
||
334 | * Handle the updates using the Webhook method. |
||
335 | * |
||
336 | * @throws \InvalidArgumentException |
||
337 | */ |
||
338 | public function handleWebhook() |
||
344 | |||
345 | /** |
||
346 | * Return the current test output and clear it. |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | public function getOutput() |
||
356 | } |
||
357 |