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 | public function getTeamById($id) |
|
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 | * Remove a team member. |
||
160 | * |
||
161 | * @param string $teamId The team ID |
||
162 | * @param string $userId The user ID |
||
163 | * |
||
164 | * https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members~1%7Buser_id%7D%2Fdelete |
||
165 | * |
||
166 | * @return Status|ResponseInterface |
||
167 | */ |
||
168 | 11 | public function removeTeamMember(string $teamId, string $userId) |
|
178 | |||
179 | /** |
||
180 | * Return the list of public channels in the given team. |
||
181 | * |
||
182 | * @param string $teamId The team ID |
||
183 | * @param array $params The listing params, 'page', 'per_page' |
||
184 | * |
||
185 | * @return Channels|ResponseInterface |
||
186 | */ |
||
187 | 9 | public function getTeamPublicChannels(string $teamId, array $params = []) |
|
197 | |||
198 | /** |
||
199 | * Retrieve the team statistics. |
||
200 | * |
||
201 | * @param string $teamId The Team ID |
||
202 | * |
||
203 | * @return TeamStats|ResponseInterface |
||
204 | */ |
||
205 | 9 | public function getTeamStats($teamId) |
|
215 | |||
216 | /** |
||
217 | * Patch a team. |
||
218 | * |
||
219 | * @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1patch%2Fput |
||
220 | * |
||
221 | * @param string $teamId |
||
222 | * @param array $params |
||
223 | * |
||
224 | * @return Team|ResponseInterface |
||
225 | */ |
||
226 | 9 | public function patchTeam(string $teamId, array $params) |
|
236 | |||
237 | /** |
||
238 | * Update a team. |
||
239 | * |
||
240 | * @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D%2Fput |
||
241 | * |
||
242 | * @param string $teamId |
||
243 | * @param array $params, required paramaters: display_name, description, company_name, allowed_domains, invite_id, allow_open_invite |
||
244 | * |
||
245 | * @return Team|ResponseInterface |
||
246 | */ |
||
247 | 9 | public function updateTeam(string $teamId, array $params) |
|
257 | } |
||
258 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: