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) |
|
238 | |||
239 | /** |
||
240 | * Get plugin class by command. |
||
241 | * |
||
242 | * @param Command $command |
||
243 | * |
||
244 | * @throws \Exception |
||
245 | * |
||
246 | * @return AbstractPlugin |
||
247 | */ |
||
248 | 3 | private function getPluginClassByCommand(Command $command) |
|
263 | |||
264 | /** |
||
265 | * @return array |
||
266 | */ |
||
267 | 2 | public function getCommands() |
|
275 | |||
276 | /** |
||
277 | * @param array $commands |
||
278 | */ |
||
279 | 2 | public function setCommands(array $commands) |
|
283 | |||
284 | /** |
||
285 | * @return string |
||
286 | */ |
||
287 | 1 | public function getLastError() |
|
291 | |||
292 | /** |
||
293 | * @param string $lastError |
||
294 | */ |
||
295 | 1 | public function setLastError($lastError) |
|
299 | |||
300 | /** |
||
301 | * Return the current command. |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | 1 | public function getCurrentCommand() |
|
309 | |||
310 | /** |
||
311 | * @param string $currentCommand |
||
312 | */ |
||
313 | 2 | public function setCurrentCommand($currentCommand) |
|
317 | |||
318 | /** |
||
319 | * Determine if bot user id is mentioned in the message. |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | 1 | public function youTalkingToMe() |
|
339 | } |
||
340 |