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 |
||
11 | trait RCONConnection |
||
12 | { |
||
13 | /** |
||
14 | * Send a alert to a online user. |
||
15 | * @param string $message The message to send. |
||
16 | * @param int $user_id The id of the user to alert. |
||
17 | * @return JsonResponse |
||
18 | */ |
||
19 | public static function alertUser(string $message, int $user_id) |
||
29 | |||
30 | /** |
||
31 | * Change room owner. |
||
32 | * @param int $room_id The id of the room to change. |
||
33 | * @param int $user_id The new user id of the room. |
||
34 | * @param string $username The new owner name of the room. |
||
35 | * @return JsonResponse |
||
36 | */ |
||
37 | public static function changeRoomOwner(int $room_id, int $user_id, string $username) |
||
48 | |||
49 | /** |
||
50 | * Create mod ticket. |
||
51 | * @param string $message The message that was included in the report. |
||
52 | * @param int $reported_id User id of the reported player. |
||
53 | * @param int $reported_room_id The room that was reported. |
||
54 | * @param string $reported_username Username of the reported player. |
||
55 | * @param int $sender_id User id who created the ticket. |
||
56 | * @param string $sender_username Username who created the ticket. |
||
57 | * @return JsonResponse |
||
58 | */ |
||
59 | public static function createModToolTicket( |
||
79 | |||
80 | /** |
||
81 | * Disconnect a user. |
||
82 | * @param string $username Optional user id of the player to disconnect. |
||
83 | * @param int|null $user_id Optional username of the player to disconnect. |
||
84 | * @return JsonResponse |
||
85 | */ |
||
86 | View Code Duplication | public static function disconnect(string $username = null, int $user_id = null) |
|
97 | |||
98 | /** |
||
99 | * Execute a command through user id. |
||
100 | * @param string $command Complete string including the semicolon at the start for the command to execute. |
||
101 | * @param int $user_id User id of the player to execute the command for. |
||
102 | * @return JsonResponse |
||
103 | */ |
||
104 | public static function executeCommand(string $command, int $user_id) |
||
114 | |||
115 | /** |
||
116 | * Forward an user into a selected room. |
||
117 | * @param int $room_id Room id of the room to forward the player to. |
||
118 | * @param int $user_id User id of the player to forward. |
||
119 | * @return JsonResponse |
||
120 | */ |
||
121 | public static function forwardUser(int $room_id, int $user_id) |
||
131 | |||
132 | /** |
||
133 | * Send a friend request. |
||
134 | * @param int $target_id User id of the player that receives a friend request. |
||
135 | * @param int $user_id User id of the player that creates a friend request. |
||
136 | * @return JsonResponse |
||
137 | */ |
||
138 | public static function sendFriendRequest(int $target_id, int $user_id) |
||
148 | |||
149 | /** |
||
150 | * Give a badge to an user. |
||
151 | * @param string $badge Badge code of the badge that the player will receive. |
||
152 | * @param int $user_id User id of the player that receives a badge. |
||
153 | * @return JsonResponse |
||
154 | */ |
||
155 | public static function giveBadge(string $badge, int $user_id) |
||
165 | |||
166 | /** |
||
167 | * Give clothes to an user. |
||
168 | * @param int $clothing_id The clothing ID |
||
169 | * @param int $user_id The id of the user to alert. |
||
170 | * @return JsonResponse |
||
171 | */ |
||
172 | public static function giveClothing(int $clothing_id, int $user_id) |
||
182 | |||
183 | /** |
||
184 | * Give credits to an user. |
||
185 | * @param int $credits Amount of credits that the player will be awarded. |
||
186 | * @param int $user_id User id of the player that will receive credits. |
||
187 | * @return JsonResponse |
||
188 | */ |
||
189 | public static function giveCredits(int $credits, int $user_id) |
||
199 | |||
200 | /** |
||
201 | * Give pixels to an user. |
||
202 | * @param int $pixels Amount of pixels that the player will be awarded. |
||
203 | * @param int $user_id User id of the player that will receive the pixels. |
||
204 | * @return JsonResponse |
||
205 | */ |
||
206 | public static function givePixels(int $pixels, int $user_id) |
||
216 | |||
217 | /** |
||
218 | * @param int $points Amount of points that the player will be awarded. |
||
219 | * @param int $type The type of points that the player will be awarded. |
||
220 | * @param int $user_id User id of the player that will be awarded. |
||
221 | * @return JsonResponse |
||
222 | */ |
||
223 | public static function givePoints(int $points, int $type, int $user_id) |
||
234 | |||
235 | /** |
||
236 | * Give respects to someone. |
||
237 | * @param int $daily_respects Amount that will be added to the current daily respect points. |
||
238 | * @param int $respect_given Amount that will be counted towards given respect statistic. |
||
239 | * @param int $respect_received Amount that will be counted towards received respect statistic. |
||
240 | * @param int $user_id User id of the player that will receive the respect. |
||
241 | * @return JsonResponse |
||
242 | */ |
||
243 | public static function giveRespect(int $daily_respects, int $respect_given, int $respect_received, int $user_id) |
||
255 | |||
256 | /** |
||
257 | * Send an alert to everyone online. |
||
258 | * @param string $message The message to display to all users online. |
||
259 | * @param string $url The link to include. |
||
260 | * @return JsonResponse |
||
261 | */ |
||
262 | View Code Duplication | public static function hotelAlert(string $message, string $url = null) |
|
275 | |||
276 | /** |
||
277 | * Ignore an user. |
||
278 | * @param int $target_id User id of the player that will be ignored |
||
279 | * @param int $user_id User id of the player that will ignore target_id. |
||
280 | * @return JsonResponse |
||
281 | */ |
||
282 | public static function ignoreUser(int $target_id, int $user_id) |
||
292 | |||
293 | /** |
||
294 | * Send an image alert to an user. |
||
295 | * @param string $bubble_key Key of the bubble alert to define extra parameters in the external_flash_texts. |
||
296 | * @param string $display_type Display type |
||
297 | * @param string $image Image to display. |
||
298 | * @param string $message The message parameter that needs to be set. |
||
299 | * @param string $title Title of the popup window. |
||
300 | * @param string $url The url parameter that needs to be set. |
||
301 | * @param string $url_message The message of the url parameter that needs to be set. |
||
302 | * @param int $user_id The user id of the user that will receive the alert |
||
303 | * @return JsonResponse |
||
304 | */ |
||
305 | View Code Duplication | public static function imageAlertUser(string $bubble_key, string $display_type, string $image, string $message, string $title, string $url, string $url_message, int $user_id) |
|
321 | |||
322 | /** |
||
323 | * Send an alert to all online users. |
||
324 | * @param string $bubble_key Key of the bubble alert to define extra parameters in the external_flash_texts. |
||
325 | * @param string $display_type Display type |
||
326 | * @param string $image Image to display. |
||
327 | * @param string $message The message parameter that needs to be set. |
||
328 | * @param string $title Title of the popup window. |
||
329 | * @param string $url The url parameter that needs to be set. |
||
330 | * @param string $url_message The message of the url parameter that needs to be set. |
||
331 | * @return JsonResponse |
||
332 | */ |
||
333 | View Code Duplication | public static function imageHotelAlert(string $bubble_key, string $display_type, string $image, string $message, string $title, string $url, string $url_message) |
|
348 | |||
349 | /** |
||
350 | * Add progress to an user achievement. |
||
351 | * @param int $achievement_id ID of the achievement. |
||
352 | * @param int $progress Amount to progress. |
||
353 | * @param int $user_id User id of the player that needs an achievement progress. |
||
354 | * @return JsonResponse |
||
355 | */ |
||
356 | public static function progressAchievement(int $achievement_id, int $progress, int $user_id) |
||
367 | |||
368 | /** |
||
369 | * Add a room event into a room. |
||
370 | * @param string $message Message that will have the room event |
||
371 | * @param int $room_id Room id of the room that will have the event |
||
372 | * @param int $user_id |
||
373 | * @return JsonResponse |
||
374 | */ |
||
375 | public static function createRoomEvent(string $message, int $room_id, int $user_id) |
||
386 | |||
387 | /** |
||
388 | * Send a gift to an user. |
||
389 | * @param int $itemid Item id to gift. |
||
390 | * @param string $message Message to include in the gift. |
||
391 | * @param int $user_id User id of the player that receives a gift. |
||
392 | * @return JsonResponse |
||
393 | */ |
||
394 | public static function sendGift(int $itemid, string $message, int $user_id) |
||
405 | |||
406 | /** |
||
407 | * Send room bundle to an user. |
||
408 | * @param int $catalog_page Id of the catalog page to buy. |
||
409 | * @param int $user_id User id of the player that will receive a room bundle. |
||
410 | * @return JsonResponse |
||
411 | */ |
||
412 | public static function sendRoomBundle(int $catalog_page, int $user_id) |
||
422 | |||
423 | /** |
||
424 | * Change the motto of an user. |
||
425 | * @param string $motto The motto to set. |
||
426 | * @param int $user_id The user id to change the motto for. |
||
427 | * @return JsonResponse |
||
428 | */ |
||
429 | public static function setMotto(string $motto, int $user_id) |
||
439 | |||
440 | /** |
||
441 | * Update an user rank. |
||
442 | * @param int $rank ID of the rank to set. |
||
443 | * @param int $user_id User id of the player to set the rank for. |
||
444 | * @return JsonResponse |
||
445 | */ |
||
446 | public static function setRank(int $rank, int $user_id) |
||
456 | |||
457 | /** |
||
458 | * @param string $message The message to send to all staff online. |
||
459 | * @return JsonResponse |
||
460 | */ |
||
461 | public static function sendStaffAlert(string $message) |
||
470 | |||
471 | /** |
||
472 | * Follow an user to another room. |
||
473 | * @param int $follow_id The user to stalk to. |
||
474 | * @param int $user_id User id of the player to stalk to another user. |
||
475 | * @return JsonResponse |
||
476 | */ |
||
477 | public static function stalkUser(int $follow_id, int $user_id) |
||
487 | |||
488 | /** |
||
489 | * @param int $bubble_id Bubble type. -1 defaults to the user type. |
||
490 | * @param string $message The message to say. |
||
491 | * @param string $type Talk type to use: - talk - whisper - shout |
||
492 | * @param int $user_id User id of the player to talk. |
||
493 | * @return JsonResponse |
||
494 | */ |
||
495 | public static function talkUser(int $bubble_id, string $message, string $type, int $user_id) |
||
507 | |||
508 | /** |
||
509 | * @param string $message Intern message |
||
510 | * @return JsonResponse |
||
511 | */ |
||
512 | public static function updateCatalog(string $message) |
||
521 | |||
522 | /** |
||
523 | * @param int $achievement_score Add achievement score. |
||
524 | * @param int $block_camera_follow Optional: Block camera following. |
||
525 | * @param int $block_following Optional: Block following in the hotel. |
||
526 | * @param int $block_friendrequests Optional: Block friendrequests in the hotel. |
||
527 | * @param int $block_roominvites Optional: Block receiving room invites in the hotel. |
||
528 | * @param string $look Optional: Look of the player. |
||
529 | * @param int $old_chat Optional: Use new chat. |
||
530 | * @param int $user_id User id of the player to update. |
||
531 | * @return JsonResponse |
||
532 | */ |
||
533 | public static function updateUser(int $achievement_score, int $block_camera_follow, int $block_following, int $block_friendrequests, int $block_roominvites, string $look, int $old_chat, int $user_id) |
||
552 | |||
553 | /** |
||
554 | * @param string $message Intern message |
||
555 | * @return JsonResponse |
||
556 | */ |
||
557 | public static function updateWordFilter(string $message) |
||
566 | } |
||
567 |
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.