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 | public $bcastRoles = array(); |
||
17 | public $useBasicAuth = false; |
||
18 | public $basicUser = 'admin'; |
||
19 | public $basicPwd = '1234'; |
||
20 | |||
21 | public function __construct() |
||
25 | |||
26 | /** |
||
27 | * Make the request and analyze the result |
||
28 | * |
||
29 | * @param string $type Request method |
||
30 | * @param string $endpoint Api request endpoint |
||
31 | * @param array $params Parameters |
||
32 | * @return array|false Array with data or error, or False when something went fully wrong |
||
33 | */ |
||
34 | |||
35 | protected function doRequest($type, $endpoint, $params=array()) |
||
79 | |||
80 | |||
81 | /** |
||
82 | * Get all registered users |
||
83 | * |
||
84 | * @return json|false Json with data or error, or False when something went fully wrong |
||
85 | */ |
||
86 | public function getUsers() |
||
91 | |||
92 | |||
93 | /** |
||
94 | * Get information for a specified user |
||
95 | * |
||
96 | * @return json|false Json with data or error, or False when something went fully wrong |
||
97 | */ |
||
98 | public function getUser($username) |
||
103 | |||
104 | |||
105 | /** |
||
106 | * Creates a new OpenFire user |
||
107 | * |
||
108 | * @param string $username Username |
||
109 | * @param string $password Password |
||
110 | * @param string|false $name Name (Optional) |
||
111 | * @param string|false $email Email (Optional) |
||
112 | * @param string[]|false $groups Groups (Optional) |
||
113 | * @return json|false Json with data or error, or False when something went fully wrong |
||
114 | */ |
||
115 | public function addUser($username, $password, $name=false, $email=false, $groups=false) |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Deletes an OpenFire user |
||
124 | * |
||
125 | * @param string $username Username |
||
126 | * @return json|false Json with data or error, or False when something went fully wrong |
||
127 | */ |
||
128 | public function deleteUser($username) |
||
133 | |||
134 | /** |
||
135 | * Updates an OpenFire user |
||
136 | * |
||
137 | * @param string $username Username |
||
138 | * @param string|false $password Password (Optional) |
||
139 | * @param string|false $name Name (Optional) |
||
140 | * @param string|false $email Email (Optional) |
||
141 | * @param string[]|false $groups Groups (Optional) |
||
142 | * @return json|false Json with data or error, or False when something went fully wrong |
||
143 | */ |
||
144 | public function updateUser($username, $password, $name=false, $email=false, $groups=false) |
||
149 | |||
150 | /** |
||
151 | * locks/Disables an OpenFire user |
||
152 | * |
||
153 | * @param string $username Username |
||
154 | * @return json|false Json with data or error, or False when something went fully wrong |
||
155 | */ |
||
156 | public function lockoutUser($username) |
||
161 | |||
162 | |||
163 | /** |
||
164 | * unlocks an OpenFire user |
||
165 | * |
||
166 | * @param string $username Username |
||
167 | * @return json|false Json with data or error, or False when something went fully wrong |
||
168 | */ |
||
169 | public function unlockUser($username) |
||
174 | |||
175 | |||
176 | /** |
||
177 | * Adds to this OpenFire user's roster |
||
178 | * |
||
179 | * @param string $username Username |
||
180 | * @param string $jid JID |
||
181 | * @param string|false $name Name (Optional) |
||
182 | * @param int|false $subscriptionType Subscription (Optional) |
||
183 | * @return json|false Json with data or error, or False when something went fully wrong |
||
184 | */ |
||
185 | public function addToRoster($username, $jid, $name=false, $subscriptionType=false) |
||
190 | |||
191 | |||
192 | /** |
||
193 | * Removes from this OpenFire user's roster |
||
194 | * |
||
195 | * @param string $username Username |
||
196 | * @param string $jid JID |
||
197 | * @return json|false Json with data or error, or False when something went fully wrong |
||
198 | */ |
||
199 | public function deleteFromRoster($username, $jid) |
||
204 | |||
205 | /** |
||
206 | * Updates this OpenFire user's roster |
||
207 | * |
||
208 | * @param string $username Username |
||
209 | * @param string $jid JID |
||
210 | * @param string|false $nickname Nick Name (Optional) |
||
211 | * @param int|false $subscriptionType Subscription (Optional) |
||
212 | * @return json|false Json with data or error, or False when something went fully wrong |
||
213 | */ |
||
214 | public function updateRoster($username, $jid, $nickname=false, $subscriptionType=false) |
||
219 | |||
220 | /** |
||
221 | * Get all groups |
||
222 | * |
||
223 | * @return json|false Json with data or error, or False when something went fully wrong |
||
224 | */ |
||
225 | public function getGroups() |
||
230 | |||
231 | /** |
||
232 | * Retrieve a group |
||
233 | * |
||
234 | * @param string $name Name of group |
||
235 | * @return json|false Json with data or error, or False when something went fully wrong |
||
236 | */ |
||
237 | public function getGroup($name) |
||
242 | |||
243 | /** |
||
244 | * Create a group |
||
245 | * |
||
246 | * @param string $name Name of the group |
||
247 | * @param string $description Some description of the group |
||
248 | * |
||
249 | * @return json|false Json with data or error, or False when something went fully wrong |
||
250 | */ |
||
251 | public function createGroup($name, $description = false) |
||
256 | |||
257 | /** |
||
258 | * Delete a group |
||
259 | * |
||
260 | * @param string $name Name of the Group to delete |
||
261 | * @return json|false Json with data or error, or False when something went fully wrong |
||
262 | */ |
||
263 | public function deleteGroup($name) |
||
268 | |||
269 | /** |
||
270 | * Update a group (description) |
||
271 | * |
||
272 | * @param string $name Name of group |
||
273 | * @param string $description Some description of the group |
||
274 | * |
||
275 | */ |
||
276 | public function updateGroup($name, $description) |
||
281 | |||
282 | /** |
||
283 | * Gell all active sessions |
||
284 | * |
||
285 | * @return json|false Json with data or error, or False when something went fully wrong |
||
286 | */ |
||
287 | public function getSessions() |
||
292 | |||
293 | public function getChatRoom($name) |
||
297 | |||
298 | public function getAllChatRooms() |
||
302 | |||
303 | public function createChatRoom($naturalName, $roomName, $description, $maxUsers = '30', $persistent = 'false', $publicRoom = 'false') |
||
308 | |||
309 | public function deleteChatRoom($name) |
||
313 | } |
||
314 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.