Complex classes like Slackbot 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 Slackbot, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Slackbot extends AbstractBot |
||
| 13 | { |
||
| 14 | private $commands; |
||
| 15 | private $lastError; |
||
| 16 | private $currentCommand; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Botonomous constructor. |
||
| 20 | * |
||
| 21 | * @param Config|null $config |
||
| 22 | * |
||
| 23 | * @throws \Exception |
||
| 24 | */ |
||
| 25 | 43 | public function __construct(Config $config = null) |
|
| 33 | |||
| 34 | /** |
||
| 35 | * Set the timezone. |
||
| 36 | * |
||
| 37 | * @throws \Exception |
||
| 38 | */ |
||
| 39 | 43 | private function setTimezone() |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @param null|string $key |
||
| 47 | * |
||
| 48 | * @throws \Exception |
||
| 49 | * |
||
| 50 | * @return mixed |
||
| 51 | */ |
||
| 52 | 10 | public function getRequest($key = null) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @throws \Exception |
||
| 59 | * |
||
| 60 | * @return AbstractBaseSlack|null|void |
||
| 61 | */ |
||
| 62 | 1 | private function handleMessageActions() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @throws \Exception |
||
| 80 | */ |
||
| 81 | 5 | private function handleSendResponse() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @throws \Exception |
||
| 127 | * |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | 3 | private function checkAccessControl(): bool |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @throws \Exception |
||
| 156 | */ |
||
| 157 | 8 | public function run() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * handle OAuth. |
||
| 173 | * |
||
| 174 | * @throws \Exception |
||
| 175 | */ |
||
| 176 | 1 | private function handleOAuth() |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @throws \Exception |
||
| 183 | */ |
||
| 184 | 1 | private function handleUrlVerification() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Pre-process the request. |
||
| 197 | * |
||
| 198 | * @throws \Exception |
||
| 199 | */ |
||
| 200 | 3 | private function preProcessRequest() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param null $message |
||
| 217 | * |
||
| 218 | * @throws \Exception |
||
| 219 | * |
||
| 220 | * @return mixed |
||
| 221 | */ |
||
| 222 | 4 | public function respond($message = null) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Get plugin class by command. |
||
| 256 | * |
||
| 257 | * @param Command $command |
||
| 258 | * |
||
| 259 | * @throws \Exception |
||
| 260 | * |
||
| 261 | * @return AbstractPlugin |
||
| 262 | */ |
||
| 263 | 3 | private function getPluginClassByCommand(Command $command) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @throws \Exception |
||
| 281 | * |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | 2 | public function getCommands(): array |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @param array $commands |
||
| 295 | */ |
||
| 296 | 2 | public function setCommands(array $commands) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | 1 | public function getLastError(): string |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $lastError |
||
| 311 | */ |
||
| 312 | 1 | public function setLastError(string $lastError) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Return the current command. |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | 1 | public function getCurrentCommand(): string |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $currentCommand |
||
| 329 | */ |
||
| 330 | 2 | public function setCurrentCommand(string $currentCommand) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Determine if bot user id is mentioned in the message. |
||
| 337 | * |
||
| 338 | * @throws \Exception |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | 1 | public function youTalkingToMe(): bool |
|
| 358 | } |
||
| 359 |