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 |
||
18 | class idea extends base |
||
19 | { |
||
20 | /** |
||
21 | * Returns the specified idea. |
||
22 | * |
||
23 | * @param int $id The ID of the idea to return. |
||
24 | * |
||
25 | * @return array|false The idea row set, or false if not found. |
||
26 | */ |
||
27 | View Code Duplication | public function get_idea($id) |
|
38 | |||
39 | /** |
||
40 | * Returns an idea specified by its topic ID. |
||
41 | * |
||
42 | * @param int $id The ID of the idea to return. |
||
43 | * |
||
44 | * @return array|false The idea row set, or false if not found. |
||
45 | */ |
||
46 | View Code Duplication | public function get_idea_by_topic_id($id) |
|
57 | |||
58 | /** |
||
59 | * Returns the status name from the status ID specified. |
||
60 | * |
||
61 | * @param int $id ID of the status. |
||
62 | * |
||
63 | * @return string|bool The status name if it exists, false otherwise. |
||
64 | */ |
||
65 | public function get_status_from_id($id) |
||
69 | |||
70 | /** |
||
71 | * Updates the status of an idea. |
||
72 | * |
||
73 | * @param int $idea_id The ID of the idea. |
||
74 | * @param int $status The ID of the status. |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | public function set_status($idea_id, $status) |
||
86 | |||
87 | /** |
||
88 | * Sets the ID of the duplicate for an idea. |
||
89 | * |
||
90 | * @param int $idea_id ID of the idea to be updated. |
||
91 | * @param string $duplicate Idea ID of duplicate. |
||
92 | * |
||
93 | * @return bool True if set, false if invalid. |
||
94 | */ |
||
95 | View Code Duplication | public function set_duplicate($idea_id, $duplicate) |
|
110 | |||
111 | /** |
||
112 | * Sets the RFC link of an idea. |
||
113 | * |
||
114 | * @param int $idea_id ID of the idea to be updated. |
||
115 | * @param string $rfc Link to the RFC. |
||
116 | * |
||
117 | * @return bool True if set, false if invalid. |
||
118 | */ |
||
119 | View Code Duplication | public function set_rfc($idea_id, $rfc) |
|
135 | |||
136 | /** |
||
137 | * Sets the ticket ID of an idea. |
||
138 | * |
||
139 | * @param int $idea_id ID of the idea to be updated. |
||
140 | * @param string $ticket Ticket ID. |
||
141 | * |
||
142 | * @return bool True if set, false if invalid. |
||
143 | */ |
||
144 | View Code Duplication | public function set_ticket($idea_id, $ticket) |
|
159 | |||
160 | /** |
||
161 | * Sets the implemented version of an idea. |
||
162 | * |
||
163 | * @param int $idea_id ID of the idea to be updated. |
||
164 | * @param string $version Version of phpBB the idea was implemented in. |
||
165 | * |
||
166 | * @return bool True if set, false if invalid. |
||
167 | */ |
||
168 | View Code Duplication | public function set_implemented($idea_id, $version) |
|
184 | |||
185 | /** |
||
186 | * Sets the title of an idea. |
||
187 | * |
||
188 | * @param int $idea_id ID of the idea to be updated. |
||
189 | * @param string $title New title. |
||
190 | * |
||
191 | * @return boolean True if updated, false if invalid length. |
||
192 | */ |
||
193 | public function set_title($idea_id, $title) |
||
208 | |||
209 | /** |
||
210 | * Get the title of an idea. |
||
211 | * |
||
212 | * @param int $id ID of an idea |
||
213 | * |
||
214 | * @return string The idea's title, empty string if not found |
||
215 | */ |
||
216 | public function get_title($id) |
||
227 | |||
228 | /** |
||
229 | * Submit new idea data to the ideas table |
||
230 | * |
||
231 | * @param array $data An array of post data from a newly posted idea |
||
232 | * |
||
233 | * @return int The ID of the new idea. |
||
234 | */ |
||
235 | public function submit($data) |
||
254 | |||
255 | /** |
||
256 | * Deletes an idea and the topic to go with it. |
||
257 | * |
||
258 | * @param int $id The ID of the idea to be deleted. |
||
259 | * @param int $topic_id The ID of the idea topic. Optional, but preferred. |
||
260 | * |
||
261 | * @return boolean Whether the idea was deleted or not. |
||
262 | */ |
||
263 | public function delete($id, $topic_id = 0) |
||
282 | |||
283 | /** |
||
284 | * Submits a vote on an idea. |
||
285 | * |
||
286 | * @param array $idea The idea returned by get_idea(). |
||
287 | * @param int $user_id The ID of the user voting. |
||
288 | * @param int $value Up (1) or down (0)? |
||
289 | * |
||
290 | * @return array|string Array of information or string on error. |
||
291 | */ |
||
292 | public function vote(&$idea, $user_id, $value) |
||
373 | |||
374 | /** |
||
375 | * Remove a user's vote from an idea |
||
376 | * |
||
377 | * @param array $idea The idea returned by get_idea(). |
||
378 | * @param int $user_id The ID of the user voting. |
||
379 | * |
||
380 | * @return array Array of information. |
||
381 | */ |
||
382 | public function remove_vote(&$idea, $user_id) |
||
415 | |||
416 | /** |
||
417 | * Returns voter info on an idea. |
||
418 | * |
||
419 | * @param int $id ID of the idea. |
||
420 | * |
||
421 | * @return array Array of row data |
||
422 | */ |
||
423 | public function get_voters($id) |
||
445 | } |
||
446 |
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.