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 |
||
37 | class CirclesRequest extends CirclesRequestBuilder { |
||
38 | |||
39 | |||
40 | /** |
||
41 | * forceGetCircle(); |
||
42 | * |
||
43 | * returns data of a circle from its Id. |
||
44 | * |
||
45 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
46 | * In case of interaction with users, Please use getCircle() instead. |
||
47 | * |
||
48 | * @param string $circleUniqueId |
||
49 | * |
||
50 | * @return Circle |
||
51 | * @throws CircleDoesNotExistException |
||
52 | */ |
||
53 | View Code Duplication | public function forceGetCircle($circleUniqueId) { |
|
71 | |||
72 | |||
73 | /** |
||
74 | * forceGetCircles(); |
||
75 | * |
||
76 | * returns data of a all circles. |
||
77 | * |
||
78 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
79 | * In case of interaction with users, Please use getCircles() instead. |
||
80 | * |
||
81 | * @param string $ownerId |
||
82 | * |
||
83 | * @return Circle[] |
||
84 | */ |
||
85 | View Code Duplication | public function forceGetCircles(string $ownerId = '') { |
|
98 | |||
99 | |||
100 | /** |
||
101 | * forceGetCircleByName(); |
||
102 | * |
||
103 | * returns data of a circle from its Name. |
||
104 | * |
||
105 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
106 | * In case of interaction with users, do not use this method. |
||
107 | * |
||
108 | * @param $name |
||
109 | * |
||
110 | * @return null|Circle |
||
111 | * @throws CircleDoesNotExistException |
||
112 | */ |
||
113 | View Code Duplication | public function forceGetCircleByName($name) { |
|
131 | |||
132 | |||
133 | /** |
||
134 | * forceGetCircleByGroupId(); |
||
135 | * |
||
136 | * returns data of a circle from its Group ID. |
||
137 | * |
||
138 | * WARNING: This function does not filters data regarding the current user/viewer. |
||
139 | * In case of interaction with users, do not use this method. |
||
140 | * |
||
141 | * @param $groupId |
||
142 | * |
||
143 | * @return null|Circle |
||
144 | */ |
||
145 | public function forceGetCircleByGroupId($groupId) { |
||
162 | |||
163 | /** |
||
164 | * @param string $userId |
||
165 | * @param int $type |
||
166 | * @param string $name |
||
167 | * @param int $level |
||
168 | * @param bool $forceAll |
||
169 | * @param string $ownerId |
||
170 | * |
||
171 | * @return Circle[] |
||
172 | * @throws ConfigNoCircleAvailableException |
||
173 | */ |
||
174 | public function getCircles($userId, $type = 0, $name = '', $level = 0, $forceAll = false, string $ownerId = '') { |
||
200 | |||
201 | |||
202 | /** |
||
203 | * |
||
204 | * @param string $circleUniqueId |
||
205 | * @param string $viewerId |
||
206 | * @param bool $forceAll |
||
207 | * |
||
208 | * @return Circle |
||
209 | * @throws CircleDoesNotExistException |
||
210 | * @throws ConfigNoCircleAvailableException |
||
211 | */ |
||
212 | public function getCircle($circleUniqueId, $viewerId, $forceAll = false) { |
||
238 | |||
239 | |||
240 | /** |
||
241 | * createCircle(); |
||
242 | * |
||
243 | * Create a circle with $userId as its owner. |
||
244 | * Will returns the circle |
||
245 | * |
||
246 | * @param Circle $circle |
||
247 | * @param $userId |
||
248 | * |
||
249 | * @throws CircleAlreadyExistsException |
||
250 | */ |
||
251 | public function createCircle(Circle &$circle, $userId) { |
||
277 | |||
278 | |||
279 | /** |
||
280 | * remove a circle |
||
281 | * |
||
282 | * @param string $circleUniqueId |
||
283 | */ |
||
284 | public function destroyCircle($circleUniqueId) { |
||
289 | |||
290 | |||
291 | /** |
||
292 | * returns if the circle is already in database |
||
293 | * |
||
294 | * @param Circle $circle |
||
295 | * @param string $userId |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | private function isCircleUnique(Circle $circle, $userId) { |
||
319 | |||
320 | |||
321 | /** |
||
322 | * return if the personal circle is unique |
||
323 | * |
||
324 | * @param Circle $circle |
||
325 | * @param string $userId |
||
326 | * |
||
327 | * @return bool |
||
328 | */ |
||
329 | private function isPersonalCircleUnique(Circle $circle, $userId) { |
||
345 | |||
346 | |||
347 | /** |
||
348 | * @param Circle $circle |
||
349 | * @param string $userId |
||
350 | * |
||
351 | * @throws CircleAlreadyExistsException |
||
352 | */ |
||
353 | public function updateCircle(Circle $circle, $userId) { |
||
369 | |||
370 | |||
371 | /** |
||
372 | * @param string $uniqueId |
||
373 | * |
||
374 | * @return Circle |
||
375 | * @throws CircleDoesNotExistException |
||
376 | */ |
||
377 | View Code Duplication | public function getCircleFromUniqueId($uniqueId) { |
|
393 | |||
394 | |||
395 | /** |
||
396 | * @param int $addressBookId |
||
397 | * |
||
398 | * @return array |
||
399 | */ |
||
400 | View Code Duplication | public function getFromBook(int $addressBookId) { |
|
413 | |||
414 | |||
415 | /** |
||
416 | * @param int $addressBookId |
||
417 | * |
||
418 | * @return Circle[] |
||
419 | */ |
||
420 | View Code Duplication | public function getFromContactBook(int $addressBookId): array { |
|
433 | |||
434 | |||
435 | /** |
||
436 | * @param int $addressBookId |
||
437 | * @param string $group |
||
438 | * |
||
439 | * @return Circle |
||
440 | * @throws CircleDoesNotExistException |
||
441 | */ |
||
442 | public function getFromContactGroup(int $addressBookId, string $group): Circle { |
||
457 | |||
458 | } |
||
459 |
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.