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 |
||
| 17 | class ApiClient |
||
| 18 | { |
||
| 19 | const BASE_URL = 'https://slack.com/api/'; |
||
| 20 | |||
| 21 | private $arguments = [ |
||
| 22 | 'rtm.start' => [ |
||
| 23 | 'required' => [ |
||
| 24 | 'token', |
||
| 25 | ], |
||
| 26 | 'optional' => [ |
||
| 27 | 'simple_latest', |
||
| 28 | 'no_unreads', |
||
| 29 | 'mpim_aware', |
||
| 30 | ], |
||
| 31 | ], |
||
| 32 | 'chat.postMessage' => [ |
||
| 33 | 'required' => [ |
||
| 34 | 'token', |
||
| 35 | 'channel', |
||
| 36 | 'text', |
||
| 37 | ], |
||
| 38 | 'optional' => [ |
||
| 39 | 'parse', |
||
| 40 | 'link_names', |
||
| 41 | 'attachments', |
||
| 42 | 'unfurl_links', |
||
| 43 | 'unfurl_media', |
||
| 44 | 'username', |
||
| 45 | 'as_user', |
||
| 46 | 'icon_url', |
||
| 47 | 'icon_emoji', |
||
| 48 | ], |
||
| 49 | ], |
||
| 50 | 'oauth.access' => [ |
||
| 51 | 'required' => [ |
||
| 52 | 'client_id', |
||
| 53 | 'client_secret', |
||
| 54 | 'code', |
||
| 55 | ], |
||
| 56 | 'optional' => [ |
||
| 57 | 'redirect_uri', |
||
| 58 | ], |
||
| 59 | ], |
||
| 60 | 'team.info' => [ |
||
| 61 | 'required' => [ |
||
| 62 | 'token', |
||
| 63 | ], |
||
| 64 | ], |
||
| 65 | 'im.list' => [ |
||
| 66 | 'required' => [ |
||
| 67 | 'token', |
||
| 68 | ], |
||
| 69 | ], |
||
| 70 | 'users.list' => [ |
||
| 71 | 'required' => [ |
||
| 72 | 'token', |
||
| 73 | ], |
||
| 74 | 'optional' => [ |
||
| 75 | 'presence', |
||
| 76 | ], |
||
| 77 | ], |
||
| 78 | 'users.info' => [ |
||
| 79 | 'required' => [ |
||
| 80 | 'token', |
||
| 81 | 'user', |
||
| 82 | ], |
||
| 83 | ], |
||
| 84 | ]; |
||
| 85 | |||
| 86 | private $client; |
||
| 87 | private $token; |
||
| 88 | private $arrayUtility; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * ApiClient constructor. |
||
| 92 | * |
||
| 93 | * @param null $token |
||
| 94 | */ |
||
| 95 | 43 | public function __construct($token = null) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * API CURL Call with post method. |
||
| 102 | * |
||
| 103 | * @param string $method |
||
| 104 | * @param array $arguments |
||
| 105 | * |
||
| 106 | * @throws \Exception |
||
| 107 | * |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | 33 | public function apiCall($method, array $arguments = []) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param $response |
||
| 147 | * |
||
| 148 | * @throws \Exception |
||
| 149 | * |
||
| 150 | * @return mixed |
||
| 151 | */ |
||
| 152 | 28 | private function processResponse($response) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @throws \Exception |
||
| 165 | * |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | 34 | public function getArgs() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @param $arguments |
||
| 182 | * |
||
| 183 | * @return mixed |
||
| 184 | */ |
||
| 185 | 2 | public function chatPostMessage($arguments) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param $arguments |
||
| 192 | * |
||
| 193 | * @throws \Exception |
||
| 194 | * |
||
| 195 | * @return mixed |
||
| 196 | */ |
||
| 197 | 1 | public function rtmStart($arguments) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @throws \Exception |
||
| 204 | * |
||
| 205 | * @return array |
||
| 206 | * @return Team |
||
| 207 | */ |
||
| 208 | 4 | public function teamInfo() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @return null|\Botonomous\AbstractBaseSlack |
||
| 221 | */ |
||
| 222 | 2 | public function teamInfoAsObject() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * List all the Slack users in the team. |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | 2 | public function usersList() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Return a user by Slack user id. |
||
| 253 | * |
||
| 254 | * @param $arguments |
||
| 255 | * |
||
| 256 | * @throws \Exception |
||
| 257 | * |
||
| 258 | * @return mixed |
||
| 259 | */ |
||
| 260 | 10 | public function userInfo($arguments) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @throws \Exception |
||
| 274 | * |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | 1 | public function test() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @throws \Exception |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | 5 | public function imList() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | 4 | public function imListAsObject() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param $arguments |
||
| 319 | * |
||
| 320 | * @throws \Exception |
||
| 321 | * |
||
| 322 | * @return mixed |
||
| 323 | */ |
||
| 324 | 5 | public function oauthAccess($arguments) |
|
| 328 | |||
| 329 | /** @noinspection PhpUndefinedClassInspection |
||
| 330 | * @param Client $client |
||
| 331 | */ |
||
| 332 | 33 | public function setClient(Client $client) |
|
| 336 | |||
| 337 | /** @noinspection PhpUndefinedClassInspection |
||
| 338 | * @return Client|null |
||
| 339 | */ |
||
| 340 | 30 | public function getClient() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param $method |
||
| 352 | * @param $arguments |
||
| 353 | * |
||
| 354 | * @throws \Exception |
||
| 355 | * |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | 33 | private function validateRequiredArguments($method, $arguments) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @param null $method |
||
| 377 | * |
||
| 378 | * @throws \Exception |
||
| 379 | * |
||
| 380 | * @return mixed |
||
| 381 | */ |
||
| 382 | 35 | public function getArguments($method = null) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @param array $arguments |
||
| 398 | */ |
||
| 399 | 1 | public function setArguments(array $arguments) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * @param $method |
||
| 406 | * @param array $arguments |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | 31 | public function filterArguments($method, array $arguments) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | 34 | public function getToken() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * @param string $token |
||
| 442 | */ |
||
| 443 | 43 | public function setToken($token) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @return ArrayUtility |
||
| 450 | */ |
||
| 451 | 30 | public function getArrayUtility() |
|
| 459 | |||
| 460 | /** |
||
| 461 | * @param ArrayUtility $arrayUtility |
||
| 462 | */ |
||
| 463 | 30 | public function setArrayUtility(ArrayUtility $arrayUtility) |
|
| 467 | } |
||
| 468 |