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 Array of logger files to set |
||
| 68 | */ |
||
| 69 | public $logging; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array List of admins to enable |
||
| 73 | */ |
||
| 74 | public $admins; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array MySQL credentials to use |
||
| 78 | */ |
||
| 79 | public $mysql; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string Custom download path to set |
||
| 83 | */ |
||
| 84 | public $download_path; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string Custom upload path to set |
||
| 88 | */ |
||
| 89 | public $upload_path; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array Custom commands paths to set |
||
| 93 | */ |
||
| 94 | public $commands_paths; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array List of custom command configs |
||
| 98 | */ |
||
| 99 | public $command_configs; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var string Botan token to enable botan.io support |
||
| 103 | */ |
||
| 104 | public $botan_token; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var string Custom raw JSON string to use as input |
||
| 108 | */ |
||
| 109 | public $custom_input; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var array List of valid actions that can be called |
||
| 113 | */ |
||
| 114 | private static $valid_actions = [ |
||
| 115 | 'set', |
||
| 116 | 'unset', |
||
| 117 | 'reset', |
||
| 118 | 'handle' |
||
| 119 | ]; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var array List of valid extra parameters that can be passed |
||
| 123 | */ |
||
| 124 | private static $valid_params = [ |
||
| 125 | 'api_key', |
||
| 126 | 'botname', |
||
| 127 | 'secret', |
||
| 128 | 'webhook', |
||
| 129 | 'selfcrt', |
||
| 130 | 'logging', |
||
| 131 | 'admins', |
||
| 132 | 'mysql', |
||
| 133 | 'download_path', |
||
| 134 | 'upload_path', |
||
| 135 | 'commands_paths', |
||
| 136 | 'command_configs', |
||
| 137 | 'botan_token', |
||
| 138 | 'custom_input' |
||
| 139 | ]; |
||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * BotManager constructor that assigns all necessary member variables. |
||
| 144 | * |
||
| 145 | * @param array $vars |
||
| 146 | * |
||
| 147 | * @throws \Exception |
||
| 148 | */ |
||
| 149 | public function __construct(array $vars) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Run this thing in all its glory! |
||
| 163 | * |
||
| 164 | * @throws \Exception |
||
| 165 | */ |
||
| 166 | public function run() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Check if this script is being called from CLI. |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | public function isCli() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Allow this script to be called via CLI. |
||
| 206 | * |
||
| 207 | * $ php entry.php s=<secret> a=<action> l=<loop> |
||
| 208 | */ |
||
| 209 | public function makeCliFriendly() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Initialise all loggers. |
||
| 227 | */ |
||
| 228 | public function initLogging() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Make sure the passed secret is valid. |
||
| 243 | * |
||
| 244 | * @param bool $force Force validation, even on CLI. |
||
| 245 | * |
||
| 246 | * @return $this |
||
| 247 | * @throws \Exception |
||
| 248 | */ |
||
| 249 | public function validateSecret($force = false) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Make sure the action is valid and set the member variable. |
||
| 264 | * |
||
| 265 | * @throws \Exception |
||
| 266 | */ |
||
| 267 | public function validateAndSetAction() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Make sure the webhook is valid and perform the requested webhook operation. |
||
| 281 | * |
||
| 282 | * @throws \Exception |
||
| 283 | */ |
||
| 284 | public function validateAndSetWebhook() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Check if the current action is one of the passed ones. |
||
| 307 | * |
||
| 308 | * @param string|array $actions |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | public function isAction($actions) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get the param of how long (in seconds) the script should loop. |
||
| 322 | * |
||
| 323 | * @return int |
||
| 324 | */ |
||
| 325 | public function getLoopTime() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
| 341 | * |
||
| 342 | * @throws \Exception |
||
| 343 | */ |
||
| 344 | public function handleRequest() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Loop the getUpdates method for the passed amount of seconds. |
||
| 361 | * |
||
| 362 | * @param int $loop_time_in_seconds |
||
| 363 | * |
||
| 364 | * @return $this |
||
| 365 | */ |
||
| 366 | public function handleGetUpdatesLoop($loop_time_in_seconds) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Handle the updates using the getUpdates method. |
||
| 385 | */ |
||
| 386 | public function handleGetUpdates() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Handle the updates using the Webhook method. |
||
| 425 | * |
||
| 426 | * @throws \Exception |
||
| 427 | */ |
||
| 428 | public function handleWebhook() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Set any extra bot features that have been assigned on construction. |
||
| 437 | */ |
||
| 438 | public function setBotExtras() |
||
| 456 | } |
||
| 457 |
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: