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 |
||
22 | * @author Michael Albertsen (http://culex.dk) <[email protected]> |
||
23 | * @link https://github.com/XoopsModules25x/smallworld |
||
24 | * @since 1.0 |
||
25 | */ |
||
26 | |||
27 | /** |
||
28 | * |
||
29 | * Images Class |
||
30 | * |
||
31 | * Handles album creation and retrieval |
||
32 | * |
||
33 | */ |
||
34 | class Images |
||
35 | { |
||
36 | /** |
||
37 | * Create album folders |
||
38 | * |
||
39 | * @todo use \Smallworld\SysUtility::createFolder() to refactor this method |
||
40 | * @param int $userId |
||
41 | * @return void |
||
42 | */ |
||
43 | public function createAlbum($userId) |
||
65 | |||
66 | /** |
||
67 | * View user album |
||
68 | * |
||
69 | * @param int $userId owner |
||
70 | * @param int $user visitors |
||
71 | * @return array|bool |
||
72 | */ |
||
73 | public function viewalbum($userId, $user) |
||
74 | { |
||
75 | $post = []; |
||
76 | $checkFriend = new User(); |
||
77 | $foundArray = $checkFriend->friendcheck((int)$userId, (int)$user); |
||
78 | if (0 < count($foundArray)) { |
||
79 | // check friend is good to go |
||
80 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_images') . " WHERE userid = '" . (int)$user . "'"; |
||
81 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
82 | while (false !== ($sqlfetch = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
83 | $post[] = [ |
||
84 | 'id' => stripslashes($sqlfetch['id']), |
||
85 | 'userid' => stripslashes($sqlfetch['userid']), |
||
86 | 'imgurl' => stripslashes($sqlfetch['imgurl']), |
||
87 | 'desc' => SwFunc\cleanupString($sqlfetch['desc']), |
||
88 | 'alt' => SwFunc\cleanupString($sqlfetch['desc']), |
||
89 | 'time' => stripslashes($sqlfetch['time']), |
||
90 | 'editimg' => "<span class='smallworld_edit_imgdesc_holder'><img src='" . Helper::getInstance()->url('images/edit_icon.png') . "'></span> <a class='smallworld_edit_imgdesc' href='editimages.php'>" . _SMALLWORLD_EDITDESCRIPTION . '</a>' |
||
91 | ]; |
||
92 | } |
||
93 | |||
114 |
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.