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 ManagesAccountContacts |
||
8 | { |
||
9 | use ImplementsActions; |
||
10 | |||
11 | /** |
||
12 | * Retrieve all existing account association. |
||
13 | * |
||
14 | * @return array |
||
15 | */ |
||
16 | public function accountContacts() |
||
24 | |||
25 | /** |
||
26 | * Retrieve an existing account association given the contactID and accountID. |
||
27 | * |
||
28 | * @param int $contactId |
||
29 | * @param int $accountId |
||
30 | * |
||
31 | * @return AccountContact|null |
||
32 | */ |
||
33 | public function findAccountContact($contactId, $accountId) |
||
48 | |||
49 | /** |
||
50 | * Retrieve an existing account association. |
||
51 | * |
||
52 | * @param int $id |
||
53 | * |
||
54 | * @return AccountContact|null |
||
55 | */ |
||
56 | public function getAccountContact($id) |
||
65 | |||
66 | /** |
||
67 | * Create a new account association. |
||
68 | * |
||
69 | * @param int $contact contact ID |
||
70 | * @param int $account account ID |
||
71 | * @param string $jobTitle |
||
72 | * |
||
73 | * @return AccountContact|null |
||
74 | */ |
||
75 | public function createAccountContact($contact, $account, $jobTitle) |
||
86 | |||
87 | /** |
||
88 | * Update an existing account association. |
||
89 | * |
||
90 | * @param int $associationId |
||
91 | * @param string $jobTitle |
||
92 | * |
||
93 | * @return AccountContact|null |
||
94 | */ |
||
95 | public function updateAccountContact($associationId, $jobTitle) |
||
106 | |||
107 | /** |
||
108 | * Find or create an account contact association. |
||
109 | * |
||
110 | * @param int $contact contact ID |
||
111 | * @param int $account account ID |
||
112 | * @param string $jobTitle |
||
113 | * |
||
114 | * @return AccountContact |
||
115 | */ |
||
116 | View Code Duplication | public function findOrCreateAccountContact($contact, $account, $jobTitle) |
|
126 | |||
127 | /** |
||
128 | * Update or create an account contact. |
||
129 | * |
||
130 | * @param int $contact contact ID |
||
131 | * @param int $account account ID |
||
132 | * @param string $jobTitle |
||
133 | * |
||
134 | * @return AccountContact |
||
135 | */ |
||
136 | View Code Duplication | public function updateOrCreateAccountContact($contact, $account, $jobTitle) |
|
146 | |||
147 | /** |
||
148 | * Delete an existing account association. |
||
149 | * |
||
150 | * @param int $associationId |
||
151 | * |
||
152 | * @return void |
||
153 | */ |
||
154 | public function deleteAccountContact($associationId) |
||
158 | } |
||
159 |
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.