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 |
||
7 | class OpenFireRestApi |
||
8 | { |
||
9 | public $host = 'localhost'; |
||
10 | public $port = '9090'; |
||
11 | public $plugin = '/plugins/restapi/v1'; |
||
12 | public $secret = 'SuperSecret'; |
||
13 | public $useSSL = false; |
||
14 | protected $params = array(); |
||
15 | public $client; |
||
16 | |||
17 | public function __construct() |
||
21 | |||
22 | /** |
||
23 | * Make the request and analyze the result |
||
24 | * |
||
25 | * @param string $type Request method |
||
26 | * @param string $endpoint Api request endpoint |
||
27 | * @param array $params Parameters |
||
28 | * @return array|false Array with data or error, or False when something went fully wrong |
||
29 | */ |
||
30 | |||
31 | protected function doRequest($type, $endpoint, $params=array()) |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Get all registered users |
||
73 | * |
||
74 | * @return json|false Json with data or error, or False when something went fully wrong |
||
75 | */ |
||
76 | public function getUsers() |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Get information for a specified user |
||
85 | * |
||
86 | * @return json|false Json with data or error, or False when something went fully wrong |
||
87 | */ |
||
88 | public function getUser($username) |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Creates a new OpenFire user |
||
97 | * |
||
98 | * @param string $username Username |
||
99 | * @param string $password Password |
||
100 | * @param string|false $name Name (Optional) |
||
101 | * @param string|false $email Email (Optional) |
||
102 | * @param string[]|false $groups Groups (Optional) |
||
103 | * @return json|false Json with data or error, or False when something went fully wrong |
||
104 | */ |
||
105 | public function addUser($username, $password, $name=false, $email=false, $groups=false) |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Deletes an OpenFire user |
||
114 | * |
||
115 | * @param string $username Username |
||
116 | * @return json|false Json with data or error, or False when something went fully wrong |
||
117 | */ |
||
118 | public function deleteUser($username) |
||
123 | |||
124 | /** |
||
125 | * Updates an OpenFire user |
||
126 | * |
||
127 | * @param string $username Username |
||
128 | * @param string|false $password Password (Optional) |
||
129 | * @param string|false $name Name (Optional) |
||
130 | * @param string|false $email Email (Optional) |
||
131 | * @param string[]|false $groups Groups (Optional) |
||
132 | * @return json|false Json with data or error, or False when something went fully wrong |
||
133 | */ |
||
134 | public function updateUser($username, $password, $name=false, $email=false, $groups=false) |
||
139 | |||
140 | /** |
||
141 | * locks/Disables an OpenFire user |
||
142 | * |
||
143 | * @param string $username Username |
||
144 | * @return json|false Json with data or error, or False when something went fully wrong |
||
145 | */ |
||
146 | public function lockoutUser($username) |
||
151 | |||
152 | |||
153 | /** |
||
154 | * unlocks an OpenFire user |
||
155 | * |
||
156 | * @param string $username Username |
||
157 | * @return json|false Json with data or error, or False when something went fully wrong |
||
158 | */ |
||
159 | public function unlockUser($username) |
||
164 | |||
165 | |||
166 | /** |
||
167 | * Adds to this OpenFire user's roster |
||
168 | * |
||
169 | * @param string $username Username |
||
170 | * @param string $jid JID |
||
171 | * @param string|false $name Name (Optional) |
||
172 | * @param int|false $subscriptionType Subscription (Optional) |
||
173 | * @return json|false Json with data or error, or False when something went fully wrong |
||
174 | */ |
||
175 | public function addToRoster($username, $jid, $name=false, $subscriptionType=false) |
||
180 | |||
181 | |||
182 | /** |
||
183 | * Removes from this OpenFire user's roster |
||
184 | * |
||
185 | * @param string $username Username |
||
186 | * @param string $jid JID |
||
187 | * @return json|false Json with data or error, or False when something went fully wrong |
||
188 | */ |
||
189 | public function deleteFromRoster($username, $jid) |
||
194 | |||
195 | /** |
||
196 | * Updates this OpenFire user's roster |
||
197 | * |
||
198 | * @param string $username Username |
||
199 | * @param string $jid JID |
||
200 | * @param string|false $nickname Nick Name (Optional) |
||
201 | * @param int|false $subscriptionType Subscription (Optional) |
||
202 | * @return json|false Json with data or error, or False when something went fully wrong |
||
203 | */ |
||
204 | public function updateRoster($username, $jid, $nickname=false, $subscriptionType=false) |
||
209 | |||
210 | /** |
||
211 | * Get all groups |
||
212 | * |
||
213 | * @return json|false Json with data or error, or False when something went fully wrong |
||
214 | */ |
||
215 | public function getGroups() |
||
220 | |||
221 | /** |
||
222 | * Retrieve a group |
||
223 | * |
||
224 | * @param string $name Name of group |
||
225 | * @return json|false Json with data or error, or False when something went fully wrong |
||
226 | */ |
||
227 | public function getGroup($name) |
||
232 | |||
233 | /** |
||
234 | * Create a group |
||
235 | * |
||
236 | * @param string $name Name of the group |
||
237 | * @param string $description Some description of the group |
||
238 | * |
||
239 | * @return json|false Json with data or error, or False when something went fully wrong |
||
240 | */ |
||
241 | public function createGroup($name, $description = false) |
||
246 | |||
247 | /** |
||
248 | * Delete a group |
||
249 | * |
||
250 | * @param string $name Name of the Group to delete |
||
251 | * @return json|false Json with data or error, or False when something went fully wrong |
||
252 | */ |
||
253 | public function deleteGroup($name) |
||
258 | |||
259 | /** |
||
260 | * Update a group (description) |
||
261 | * |
||
262 | * @param string $name Name of group |
||
263 | * @param string $description Some description of the group |
||
264 | * |
||
265 | */ |
||
266 | public function updateGroup($name, $description) |
||
271 | |||
272 | /** |
||
273 | * Gell all active sessions |
||
274 | * |
||
275 | * @return json|false Json with data or error, or False when something went fully wrong |
||
276 | */ |
||
277 | public function getSessions() |
||
282 | |||
283 | public function getChatRoom($name) |
||
287 | |||
288 | public function createChatRoom($naturalName, $roomName, $description) |
||
292 | |||
293 | public function deleteChatRoom($name) |
||
297 | } |
||
298 |
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.