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 | trait BoardInvites |
||
8 | { |
||
9 | use HandlesRequest; |
||
10 | |||
11 | /** |
||
12 | * @return array |
||
13 | */ |
||
14 | protected function requiresLoginForBoardInvites() { |
||
24 | |||
25 | /** |
||
26 | * Get boards invites |
||
27 | * @return array |
||
28 | */ |
||
29 | public function invites() |
||
40 | |||
41 | /** |
||
42 | * @param string $boardId |
||
43 | * @param string|array $emails |
||
44 | * @return bool |
||
45 | */ |
||
46 | View Code Duplication | public function sendInviteByEmail($boardId, $emails) |
|
56 | |||
57 | /** |
||
58 | * @param string $boardId |
||
59 | * @param string|array $users |
||
60 | * @return bool |
||
61 | */ |
||
62 | public function sendInvite($boardId, $users) |
||
72 | |||
73 | /** |
||
74 | * @param string $boardId |
||
75 | * @param string|array $userIds |
||
76 | * @return bool |
||
77 | */ |
||
78 | View Code Duplication | public function sendInviteByUserId($boardId, $userIds) |
|
88 | |||
89 | /** |
||
90 | * @param string $boardId |
||
91 | * @param string $userId |
||
92 | * @param bool $ban |
||
93 | * @return bool |
||
94 | */ |
||
95 | public function deleteInvite($boardId, $userId, $ban = false) |
||
106 | |||
107 | /** |
||
108 | * @param string $boardId |
||
109 | * @return bool |
||
110 | */ |
||
111 | public function ignoreInvite($boardId) |
||
115 | |||
116 | /** |
||
117 | * @param string $boardId |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function acceptInvite($boardId) |
||
124 | |||
125 | /** |
||
126 | * @param string $boardId |
||
127 | * @param string $endpoint |
||
128 | * @return bool |
||
129 | */ |
||
130 | protected function makeInviteCall($boardId, $endpoint) |
||
139 | } |
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.