Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
17 | final class TeamsApi extends HttpApi |
||
18 | { |
||
19 | /** |
||
20 | * Returns an team by its ID. |
||
21 | * |
||
22 | * @param string $id |
||
23 | * |
||
24 | * @return Team|ResponseInterface |
||
25 | */ |
||
26 | 9 | View Code Duplication | public function getTeamById($id) |
|
|||
27 | { |
||
28 | 9 | if (empty($id)) { |
|
29 | 1 | throw new InvalidArgumentException('Id can not be empty'); |
|
30 | } |
||
31 | |||
32 | 8 | $response = $this->httpGet(sprintf('/teams/%s', $id)); |
|
33 | |||
34 | 8 | return $this->handleResponse($response, Team::class); |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * Create a team. Required parameters: 'name', 'display_name' and 'type'. |
||
39 | * |
||
40 | * @param array $params |
||
41 | * |
||
42 | * @return Team|ResponseInterface |
||
43 | */ |
||
44 | 8 | public function createTeam(array $params) |
|
52 | |||
53 | /** |
||
54 | * Returns a collection of teams. |
||
55 | * |
||
56 | * @param array $params The listing params, 'page', 'per_page' |
||
57 | * |
||
58 | * @return Teams|ResponseInterface |
||
59 | */ |
||
60 | 8 | public function getTeams(array $params = []) |
|
66 | |||
67 | /** |
||
68 | * Delete a team softly and put in archived only. |
||
69 | * |
||
70 | * @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D%2Fdelete |
||
71 | * |
||
72 | * @param string $teamId Team GUID |
||
73 | * @param bool $permanent permanently delete the team, to be used for complience reasons only |
||
74 | * |
||
75 | * @return Status|ResponseInterface |
||
76 | */ |
||
77 | 10 | public function deleteTeam(string $teamId, bool $permanent = false) |
|
89 | |||
90 | /** |
||
91 | * Returns a team given its name. |
||
92 | * |
||
93 | * @param string $name |
||
94 | * |
||
95 | * @return Team|ResponseInterface |
||
96 | */ |
||
97 | 9 | public function getTeamByName($name) |
|
107 | |||
108 | /** |
||
109 | * Add a user to a team, with specific roles. |
||
110 | * |
||
111 | * @param string $teamId |
||
112 | * @param string $userId |
||
113 | * @param string $roles |
||
114 | * @param array $pathParams |
||
115 | * |
||
116 | * @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members%2Fpost |
||
117 | * |
||
118 | * @return TeamMember|ResponseInterface |
||
119 | */ |
||
120 | 11 | View Code Duplication | public function addTeamMember($teamId, $userId, $roles = '', $pathParams = []) |
136 | |||
137 | /** |
||
138 | * Return the team members. |
||
139 | * |
||
140 | * @param string $teamId The Team ID |
||
141 | * @param array $params The listing params, 'page', 'per_page' |
||
142 | * |
||
143 | * @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members%2Fget |
||
144 | * |
||
145 | * @return TeamMembers |
||
146 | */ |
||
147 | 9 | public function getTeamMembers(string $teamId, array $params = []) |
|
157 | |||
158 | /** |
||
159 | * Get a team member from the system given a Team and User IDs. |
||
160 | * |
||
161 | * @param string $teamId The Team GUID |
||
162 | * @param string $userId The User GUID |
||
163 | * |
||
164 | * @return TeamMember |
||
165 | */ |
||
166 | 11 | View Code Duplication | public function getTeamMember(string $teamId, string $userId) |
176 | |||
177 | /** |
||
178 | * Remove a team member. |
||
179 | * |
||
180 | * @param string $teamId The team ID |
||
181 | * @param string $userId The user ID |
||
182 | * |
||
183 | * https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members~1%7Buser_id%7D%2Fdelete |
||
184 | * |
||
185 | * @return Status|ResponseInterface |
||
186 | */ |
||
187 | 11 | public function removeTeamMember(string $teamId, string $userId) |
|
197 | |||
198 | /** |
||
199 | * Return the list of public channels in the given team. |
||
200 | * |
||
201 | * @param string $teamId The team ID |
||
202 | * @param array $params The listing params, 'page', 'per_page' |
||
203 | * |
||
204 | * @return Channels|ResponseInterface |
||
205 | */ |
||
206 | 9 | public function getTeamPublicChannels(string $teamId, array $params = []) |
|
216 | |||
217 | /** |
||
218 | * Retrieve the team statistics. |
||
219 | * |
||
220 | * @param string $teamId The Team ID |
||
221 | * |
||
222 | * @return TeamStats|ResponseInterface |
||
223 | */ |
||
224 | 9 | public function getTeamStats($teamId) |
|
234 | |||
235 | /** |
||
236 | * Patch a team. |
||
237 | * |
||
238 | * @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1patch%2Fput |
||
239 | * |
||
240 | * @param string $teamId |
||
241 | * @param array $params |
||
242 | * |
||
243 | * @return Team|ResponseInterface |
||
244 | */ |
||
245 | 9 | public function patchTeam(string $teamId, array $params) |
|
255 | |||
256 | /** |
||
257 | * Update a team. |
||
258 | * |
||
259 | * @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D%2Fput |
||
260 | * |
||
261 | * @param string $teamId |
||
262 | * @param array $params, required paramaters: display_name, description, company_name, allowed_domains, invite_id, allow_open_invite |
||
263 | * |
||
264 | * @return Team|ResponseInterface |
||
265 | */ |
||
266 | 9 | public function updateTeam(string $teamId, array $params) |
|
276 | } |
||
277 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.