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 |
||
16 | final class ChannelsApi extends HttpApi |
||
17 | { |
||
18 | /** |
||
19 | * Returns an channel by its ID. |
||
20 | * |
||
21 | * @param string $channelId |
||
22 | * |
||
23 | * @return Channel|ResponseInterface |
||
24 | */ |
||
25 | 9 | View Code Duplication | public function getChannelById(string $channelId) |
35 | |||
36 | /** |
||
37 | * Returns an channel by its ID. |
||
38 | * |
||
39 | * @param string $teamId |
||
40 | * @param string $channelName |
||
41 | * |
||
42 | * @return Channel|ResponseInterface |
||
43 | */ |
||
44 | 11 | public function getChannelByName(string $teamId, string $channelName) |
|
54 | |||
55 | /** |
||
56 | * Returns an channel by its name. |
||
57 | * |
||
58 | * @param string $teamName |
||
59 | * @param string $channelName |
||
60 | * |
||
61 | * @return Channel|ResponseInterface |
||
62 | */ |
||
63 | 11 | public function getChannelByNameAndTeamName($teamName, $channelName) |
|
73 | |||
74 | /** |
||
75 | * Retrieve the channel statistics. |
||
76 | * |
||
77 | * @param string $channelId The Team ID |
||
78 | * |
||
79 | * @return ChannelStats|ResponseInterface |
||
80 | */ |
||
81 | 9 | public function getChannelStats($channelId) |
|
91 | |||
92 | /** |
||
93 | * Create a Channel. Required parameters: 'team_id', 'name', 'display_name' and 'type'. |
||
94 | * |
||
95 | * @param array $params |
||
96 | * |
||
97 | * @return Channel|ResponseInterface |
||
98 | */ |
||
99 | 8 | public function createChannel(array $params) |
|
105 | |||
106 | /** |
||
107 | * Delete a Channel. |
||
108 | * |
||
109 | * @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D%2Fdelete |
||
110 | * |
||
111 | * @param string $channelId |
||
112 | * |
||
113 | * @return Status |
||
114 | */ |
||
115 | 9 | public function deleteChannel($channelId) |
|
125 | |||
126 | /** |
||
127 | * Restore channel from the provided channel id string. |
||
128 | * |
||
129 | * @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D~1restore%2Fpost |
||
130 | * |
||
131 | * @param string $channelId |
||
132 | * |
||
133 | * @return Channel|ResponseInterface |
||
134 | */ |
||
135 | 9 | View Code Duplication | public function restoreChannel($channelId) |
145 | |||
146 | /** |
||
147 | * Patch a channel. |
||
148 | * |
||
149 | * @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D~1patch%2Fput |
||
150 | * |
||
151 | * @param string $channelId |
||
152 | * @param array $params |
||
153 | * |
||
154 | * @return Channel|ResponseInterface |
||
155 | */ |
||
156 | 9 | public function patchChannel(string $channelId, array $params) |
|
166 | |||
167 | /** |
||
168 | * Update a channel. |
||
169 | * |
||
170 | * @see https://api.mattermost.com/v4/#tag/channels%2Fpaths%2F~1channels~1%7Bchannel_id%7D%2Fput |
||
171 | * |
||
172 | * @param string $channelId |
||
173 | * @param array $params |
||
174 | * |
||
175 | * @return Channel|ResponseInterface |
||
176 | */ |
||
177 | 9 | public function updateChannel(string $channelId, array $params) |
|
187 | |||
188 | /** |
||
189 | * Add a user to a channel, with specific roles. |
||
190 | * |
||
191 | * @param string $channelId |
||
192 | * @param string $userId |
||
193 | * @param string $roles |
||
194 | * |
||
195 | * @return ChannelMember|ResponseInterface |
||
196 | */ |
||
197 | 11 | View Code Duplication | public function addChannelMember(string $channelId, string $userId, string $roles = '') |
213 | |||
214 | /** |
||
215 | * Remove a user from a channel. |
||
216 | * |
||
217 | * @param string $channelId |
||
218 | * @param string $userId |
||
219 | * |
||
220 | * @return Status|ResponseInterface |
||
221 | */ |
||
222 | 11 | public function removeChannelMember(string $channelId, string $userId) |
|
232 | |||
233 | /** |
||
234 | * Get members of a channel. |
||
235 | * |
||
236 | * @param string $channelId |
||
237 | * @param array $params The listing params, 'page', 'per_page' |
||
238 | * |
||
239 | * @return ChannelMembers|ResponseInterface |
||
240 | */ |
||
241 | 9 | public function getChannelMembers(string $channelId, array $params = []) |
|
251 | |||
252 | /** |
||
253 | * Get the posts for a channel. |
||
254 | * |
||
255 | * @param string $channelId |
||
256 | * @param array $params The listing params: 'page', 'per_page', 'before', 'after', 'since' |
||
257 | * |
||
258 | * @see: https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1channels~1%7Bchannel_id%7D~1posts%2Fget |
||
259 | * |
||
260 | * @return Posts|ResponseInterface |
||
261 | */ |
||
262 | 10 | public function getChannelPosts(string $channelId, array $params = []) |
|
272 | } |
||
273 |
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.