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 Params Object that manages the parameters. |
||
| 38 | */ |
||
| 39 | private $params; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var 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 | public function getParams() |
||
| 63 | |||
| 64 | public function getAction() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Run this thing in all its glory! |
||
| 71 | * |
||
| 72 | * @throws \InvalidArgumentException |
||
| 73 | */ |
||
| 74 | public function run() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Initialise all loggers. |
||
| 101 | */ |
||
| 102 | public function initLogging() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Make sure the passed secret is valid. |
||
| 119 | * |
||
| 120 | * @param bool $force Force validation, even on CLI. |
||
| 121 | * |
||
| 122 | * @return $this |
||
| 123 | * @throws \InvalidArgumentException |
||
| 124 | */ |
||
| 125 | public function validateSecret($force = false) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Make sure the webhook is valid and perform the requested webhook operation. |
||
| 141 | * |
||
| 142 | * @throws \InvalidArgumentException |
||
| 143 | */ |
||
| 144 | public function validateAndSetWebhook() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Set any extra bot features that have been assigned on construction. |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function setBotExtras() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
| 195 | * |
||
| 196 | * @throws \InvalidArgumentException |
||
| 197 | */ |
||
| 198 | public function handleRequest() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get the number of seconds the script should loop. |
||
| 215 | * |
||
| 216 | * @return int |
||
| 217 | */ |
||
| 218 | public function getLoopTime() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Loop the getUpdates method for the passed amount of seconds. |
||
| 235 | * |
||
| 236 | * @param int $loop_time_in_seconds |
||
| 237 | * |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | public function handleGetUpdatesLoop($loop_time_in_seconds) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Handle the updates using the getUpdates method. |
||
| 259 | */ |
||
| 260 | public function handleGetUpdates() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Handle the updates using the Webhook method. |
||
| 299 | * |
||
| 300 | * @throws \InvalidArgumentException |
||
| 301 | */ |
||
| 302 | public function handleWebhook() |
||
| 308 | } |
||
| 309 |