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 | 43 | public function __construct(Config $config = null) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Set the timezone. |
||
| 35 | */ |
||
| 36 | 43 | 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) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Get plugin class by command. |
||
| 240 | * |
||
| 241 | * @param Command $command |
||
| 242 | * |
||
| 243 | * @throws \Exception |
||
| 244 | * |
||
| 245 | * @return AbstractPlugin |
||
| 246 | */ |
||
| 247 | 3 | private function getPluginClassByCommand(Command $command) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | 2 | public function getCommands() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @param array $commands |
||
| 276 | */ |
||
| 277 | 2 | public function setCommands(array $commands) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | 1 | public function getLastError() |
|
| 286 | { |
||
| 287 | 1 | return $this->lastError; |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $lastError |
||
| 292 | */ |
||
| 293 | 1 | public function setLastError($lastError) |
|
| 294 | { |
||
| 295 | 1 | $this->lastError = $lastError; |
|
| 296 | 1 | } |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Return the current command. |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | 1 | public function getCurrentCommand() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $currentCommand |
||
| 310 | */ |
||
| 311 | 2 | public function setCurrentCommand($currentCommand) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Determine if bot user id is mentioned in the message. |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | 1 | public function youTalkingToMe() |
|
| 337 | } |
||
| 338 |