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 |
||
| 11 | class Slackbot extends AbstractBot |
||
| 12 | { |
||
| 13 | private $commands; |
||
| 14 | private $lastError; |
||
| 15 | private $currentCommand; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Botonomous constructor. |
||
| 19 | * |
||
| 20 | * @param Config|null $config |
||
| 21 | * |
||
| 22 | * @throws \Exception |
||
| 23 | */ |
||
| 24 | 42 | public function __construct(Config $config = null) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Set the timezone. |
||
| 35 | */ |
||
| 36 | 42 | private function setTimezone() |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @param null|string $key |
||
| 44 | * |
||
| 45 | * @return mixed |
||
| 46 | */ |
||
| 47 | 10 | public function getRequest($key = null) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @return AbstractBaseSlack|null|void |
||
| 54 | */ |
||
| 55 | 1 | private function handleMessageActions() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @throws \Exception |
||
| 73 | */ |
||
| 74 | 5 | private function handleSendResponse() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | 3 | private function checkAccessControl() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @throws \Exception |
||
| 145 | */ |
||
| 146 | 8 | public function run() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * handle OAuth. |
||
| 162 | */ |
||
| 163 | 1 | private function handleOAuth() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @throws \Exception |
||
| 170 | */ |
||
| 171 | 1 | private function handleUrlVerification() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Pre-process the request. |
||
| 184 | */ |
||
| 185 | 3 | private function preProcessRequest() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param null $message |
||
| 202 | * |
||
| 203 | * @throws \Exception |
||
| 204 | * |
||
| 205 | * @return mixed |
||
| 206 | */ |
||
| 207 | 4 | public function respond($message = null) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Get plugin class by command. |
||
| 234 | * |
||
| 235 | * @param Command $command |
||
| 236 | * |
||
| 237 | * @throws \Exception |
||
| 238 | * |
||
| 239 | * @return AbstractPlugin |
||
| 240 | */ |
||
| 241 | 3 | private function getPluginClassByCommand(Command $command) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param null $message |
||
| 258 | * |
||
| 259 | * @throws \Exception |
||
| 260 | * |
||
| 261 | * @return bool|Command |
||
| 262 | */ |
||
| 263 | 7 | public function getCommandByMessage($message = null) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | 2 | public function getCommands() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param array $commands |
||
| 336 | */ |
||
| 337 | 2 | public function setCommands(array $commands) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | 4 | public function getLastError() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $lastError |
||
| 352 | */ |
||
| 353 | 4 | public function setLastError($lastError) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Return the current command. |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | 1 | public function getCurrentCommand() |
|
| 367 | |||
| 368 | /** |
||
| 369 | * @param string $currentCommand |
||
| 370 | */ |
||
| 371 | 2 | public function setCurrentCommand($currentCommand) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Determine if bot user id is mentioned in the message. |
||
| 378 | * |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | 1 | public function youTalkingToMe() |
|
| 397 | } |
||
| 398 |