Complex classes like Api 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 Api, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Api |
||
12 | { |
||
13 | /** |
||
14 | * @var ClientInterface |
||
15 | */ |
||
16 | private $client; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $currentTeam; |
||
22 | |||
23 | /** |
||
24 | * @param ClientInterface $client |
||
25 | * @param string $currentTeam |
||
26 | */ |
||
27 | public function __construct(ClientInterface $client, $currentTeam) |
||
32 | |||
33 | /** |
||
34 | * @param array $params |
||
35 | * |
||
36 | * @return array |
||
37 | */ |
||
38 | public function user(array $params = []) |
||
44 | |||
45 | /** |
||
46 | * @return array |
||
47 | */ |
||
48 | public function teams() |
||
52 | |||
53 | /** |
||
54 | * @param string|null $name |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | public function team($name = null) |
||
62 | |||
63 | /** |
||
64 | * @return array |
||
65 | */ |
||
66 | public function stats() |
||
70 | |||
71 | /** |
||
72 | * @return array |
||
73 | */ |
||
74 | public function members() |
||
78 | |||
79 | /** |
||
80 | * @param array $params |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | public function posts(array $params = []) |
||
90 | |||
91 | /** |
||
92 | * @param int $number |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | public function post($number) |
||
100 | |||
101 | /** |
||
102 | * @param array $data |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | public function createPost($data) |
||
114 | |||
115 | /** |
||
116 | * @param int $number |
||
117 | * @param array $data |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | public function updatePost($number, array $data) |
||
129 | |||
130 | /** |
||
131 | * @param int $number |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | public function deletePost($number) |
||
139 | |||
140 | /** |
||
141 | * @param int $number |
||
142 | * @param array $params |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | public function comments($number = null, array $params = []) |
||
158 | |||
159 | /** |
||
160 | * @param int $commentId |
||
161 | * @param array $params |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | public function comment($commentId, array $params = []) |
||
171 | |||
172 | /** |
||
173 | * @param int $postNumber |
||
174 | * @param array $data |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | public function createComment($postNumber, array $data) |
||
186 | |||
187 | /** |
||
188 | * @param int $commentId |
||
189 | * @param array $data |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | public function updateComment($commentId, array $data) |
||
201 | |||
202 | /** |
||
203 | * @param int $commentId |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | public function deleteComment($commentId) |
||
211 | |||
212 | /** |
||
213 | * @param int $postNumber |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | public function createSharing($postNumber) |
||
221 | |||
222 | /** |
||
223 | * @param int $postNumber |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | public function deleteSharing($postNumber) |
||
231 | |||
232 | /** |
||
233 | * @param int $postNumber |
||
234 | * @param array $params |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | public function postStargazers($postNumber, array $params = []) |
||
244 | |||
245 | /** |
||
246 | * @param int $postNumber |
||
247 | * @param array $params |
||
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | public function addPostStar($postNumber, array $params = []) |
||
257 | |||
258 | /** |
||
259 | * @param int $postNumber |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | public function deletePostStar($postNumber) |
||
267 | |||
268 | /** |
||
269 | * @param int $commentId |
||
270 | * @param array $params |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | public function commentStargazers($commentId, array $params = []) |
||
280 | |||
281 | /** |
||
282 | * @param $commentId |
||
283 | * @param array $params |
||
284 | * |
||
285 | * @return array |
||
286 | */ |
||
287 | public function addCommentStar($commentId, array $params = []) |
||
293 | |||
294 | /** |
||
295 | * @param $commentId |
||
296 | * |
||
297 | * @return array |
||
298 | */ |
||
299 | public function deleteCommentStar($commentId) |
||
303 | |||
304 | /** |
||
305 | * @param integer$postNumber |
||
306 | * @param array $params |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | public function watchers($postNumber, array $params = []) |
||
316 | |||
317 | /** |
||
318 | * @param $postNumber |
||
319 | * |
||
320 | * @return array |
||
321 | */ |
||
322 | public function addWatch($postNumber) |
||
326 | |||
327 | /** |
||
328 | * @param $postNumber |
||
329 | * |
||
330 | * @return array |
||
331 | */ |
||
332 | public function deleteWatch($postNumber) |
||
336 | |||
337 | /** |
||
338 | * @return array |
||
339 | */ |
||
340 | public function categories() |
||
344 | |||
345 | /** |
||
346 | * @param array $params |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | public function batchMoveCategory(array $params = []) |
||
356 | |||
357 | /** |
||
358 | * @return array |
||
359 | */ |
||
360 | public function tags() |
||
364 | |||
365 | /** |
||
366 | * @return array |
||
367 | */ |
||
368 | public function invitation() |
||
372 | |||
373 | /** |
||
374 | * @return array |
||
375 | */ |
||
376 | public function regenerateInvitation() |
||
380 | |||
381 | /** |
||
382 | * @param $params |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | public function pendingInvitations(array $params = []) |
||
392 | |||
393 | /** |
||
394 | * @param array $emails |
||
395 | * |
||
396 | * @return array |
||
397 | */ |
||
398 | public function sendInvitation(array $emails) |
||
406 | |||
407 | /** |
||
408 | * @param string $code |
||
409 | * |
||
410 | * @return array |
||
411 | */ |
||
412 | public function cancelInvitation($code) |
||
416 | |||
417 | /** |
||
418 | * @param array $params |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | public function emojis(array $params = []) |
||
428 | |||
429 | /** |
||
430 | * @param $data |
||
431 | * |
||
432 | * @return array |
||
433 | */ |
||
434 | public function createEmoji(array $data) |
||
442 | |||
443 | /** |
||
444 | * @param $code |
||
445 | * |
||
446 | * @return array |
||
447 | */ |
||
448 | public function deleteEmoji($code) |
||
452 | |||
453 | /** |
||
454 | * @param $accessToken |
||
455 | * @param $currentTeam |
||
456 | * |
||
457 | * @return Api |
||
458 | */ |
||
459 | public static function factory($accessToken, $currentTeam) |
||
465 | |||
466 | private function getCurrentTeamUrl($path = '') |
||
470 | } |
||
471 |