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 Telegram |
||
28 | */ |
||
29 | public $telegram; |
||
30 | |||
31 | /** |
||
32 | * @var string The output for testing, instead of echoing |
||
33 | */ |
||
34 | public $test_output; |
||
35 | |||
36 | /** |
||
37 | * @var string Telegram Bot API key |
||
38 | */ |
||
39 | protected $api_key = ''; |
||
40 | |||
41 | /** |
||
42 | * @var string Telegram Bot name |
||
43 | */ |
||
44 | public $botname; |
||
45 | |||
46 | /** |
||
47 | * @var string Secret string to validate calls |
||
48 | */ |
||
49 | public $secret; |
||
50 | |||
51 | /** |
||
52 | * @var string Action to be executed |
||
53 | */ |
||
54 | public $action = 'handle'; |
||
55 | |||
56 | /** |
||
57 | * @var string URI of the webhook |
||
58 | */ |
||
59 | public $webhook; |
||
60 | |||
61 | /** |
||
62 | * @var string Path to the self-signed certificate |
||
63 | */ |
||
64 | public $selfcrt; |
||
65 | |||
66 | /** |
||
67 | * @var array List of valid actions that can be called |
||
68 | */ |
||
69 | private static $valid_actions = [ |
||
70 | 'set', |
||
71 | 'unset', |
||
72 | 'reset', |
||
73 | 'handle' |
||
74 | ]; |
||
75 | |||
76 | /** |
||
77 | * @var array List of valid extra parameters that can be passed |
||
78 | */ |
||
79 | private static $valid_params = [ |
||
80 | 'api_key', |
||
81 | 'botname', |
||
82 | 'secret', |
||
83 | 'webhook', |
||
84 | 'selfcrt', |
||
85 | 'logging', |
||
86 | 'admins', |
||
87 | 'mysql', |
||
88 | 'download_path', |
||
89 | 'upload_path', |
||
90 | 'commands_paths', |
||
91 | 'command_configs', |
||
92 | 'botan_token', |
||
93 | 'custom_input' |
||
94 | ]; |
||
95 | |||
96 | |||
97 | /** |
||
98 | * BotManager constructor that assigns all necessary member variables. |
||
99 | * |
||
100 | * @param array $vars |
||
101 | * |
||
102 | * @throws \Exception |
||
103 | */ |
||
104 | public function __construct(array $vars) |
||
115 | |||
116 | /** |
||
117 | * Run this thing in all its glory! |
||
118 | * |
||
119 | * @throws \Exception |
||
120 | */ |
||
121 | public function run() |
||
148 | |||
149 | /** |
||
150 | * Check if this script is being called from CLI. |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function isCli() |
||
158 | |||
159 | /** |
||
160 | * Allow this script to be called via CLI. |
||
161 | * |
||
162 | * $ php entry.php s=<secret> a=<action> l=<loop> |
||
163 | */ |
||
164 | public function makeCliFriendly() |
||
179 | |||
180 | /** |
||
181 | * Initialise all loggers. |
||
182 | */ |
||
183 | public function initLogging() |
||
195 | |||
196 | /** |
||
197 | * Make sure the passed secret is valid. |
||
198 | * |
||
199 | * @param bool $force Force validation, even on CLI. |
||
200 | * |
||
201 | * @return $this |
||
202 | * @throws \Exception |
||
203 | */ |
||
204 | public function validateSecret($force = false) |
||
216 | |||
217 | /** |
||
218 | * Make sure the action is valid and set the member variable. |
||
219 | * |
||
220 | * @throws \Exception |
||
221 | */ |
||
222 | public function validateAndSetAction() |
||
233 | |||
234 | /** |
||
235 | * Make sure the webhook is valid and perform the requested webhook operation. |
||
236 | * |
||
237 | * @throws \Exception |
||
238 | */ |
||
239 | public function validateAndSetWebhook() |
||
259 | |||
260 | /** |
||
261 | * Check if the current action is one of the passed ones. |
||
262 | * |
||
263 | * @param $actions |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function isAction($actions) |
||
274 | |||
275 | /** |
||
276 | * Get the param of how long (in seconds) the script should loop. |
||
277 | * |
||
278 | * @return int |
||
279 | */ |
||
280 | public function getLoopTime() |
||
293 | |||
294 | /** |
||
295 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
296 | * |
||
297 | * @throws \Exception |
||
298 | */ |
||
299 | public function handleRequest() |
||
313 | |||
314 | /** |
||
315 | * Loop the getUpdates method for the passed amount of seconds. |
||
316 | * |
||
317 | * @param $loop_time_in_seconds int |
||
318 | * |
||
319 | * @return $this |
||
320 | */ |
||
321 | public function handleGetUpdatesLoop($loop_time_in_seconds) |
||
337 | |||
338 | /** |
||
339 | * Handle the updates using the getUpdates method. |
||
340 | */ |
||
341 | public function handleGetUpdates() |
||
377 | |||
378 | /** |
||
379 | * Handle the updates using the Webhook method. |
||
380 | * |
||
381 | * @throws \Exception |
||
382 | */ |
||
383 | public function handleWebhook() |
||
389 | |||
390 | /** |
||
391 | * Set any extra bot features that have been assigned on construction. |
||
392 | */ |
||
393 | public function setBotExtras() |
||
411 | } |
||
412 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: