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 |
||
15 | class ApiClient extends AbstractClient |
||
16 | { |
||
17 | const BASE_URL = 'https://slack.com/api/'; |
||
18 | const CONTENT_TYPE = 'application/x-www-form-urlencoded'; |
||
19 | const REQUIRED_ARGUMENTS_KEY = 'required'; |
||
20 | const OPTIONAL_ARGUMENTS_KEY = 'optional'; |
||
21 | const TOKEN_ARGUMENT_KEY = 'token'; |
||
22 | |||
23 | private $arguments = [ |
||
24 | 'rtm.start' => [ |
||
25 | self::REQUIRED_ARGUMENTS_KEY => [ |
||
26 | self::TOKEN_ARGUMENT_KEY, |
||
27 | ], |
||
28 | self::OPTIONAL_ARGUMENTS_KEY => [ |
||
29 | 'simple_latest', |
||
30 | 'no_unreads', |
||
31 | 'mpim_aware', |
||
32 | ], |
||
33 | ], |
||
34 | 'chat.postMessage' => [ |
||
35 | self::REQUIRED_ARGUMENTS_KEY => [ |
||
36 | self::TOKEN_ARGUMENT_KEY, |
||
37 | 'channel', |
||
38 | 'text', |
||
39 | ], |
||
40 | self::OPTIONAL_ARGUMENTS_KEY => [ |
||
41 | 'parse', |
||
42 | 'link_names', |
||
43 | 'attachments', |
||
44 | 'unfurl_links', |
||
45 | 'unfurl_media', |
||
46 | 'username', |
||
47 | 'as_user', |
||
48 | 'icon_url', |
||
49 | 'icon_emoji', |
||
50 | ], |
||
51 | ], |
||
52 | 'oauth.access' => [ |
||
53 | self::REQUIRED_ARGUMENTS_KEY => [ |
||
54 | 'client_id', |
||
55 | 'client_secret', |
||
56 | 'code', |
||
57 | ], |
||
58 | self::OPTIONAL_ARGUMENTS_KEY => [ |
||
59 | 'redirect_uri', |
||
60 | ], |
||
61 | ], |
||
62 | 'team.info' => [ |
||
63 | self::REQUIRED_ARGUMENTS_KEY => [ |
||
64 | self::TOKEN_ARGUMENT_KEY, |
||
65 | ], |
||
66 | ], |
||
67 | 'im.list' => [ |
||
68 | self::REQUIRED_ARGUMENTS_KEY => [ |
||
69 | self::TOKEN_ARGUMENT_KEY, |
||
70 | ], |
||
71 | ], |
||
72 | 'users.list' => [ |
||
73 | self::REQUIRED_ARGUMENTS_KEY => [ |
||
74 | self::TOKEN_ARGUMENT_KEY, |
||
75 | ], |
||
76 | self::OPTIONAL_ARGUMENTS_KEY => [ |
||
77 | 'presence', |
||
78 | ], |
||
79 | ], |
||
80 | 'users.info' => [ |
||
81 | self::REQUIRED_ARGUMENTS_KEY => [ |
||
82 | self::TOKEN_ARGUMENT_KEY, |
||
83 | 'user', |
||
84 | ], |
||
85 | ], |
||
86 | ]; |
||
87 | |||
88 | private $token; |
||
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(string $method, array $arguments = []) |
|
121 | |||
122 | /** |
||
123 | * @param $method |
||
124 | * @param $requestBody |
||
125 | * |
||
126 | * @throws \Exception |
||
127 | * |
||
128 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
129 | */ |
||
130 | 30 | private function sendRequest(string $method, $requestBody) |
|
146 | |||
147 | /** |
||
148 | * @param $method |
||
149 | * @param array $arguments |
||
150 | * |
||
151 | * @throws \Exception |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | 33 | private function prepareRequestBody(string $method, array $arguments = []) |
|
169 | |||
170 | /** |
||
171 | * @param $response |
||
172 | * |
||
173 | * @throws \Exception |
||
174 | * |
||
175 | * @return mixed |
||
176 | */ |
||
177 | 28 | private function processResponse(ResponseInterface $response) |
|
187 | |||
188 | /** |
||
189 | * @throws \Exception |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | 34 | public function getArgs(): array |
|
202 | |||
203 | /** |
||
204 | * @param array $arguments |
||
205 | * |
||
206 | * @throws \Exception |
||
207 | * |
||
208 | * @return mixed |
||
209 | */ |
||
210 | 2 | public function chatPostMessage(array $arguments) |
|
214 | |||
215 | /** |
||
216 | * @param $arguments |
||
217 | * |
||
218 | * @throws \Exception |
||
219 | * |
||
220 | * @return mixed |
||
221 | */ |
||
222 | 1 | public function rtmStart(array $arguments) |
|
226 | |||
227 | /** |
||
228 | * @throws \Exception |
||
229 | * |
||
230 | * @return array |
||
231 | * @return Team |
||
232 | */ |
||
233 | 4 | public function teamInfo() |
|
243 | |||
244 | /** |
||
245 | * @throws \Exception |
||
246 | * |
||
247 | * @return \Botonomous\AbstractBaseSlack|null|void |
||
248 | */ |
||
249 | 2 | public function teamInfoAsObject() |
|
261 | |||
262 | /** |
||
263 | * List all the Slack users in the team. |
||
264 | * |
||
265 | * @throws \Exception |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | 2 | public function usersList(): array |
|
279 | |||
280 | /** |
||
281 | * Return a user by Slack user id. |
||
282 | * |
||
283 | * @param $arguments |
||
284 | * |
||
285 | * @throws \Exception |
||
286 | * |
||
287 | * @return mixed |
||
288 | */ |
||
289 | 10 | public function userInfo(array $arguments) |
|
300 | |||
301 | /** |
||
302 | * @throws \Exception |
||
303 | * |
||
304 | * @return mixed |
||
305 | */ |
||
306 | 1 | public function test() |
|
310 | |||
311 | /** |
||
312 | * @throws \Exception |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | 5 | public function imList() |
|
326 | |||
327 | /** |
||
328 | * @throws \Exception |
||
329 | * |
||
330 | * @return array |
||
331 | */ |
||
332 | 4 | public function imListAsObject(): array |
|
347 | |||
348 | /** |
||
349 | * @param $arguments |
||
350 | * |
||
351 | * @throws \Exception |
||
352 | * |
||
353 | * @return mixed |
||
354 | */ |
||
355 | 5 | public function oauthAccess(array $arguments) |
|
359 | |||
360 | /** |
||
361 | * @param $method |
||
362 | * @param $arguments |
||
363 | * |
||
364 | * @throws \Exception |
||
365 | * |
||
366 | * @return bool |
||
367 | */ |
||
368 | 33 | private function validateRequiredArguments(string $method, array $arguments) |
|
384 | |||
385 | /** |
||
386 | * @param null $method |
||
387 | * |
||
388 | * @throws \Exception |
||
389 | * |
||
390 | * @return mixed |
||
391 | */ |
||
392 | 35 | public function getArguments($method = null) |
|
405 | |||
406 | /** |
||
407 | * @param array $arguments |
||
408 | */ |
||
409 | 1 | public function setArguments(array $arguments) |
|
413 | |||
414 | /** |
||
415 | * @param string $method |
||
416 | * @param array $arguments |
||
417 | * |
||
418 | * @throws \Exception |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | 31 | public function filterArguments(string $method, array $arguments): array |
|
441 | |||
442 | /** |
||
443 | * @throws \Exception |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | 34 | public function getToken(): string |
|
456 | |||
457 | /** |
||
458 | * @param string $token |
||
459 | */ |
||
460 | 43 | public function setToken($token) |
|
464 | } |
||
465 |