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 | * @return Command|void |
||
| 260 | */ |
||
| 261 | 7 | public function getCommandByMessage($message = null) |
|
| 262 | { |
||
| 263 | // If message is not set, get it from the current request |
||
| 264 | 7 | if ($message === null) { |
|
| 265 | 4 | $message = $this->getListener()->getMessage(); |
|
| 266 | } |
||
| 267 | |||
| 268 | 7 | if (empty($message)) { |
|
| 269 | 1 | $this->setLastError('Message is empty'); |
|
| 270 | 1 | return; |
|
| 271 | } |
||
| 272 | |||
| 273 | /* |
||
| 274 | * Process the message. |
||
| 275 | */ |
||
| 276 | 6 | return $this->getCommandObjectByMessage($message); |
|
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param $message |
||
| 281 | * |
||
| 282 | * @return Command|void |
||
| 283 | */ |
||
| 284 | 6 | private function getCommandObjectByMessage($message) |
|
| 285 | { |
||
| 286 | 6 | $command = $this->getMessageUtility()->extractCommandName($message); |
|
| 287 | |||
| 288 | // check command name |
||
| 289 | 6 | if (empty($command)) { |
|
| 290 | // get the default command if no command is find in the message |
||
| 291 | 2 | $command = $this->getConfig()->get('defaultCommand'); |
|
| 292 | |||
| 293 | 2 | if (empty($command)) { |
|
| 294 | 2 | $this->setLastError($this->getDictionary()->get('generic-messages')['noCommandMessage']); |
|
| 295 | 2 | return; |
|
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | 4 | return $this->getCommandObjectByCommand($command); |
|
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param $command |
||
| 304 | * |
||
| 305 | * @return Command|void |
||
| 306 | */ |
||
| 307 | 4 | private function getCommandObjectByCommand($command) |
|
| 308 | { |
||
| 309 | 4 | $commandObject = $this->getCommandContainer()->getAsObject($command); |
|
| 310 | |||
| 311 | 4 | if ($this->validateCommandObject($commandObject) !== true) { |
|
| 312 | 1 | return; |
|
| 313 | } |
||
| 314 | |||
| 315 | 4 | return $commandObject; |
|
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Validate the command object. |
||
| 320 | * |
||
| 321 | * @param Command|null $commandObject |
||
| 322 | * |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | 4 | private function validateCommandObject($commandObject) |
|
| 326 | { |
||
| 327 | // check command details |
||
| 328 | 4 | if (empty($commandObject)) { |
|
| 329 | 1 | $this->setLastError( |
|
| 330 | 1 | $this->getDictionary()->getValueByKey('generic-messages', 'unknownCommandMessage') |
|
| 331 | ); |
||
| 332 | |||
| 333 | 1 | return false; |
|
| 334 | } |
||
| 335 | |||
| 336 | 4 | return true; |
|
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @return array |
||
| 341 | */ |
||
| 342 | 2 | public function getCommands() |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @param array $commands |
||
| 353 | */ |
||
| 354 | 2 | public function setCommands(array $commands) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | 4 | public function getLastError() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $lastError |
||
| 369 | */ |
||
| 370 | 4 | public function setLastError($lastError) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Return the current command. |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | 1 | public function getCurrentCommand() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $currentCommand |
||
| 387 | */ |
||
| 388 | 2 | public function setCurrentCommand($currentCommand) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Determine if bot user id is mentioned in the message. |
||
| 395 | * |
||
| 396 | * @return bool |
||
| 397 | */ |
||
| 398 | 1 | public function youTalkingToMe() |
|
| 414 | } |
||
| 415 |