Complex classes like ApiClient 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 ApiClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class ApiClient extends AbstractClient |
||
| 14 | { |
||
| 15 | const BASE_URL = 'https://slack.com/api/'; |
||
| 16 | const CONTENT_TYPE = 'application/x-www-form-urlencoded'; |
||
| 17 | |||
| 18 | private $arguments = [ |
||
| 19 | 'rtm.start' => [ |
||
| 20 | 'required' => [ |
||
| 21 | 'token', |
||
| 22 | ], |
||
| 23 | 'optional' => [ |
||
| 24 | 'simple_latest', |
||
| 25 | 'no_unreads', |
||
| 26 | 'mpim_aware', |
||
| 27 | ], |
||
| 28 | ], |
||
| 29 | 'chat.postMessage' => [ |
||
| 30 | 'required' => [ |
||
| 31 | 'token', |
||
| 32 | 'channel', |
||
| 33 | 'text', |
||
| 34 | ], |
||
| 35 | 'optional' => [ |
||
| 36 | 'parse', |
||
| 37 | 'link_names', |
||
| 38 | 'attachments', |
||
| 39 | 'unfurl_links', |
||
| 40 | 'unfurl_media', |
||
| 41 | 'username', |
||
| 42 | 'as_user', |
||
| 43 | 'icon_url', |
||
| 44 | 'icon_emoji', |
||
| 45 | ], |
||
| 46 | ], |
||
| 47 | 'oauth.access' => [ |
||
| 48 | 'required' => [ |
||
| 49 | 'client_id', |
||
| 50 | 'client_secret', |
||
| 51 | 'code', |
||
| 52 | ], |
||
| 53 | 'optional' => [ |
||
| 54 | 'redirect_uri', |
||
| 55 | ], |
||
| 56 | ], |
||
| 57 | 'team.info' => [ |
||
| 58 | 'required' => [ |
||
| 59 | 'token', |
||
| 60 | ], |
||
| 61 | ], |
||
| 62 | 'im.list' => [ |
||
| 63 | 'required' => [ |
||
| 64 | 'token', |
||
| 65 | ], |
||
| 66 | ], |
||
| 67 | 'users.list' => [ |
||
| 68 | 'required' => [ |
||
| 69 | 'token', |
||
| 70 | ], |
||
| 71 | 'optional' => [ |
||
| 72 | 'presence', |
||
| 73 | ], |
||
| 74 | ], |
||
| 75 | 'users.info' => [ |
||
| 76 | 'required' => [ |
||
| 77 | 'token', |
||
| 78 | 'user', |
||
| 79 | ], |
||
| 80 | ], |
||
| 81 | ]; |
||
| 82 | |||
| 83 | private $token; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * ApiClient constructor. |
||
| 87 | * |
||
| 88 | * @param null $token |
||
| 89 | */ |
||
| 90 | public function __construct($token = null) |
||
| 94 | 43 | ||
| 95 | /** |
||
| 96 | 43 | * API CURL Call with post method. |
|
| 97 | 43 | * |
|
| 98 | * @param string $method |
||
| 99 | * @param array $arguments |
||
| 100 | * |
||
| 101 | * @throws \Exception |
||
| 102 | * |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | public function apiCall($method, array $arguments = []) |
||
| 116 | 6 | ||
| 117 | 6 | /** |
|
| 118 | * @param $method |
||
| 119 | * @param $requestBody |
||
| 120 | * |
||
| 121 | * @throws \Exception |
||
| 122 | * |
||
| 123 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
| 124 | */ |
||
| 125 | private function sendRequest($method, $requestBody) |
||
| 141 | 2 | ||
| 142 | 2 | /** |
|
| 143 | * @param $method |
||
| 144 | * @param array $arguments |
||
| 145 | * |
||
| 146 | * @throws \Exception |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | private function prepareRequestBody($method, array $arguments = []) |
||
| 164 | |||
| 165 | /** |
||
| 166 | 30 | * @param $response |
|
| 167 | * |
||
| 168 | * @throws \Exception |
||
| 169 | * |
||
| 170 | * @return mixed |
||
| 171 | */ |
||
| 172 | private function processResponse($response) |
||
| 182 | |||
| 183 | /** |
||
| 184 | 27 | * @throws \Exception |
|
| 185 | * |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | public function getArgs() |
||
| 197 | 34 | ||
| 198 | 34 | /** |
|
| 199 | 34 | * @param $arguments |
|
| 200 | 34 | * |
|
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | public function chatPostMessage($arguments) |
||
| 207 | |||
| 208 | /** |
||
| 209 | 2 | * @param $arguments |
|
| 210 | * |
||
| 211 | 2 | * @throws \Exception |
|
| 212 | * |
||
| 213 | * @return mixed |
||
| 214 | */ |
||
| 215 | public function rtmStart($arguments) |
||
| 219 | |||
| 220 | /** |
||
| 221 | 1 | * @throws \Exception |
|
| 222 | * |
||
| 223 | 1 | * @return array |
|
| 224 | * @return Team |
||
| 225 | */ |
||
| 226 | public function teamInfo() |
||
| 236 | 4 | ||
| 237 | 2 | /** |
|
| 238 | * @return null|\Botonomous\AbstractBaseSlack |
||
| 239 | */ |
||
| 240 | 2 | public function teamInfoAsObject() |
|
| 252 | 1 | ||
| 253 | /** |
||
| 254 | * List all the Slack users in the team. |
||
| 255 | * |
||
| 256 | 1 | * @return array |
|
| 257 | */ |
||
| 258 | public function usersList() |
||
| 268 | 2 | ||
| 269 | 1 | /** |
|
| 270 | * Return a user by Slack user id. |
||
| 271 | * |
||
| 272 | 1 | * @param $arguments |
|
| 273 | * |
||
| 274 | * @throws \Exception |
||
| 275 | * |
||
| 276 | * @return mixed |
||
| 277 | */ |
||
| 278 | public function userInfo($arguments) |
||
| 289 | |||
| 290 | 4 | /** |
|
| 291 | * @throws \Exception |
||
| 292 | * |
||
| 293 | 6 | * @return mixed |
|
| 294 | */ |
||
| 295 | public function test() |
||
| 299 | |||
| 300 | /** |
||
| 301 | 1 | * @throws \Exception |
|
| 302 | * |
||
| 303 | 1 | * @return array |
|
| 304 | */ |
||
| 305 | public function imList() |
||
| 315 | 5 | ||
| 316 | 2 | /** |
|
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | 4 | public function imListAsObject() |
|
| 334 | 3 | ||
| 335 | 3 | /** |
|
| 336 | * @param $arguments |
||
| 337 | * |
||
| 338 | 3 | * @throws \Exception |
|
| 339 | * |
||
| 340 | * @return mixed |
||
| 341 | */ |
||
| 342 | public function oauthAccess($arguments) |
||
| 346 | |||
| 347 | /** |
||
| 348 | 5 | * @param $method |
|
| 349 | * @param $arguments |
||
| 350 | 5 | * |
|
| 351 | * @throws \Exception |
||
| 352 | * |
||
| 353 | * @return bool |
||
| 354 | */ |
||
| 355 | private function validateRequiredArguments($method, $arguments) |
||
| 371 | 30 | ||
| 372 | /** |
||
| 373 | * @param null $method |
||
| 374 | * |
||
| 375 | * @throws \Exception |
||
| 376 | * |
||
| 377 | * @return mixed |
||
| 378 | */ |
||
| 379 | public function getArguments($method = null) |
||
| 392 | 30 | ||
| 393 | /** |
||
| 394 | * @param array $arguments |
||
| 395 | */ |
||
| 396 | 27 | public function setArguments(array $arguments) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @param $method |
||
| 403 | * @param array $arguments |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | 35 | */ |
|
| 407 | public function filterArguments($method, array $arguments) |
||
| 423 | 1 | ||
| 424 | /** |
||
| 425 | 1 | * @return string |
|
| 426 | 1 | */ |
|
| 427 | public function getToken() |
||
| 436 | 31 | ||
| 437 | /** |
||
| 438 | 31 | * @param string $token |
|
| 439 | 3 | */ |
|
| 440 | public function setToken($token) |
||
| 444 | } |
||
| 445 |