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 = []) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @throws \Exception |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | 34 | public function getArgs() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param $arguments |
||
| 166 | * |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | 2 | public function chatPostMessage($arguments) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param $arguments |
||
| 176 | * |
||
| 177 | * @throws \Exception |
||
| 178 | * |
||
| 179 | * @return mixed |
||
| 180 | */ |
||
| 181 | 1 | public function rtmStart($arguments) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @throws \Exception |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | * @return Team |
||
| 191 | */ |
||
| 192 | 4 | public function teamInfo() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @return null|\Botonomous\AbstractBaseSlack |
||
| 205 | */ |
||
| 206 | 2 | public function teamInfoAsObject() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * List all the Slack users in the team. |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | 2 | public function usersList() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Return a user by Slack user id. |
||
| 237 | * |
||
| 238 | * @param $arguments |
||
| 239 | * |
||
| 240 | * @throws \Exception |
||
| 241 | * |
||
| 242 | * @return mixed |
||
| 243 | */ |
||
| 244 | 10 | public function userInfo($arguments) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @throws \Exception |
||
| 258 | * |
||
| 259 | * @return mixed |
||
| 260 | */ |
||
| 261 | 1 | public function test() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @throws \Exception |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | 5 | public function imList() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | 4 | public function imListAsObject() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @param $arguments |
||
| 303 | * |
||
| 304 | * @throws \Exception |
||
| 305 | * |
||
| 306 | * @return mixed |
||
| 307 | */ |
||
| 308 | 5 | public function oauthAccess($arguments) |
|
| 312 | |||
| 313 | /** @noinspection PhpUndefinedClassInspection |
||
| 314 | * @param Client $client |
||
| 315 | */ |
||
| 316 | 33 | public function setClient(Client $client) |
|
| 320 | |||
| 321 | /** @noinspection PhpUndefinedClassInspection |
||
| 322 | * @return Client|null |
||
| 323 | */ |
||
| 324 | 30 | public function getClient() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param $method |
||
| 336 | * @param $arguments |
||
| 337 | * |
||
| 338 | * @throws \Exception |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | 33 | private function validateRequiredArguments($method, $arguments) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @param null $method |
||
| 361 | * |
||
| 362 | * @throws \Exception |
||
| 363 | * |
||
| 364 | * @return mixed |
||
| 365 | */ |
||
| 366 | 35 | public function getArguments($method = null) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @param array $arguments |
||
| 382 | */ |
||
| 383 | 1 | public function setArguments(array $arguments) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @param $method |
||
| 390 | * @param array $arguments |
||
| 391 | * |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | 31 | public function filterArguments($method, array $arguments) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | 34 | public function getToken() |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $token |
||
| 426 | */ |
||
| 427 | 43 | public function setToken($token) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * @return ArrayUtility |
||
| 434 | */ |
||
| 435 | 30 | public function getArrayUtility() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * @param ArrayUtility $arrayUtility |
||
| 446 | */ |
||
| 447 | 30 | public function setArrayUtility(ArrayUtility $arrayUtility) |
|
| 451 | } |
||
| 452 |