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 |
||
14 | final class UsersApi extends HttpApi |
||
15 | { |
||
16 | /** |
||
17 | * @param string $loginId The login Id |
||
18 | * @param string $password The password |
||
19 | * @param null $token The login token, as output variable |
||
20 | * |
||
21 | * @return User|ResponseInterface |
||
22 | */ |
||
23 | 10 | public function login($loginId, $password, &$token = null) |
|
46 | |||
47 | /** |
||
48 | * Returns an user by its ID, use "me" to get the current logged in user. |
||
49 | * |
||
50 | * @param string $userId User GUID |
||
51 | * |
||
52 | * @return User|ResponseInterface |
||
53 | */ |
||
54 | 9 | public function getUserById(string $userId) |
|
64 | |||
65 | /** |
||
66 | * Get a list of teams that a user is on. |
||
67 | * |
||
68 | * @param string $userId User GUID |
||
69 | * |
||
70 | * @return User|ResponseInterface |
||
71 | */ |
||
72 | 9 | View Code Duplication | public function getUserTeams(string $userId) |
82 | |||
83 | /** |
||
84 | * Returns a collection of users matching the given IDs. |
||
85 | * |
||
86 | * @param array $userIds |
||
87 | * |
||
88 | * @return Users|ResponseInterface |
||
89 | */ |
||
90 | 9 | View Code Duplication | public function getUsersByIds(array $userIds) |
100 | |||
101 | /** |
||
102 | * Returns a collection of users. |
||
103 | * |
||
104 | * @param array $params The listing params, 'page', 'per_page', 'in_channel', 'in_team', 'not_in_channel' |
||
105 | * |
||
106 | * @return Users|ResponseInterface |
||
107 | */ |
||
108 | 9 | public function getUsers(array $params = []) |
|
114 | |||
115 | /** |
||
116 | * Returns a user given its email. |
||
117 | * |
||
118 | * @param string $email |
||
119 | * |
||
120 | * @return User|ResponseInterface |
||
121 | */ |
||
122 | 9 | public function getUserByEmail(string $email) |
|
132 | |||
133 | /** |
||
134 | * Returns a collection of users matching the given usernames. |
||
135 | * |
||
136 | * @param array $userNames |
||
137 | * |
||
138 | * @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1usernames%2Fpost |
||
139 | * |
||
140 | * @return Users|ResponseInterface |
||
141 | */ |
||
142 | 9 | View Code Duplication | public function getUsersByUsernames(array $userNames) |
152 | |||
153 | /** |
||
154 | * Update user active or inactive status. |
||
155 | * |
||
156 | * @param string $userId The user ID |
||
157 | * @param bool $activeStatus Use `true` to set the user active, `false` for inactive |
||
158 | * |
||
159 | * @return Status|ResponseInterface |
||
160 | */ |
||
161 | 9 | View Code Duplication | public function setUserActive(string $userId, bool $activeStatus) |
173 | |||
174 | /** |
||
175 | * Deactivates the user by archiving its user object. |
||
176 | * |
||
177 | * @param string $userId The user GUID |
||
178 | * |
||
179 | * @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1%7Buser_id%7D%2Fdelete |
||
180 | * |
||
181 | * @return Status|ResponseInterface |
||
182 | */ |
||
183 | 9 | public function deactivateUser(string $userId) |
|
193 | |||
194 | /** |
||
195 | * Update a user's system-level roles. |
||
196 | * Valid user roles are "system_user", "system_admin" or both of them. |
||
197 | * Overwrites any previously assigned system-level roles. |
||
198 | * |
||
199 | * @param string $userId The user GUID |
||
200 | * @param string $roles Space-delimited system roles to assign to the user |
||
201 | * |
||
202 | * @return Status|ResponseInterface |
||
203 | */ |
||
204 | 9 | View Code Duplication | public function updateUserRoles(string $userId, string $roles) |
216 | |||
217 | /** |
||
218 | * Create a user. Required parameters: 'username', 'email' and 'password'. |
||
219 | * |
||
220 | * @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users%2Fpost |
||
221 | * |
||
222 | * @param array $params |
||
223 | * |
||
224 | * @return User|ResponseInterface |
||
225 | */ |
||
226 | 8 | public function createUser(array $params) |
|
232 | |||
233 | /** |
||
234 | * Patch a user. |
||
235 | * |
||
236 | * @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1%7Buser_id%7D~1patch%2Fput |
||
237 | * |
||
238 | * @param string $userId |
||
239 | * @param array $params |
||
240 | * |
||
241 | * @return User|ResponseInterface |
||
242 | */ |
||
243 | 9 | public function patchUser(string $userId, array $params) |
|
253 | |||
254 | /** |
||
255 | * Update a user, required parameter: 'id'. |
||
256 | * |
||
257 | * @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1%7Buser_id%7D%2Fput |
||
258 | * |
||
259 | * @param string $userId |
||
260 | * @param array $params |
||
261 | * |
||
262 | * @return User|ResponseInterface |
||
263 | */ |
||
264 | 9 | public function updateUser(string $userId, array $params) |
|
274 | |||
275 | /** |
||
276 | * Update a user's password. New password must meet password policy set by server configuration. |
||
277 | * |
||
278 | * @param string $userId User GUID |
||
279 | * @param string $currentPassword The current password for the user |
||
280 | * @param string $newPassword The new password for the user |
||
281 | * |
||
282 | * @return Status|ResponseInterface |
||
283 | */ |
||
284 | 11 | public function updateUserPassword(string $userId, string $currentPassword, string $newPassword) |
|
300 | |||
301 | /** |
||
302 | * Returns an user by its username, use "me" to get the current logged in user. |
||
303 | * |
||
304 | * @param string $username |
||
305 | * |
||
306 | * @return User|ResponseInterface |
||
307 | */ |
||
308 | 9 | public function getUserByUsername(string $username) |
|
318 | } |
||
319 |
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.