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) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Validate the command object. |
||
| 308 | * |
||
| 309 | * @param $commandObject |
||
| 310 | * @param $command |
||
| 311 | * @return bool |
||
| 312 | * @throws \Exception |
||
| 313 | */ |
||
| 314 | 4 | private function validateCommandObject($commandObject, $command) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | 2 | public function getCommands() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param array $commands |
||
| 351 | */ |
||
| 352 | 2 | public function setCommands(array $commands) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | 4 | public function getLastError() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $lastError |
||
| 367 | */ |
||
| 368 | 4 | public function setLastError($lastError) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Return the current command. |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | 1 | public function getCurrentCommand() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $currentCommand |
||
| 385 | */ |
||
| 386 | 2 | public function setCurrentCommand($currentCommand) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Determine if bot user id is mentioned in the message. |
||
| 393 | * |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | 1 | public function youTalkingToMe() |
|
| 412 | } |
||
| 413 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.