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 | const CONTENT_TYPE = 'application/x-www-form-urlencoded'; |
||
| 21 | |||
| 22 | private $arguments = [ |
||
| 23 | 'rtm.start' => [ |
||
| 24 | 'required' => [ |
||
| 25 | 'token', |
||
| 26 | ], |
||
| 27 | 'optional' => [ |
||
| 28 | 'simple_latest', |
||
| 29 | 'no_unreads', |
||
| 30 | 'mpim_aware', |
||
| 31 | ], |
||
| 32 | ], |
||
| 33 | 'chat.postMessage' => [ |
||
| 34 | 'required' => [ |
||
| 35 | 'token', |
||
| 36 | 'channel', |
||
| 37 | 'text', |
||
| 38 | ], |
||
| 39 | 'optional' => [ |
||
| 40 | 'parse', |
||
| 41 | 'link_names', |
||
| 42 | 'attachments', |
||
| 43 | 'unfurl_links', |
||
| 44 | 'unfurl_media', |
||
| 45 | 'username', |
||
| 46 | 'as_user', |
||
| 47 | 'icon_url', |
||
| 48 | 'icon_emoji', |
||
| 49 | ], |
||
| 50 | ], |
||
| 51 | 'oauth.access' => [ |
||
| 52 | 'required' => [ |
||
| 53 | 'client_id', |
||
| 54 | 'client_secret', |
||
| 55 | 'code', |
||
| 56 | ], |
||
| 57 | 'optional' => [ |
||
| 58 | 'redirect_uri', |
||
| 59 | ], |
||
| 60 | ], |
||
| 61 | 'team.info' => [ |
||
| 62 | 'required' => [ |
||
| 63 | 'token', |
||
| 64 | ], |
||
| 65 | ], |
||
| 66 | 'im.list' => [ |
||
| 67 | 'required' => [ |
||
| 68 | 'token', |
||
| 69 | ], |
||
| 70 | ], |
||
| 71 | 'users.list' => [ |
||
| 72 | 'required' => [ |
||
| 73 | 'token', |
||
| 74 | ], |
||
| 75 | 'optional' => [ |
||
| 76 | 'presence', |
||
| 77 | ], |
||
| 78 | ], |
||
| 79 | 'users.info' => [ |
||
| 80 | 'required' => [ |
||
| 81 | 'token', |
||
| 82 | 'user', |
||
| 83 | ], |
||
| 84 | ], |
||
| 85 | ]; |
||
| 86 | |||
| 87 | private $client; |
||
| 88 | private $token; |
||
| 89 | private $arrayUtility; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * ApiClient constructor. |
||
| 93 | * |
||
| 94 | * @param null $token |
||
| 95 | */ |
||
| 96 | 43 | public function __construct($token = null) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * API CURL Call with post method. |
||
| 103 | * |
||
| 104 | * @param string $method |
||
| 105 | * @param array $arguments |
||
| 106 | * |
||
| 107 | * @throws \Exception |
||
| 108 | * |
||
| 109 | * @return mixed |
||
| 110 | */ |
||
| 111 | 33 | public function apiCall($method, array $arguments = []) |
|
| 121 | |||
| 122 | 30 | private function sendRequest($method, $requestBody) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param $method |
||
| 135 | * @param array $arguments |
||
| 136 | * |
||
| 137 | * @throws \Exception |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 33 | private function prepareRequestBody($method, array $arguments = []) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param $response |
||
| 158 | * |
||
| 159 | * @throws \Exception |
||
| 160 | * |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | 28 | private function processResponse($response) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @throws \Exception |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | 34 | public function getArgs() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param $arguments |
||
| 193 | * |
||
| 194 | * @return mixed |
||
| 195 | */ |
||
| 196 | 2 | public function chatPostMessage($arguments) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @param $arguments |
||
| 203 | * |
||
| 204 | * @throws \Exception |
||
| 205 | * |
||
| 206 | * @return mixed |
||
| 207 | */ |
||
| 208 | 1 | public function rtmStart($arguments) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @throws \Exception |
||
| 215 | * |
||
| 216 | * @return array |
||
| 217 | * @return Team |
||
| 218 | */ |
||
| 219 | 4 | public function teamInfo() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @return null|\Botonomous\AbstractBaseSlack |
||
| 232 | */ |
||
| 233 | 2 | public function teamInfoAsObject() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * List all the Slack users in the team. |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | 2 | public function usersList() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Return a user by Slack user id. |
||
| 264 | * |
||
| 265 | * @param $arguments |
||
| 266 | * |
||
| 267 | * @throws \Exception |
||
| 268 | * |
||
| 269 | * @return mixed |
||
| 270 | */ |
||
| 271 | 10 | public function userInfo($arguments) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @throws \Exception |
||
| 285 | * |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | 1 | public function test() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @throws \Exception |
||
| 295 | * |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | 5 | public function imList() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | 4 | public function imListAsObject() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @param $arguments |
||
| 330 | * |
||
| 331 | * @throws \Exception |
||
| 332 | * |
||
| 333 | * @return mixed |
||
| 334 | */ |
||
| 335 | 5 | public function oauthAccess($arguments) |
|
| 339 | |||
| 340 | /** @noinspection PhpUndefinedClassInspection |
||
| 341 | * @param Client $client |
||
| 342 | */ |
||
| 343 | 33 | public function setClient(Client $client) |
|
| 347 | |||
| 348 | /** @noinspection PhpUndefinedClassInspection |
||
| 349 | * @return Client|null |
||
| 350 | */ |
||
| 351 | 30 | public function getClient() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @param $method |
||
| 363 | * @param $arguments |
||
| 364 | * |
||
| 365 | * @throws \Exception |
||
| 366 | * |
||
| 367 | * @return bool |
||
| 368 | */ |
||
| 369 | 33 | private function validateRequiredArguments($method, $arguments) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @param null $method |
||
| 388 | * |
||
| 389 | * @throws \Exception |
||
| 390 | * |
||
| 391 | * @return mixed |
||
| 392 | */ |
||
| 393 | 35 | public function getArguments($method = null) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @param array $arguments |
||
| 409 | */ |
||
| 410 | 1 | public function setArguments(array $arguments) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * @param $method |
||
| 417 | * @param array $arguments |
||
| 418 | * |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | 31 | public function filterArguments($method, array $arguments) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | 34 | public function getToken() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $token |
||
| 453 | */ |
||
| 454 | 43 | public function setToken($token) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * @return ArrayUtility |
||
| 461 | */ |
||
| 462 | 30 | public function getArrayUtility() |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @param ArrayUtility $arrayUtility |
||
| 473 | */ |
||
| 474 | 30 | public function setArrayUtility(ArrayUtility $arrayUtility) |
|
| 478 | } |
||
| 479 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.