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 |
||
| 13 | class Api |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var ClientInterface |
||
| 17 | */ |
||
| 18 | private $client; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $currentTeam; |
||
| 24 | |||
| 25 | public function __construct(ClientInterface $client, string $currentTeam) |
||
| 30 | 39 | ||
| 31 | 39 | public function user(array $params = []): array |
|
| 37 | |||
| 38 | 1 | public function teams(): array |
|
| 42 | |||
| 43 | public function team(string $name = null): array |
||
| 47 | |||
| 48 | 1 | public function stats(): array |
|
| 52 | |||
| 53 | public function members(): array |
||
| 57 | |||
| 58 | 1 | public function posts(array $params = []): array |
|
| 64 | |||
| 65 | public function post(int $number): array |
||
| 69 | |||
| 70 | public function createPost(array $data): array |
||
| 78 | |||
| 79 | public function updatePost(int $number, array $data): array |
||
| 87 | 1 | ||
| 88 | public function deletePost(int $number): array |
||
| 92 | |||
| 93 | public function comments(int $number = null, array $params = []): array |
||
| 105 | |||
| 106 | 1 | public function comment(int $commentId, array $params = []): array |
|
| 112 | |||
| 113 | public function createComment(int $postNumber, array $data): array |
||
| 121 | 1 | ||
| 122 | public function updateComment(int $commentId, array $data): array |
||
| 130 | |||
| 131 | public function deleteComment(int $commentId): array |
||
| 135 | 1 | ||
| 136 | public function createSharing(int $postNumber): array |
||
| 140 | |||
| 141 | public function deleteSharing(int $postNumber): array |
||
| 145 | |||
| 146 | 2 | public function postStargazers(int $postNumber, array $params = []): array |
|
| 152 | |||
| 153 | public function addPostStar(int $postNumber, array $params = []): array |
||
| 159 | |||
| 160 | public function deletePostStar(int $postNumber): array |
||
| 164 | |||
| 165 | 1 | public function commentStargazers(int $commentId, array $params = []): array |
|
| 171 | |||
| 172 | public function addCommentStar(int $commentId, array $params = []): array |
||
| 178 | 1 | ||
| 179 | public function deleteCommentStar(int $commentId): array |
||
| 183 | |||
| 184 | public function watchers(int $postNumber, array $params = []): array |
||
| 190 | |||
| 191 | public function addWatch(int $postNumber): array |
||
| 195 | 1 | ||
| 196 | public function deleteWatch(int $postNumber): array |
||
| 200 | |||
| 201 | public function categories(): array |
||
| 205 | |||
| 206 | public function batchMoveCategory(array $params = []): array |
||
| 212 | |||
| 213 | public function tags(): array |
||
| 217 | 1 | ||
| 218 | public function invitation(): array |
||
| 222 | |||
| 223 | public function regenerateInvitation(): array |
||
| 227 | 1 | ||
| 228 | public function pendingInvitations(array $params = []): array |
||
| 234 | |||
| 235 | public function sendInvitation(array $emails): array |
||
| 243 | |||
| 244 | public function cancelInvitation(string $code): array |
||
| 248 | |||
| 249 | public function emojis(array $params = []): array |
||
| 255 | |||
| 256 | public function createEmoji(array $data): array |
||
| 264 | |||
| 265 | 1 | public function deleteEmoji(string $code): array |
|
| 269 | |||
| 270 | public static function factory(string $accessToken, string $currentTeam): self |
||
| 276 | 1 | ||
| 277 | 1 | private function getCurrentTeamUrl(string $path = ''): string |
|
| 281 | } |
||
| 282 |