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 | class acp_manager |
||
23 | { |
||
24 | /** @var driver_interface */ |
||
25 | protected $db; |
||
26 | |||
27 | /** @var helper */ |
||
28 | protected $group_helper; |
||
29 | |||
30 | /** @var language */ |
||
31 | protected $language; |
||
32 | |||
33 | /** @var request */ |
||
34 | protected $request; |
||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param driver_interface $db |
||
40 | * @param helper $group_helper |
||
41 | * @param language $language |
||
42 | * @param request $request |
||
43 | * @access public |
||
44 | 34 | */ |
|
45 | public function __construct(driver_interface $db, helper $group_helper, language $language, request $request) |
||
52 | |||
53 | /** |
||
54 | * Move a BBCode up in order |
||
55 | */ |
||
56 | public function move_up() |
||
60 | 10 | ||
61 | /** |
||
62 | 10 | * Move a BBCode down in order |
|
63 | 10 | */ |
|
64 | 1 | public function move_down() |
|
68 | |||
69 | /** |
||
70 | 9 | * Update BBCode order fields in the db on move up/down |
|
71 | 9 | * |
|
72 | 2 | * @param string $action The action move_up|move_down |
|
73 | * @access public |
||
74 | */ |
||
75 | 7 | public function move($action) |
|
98 | 2 | ||
99 | /** |
||
100 | * Update BBCode order fields in the db on drag-n-drop |
||
101 | 2 | * |
|
102 | * @access public |
||
103 | 2 | */ |
|
104 | 2 | public function move_drag() |
|
131 | |||
132 | 3 | /** |
|
133 | * Retrieve the maximum value from the bbcode_order field stored in the db |
||
134 | 3 | * |
|
135 | 3 | * @return int The maximum order |
|
136 | * @access public |
||
137 | 3 | */ |
|
138 | public function get_max_bbcode_order() |
||
142 | |||
143 | /** |
||
144 | * Get the bbcode_group data from the posted form |
||
145 | * |
||
146 | * @return string The usergroup id numbers, comma delimited, or empty |
||
147 | 6 | * @access public |
|
148 | */ |
||
149 | public function get_bbcode_group_form_data() |
||
156 | 6 | ||
157 | /** |
||
158 | * Get the bbcode_group data from the database |
||
159 | * |
||
160 | * @param int $bbcode_id Custom BBCode id |
||
161 | * @return array Custom BBCode user group ids |
||
162 | * @access public |
||
163 | */ |
||
164 | View Code Duplication | public function get_bbcode_group_data($bbcode_id) |
|
175 | 1 | ||
176 | 1 | /** |
|
177 | 1 | * Get the bbcode_group data from the database, |
|
178 | * for every BBCode that has groups assigned |
||
179 | 1 | * |
|
180 | * @return array Custom BBCode user group ids for each BBCode, by name |
||
181 | * @access public |
||
182 | */ |
||
183 | public function get_bbcode_groups_data() |
||
198 | 5 | ||
199 | 5 | /** |
|
200 | * Generate a select box containing user groups |
||
201 | 5 | * |
|
202 | 5 | * @param array $select_id The user groups to mark as selected |
|
203 | 5 | * @return string HTML markup of user groups select box for the form |
|
204 | 5 | * @access public |
|
205 | */ |
||
206 | 5 | public function bbcode_group_select_options(array $select_id = array()) |
|
225 | 14 | ||
226 | /** |
||
227 | 14 | * Resynchronize the Custom BBCodes order field |
|
228 | 14 | * (Originally based on Custom BBCode Sorting MOD by RMcGirr83) |
|
229 | 7 | * |
|
230 | 7 | * @access public |
|
231 | 14 | */ |
|
232 | 14 | public function resynchronize_bbcode_order() |
|
253 | 9 | ||
254 | /** |
||
255 | * Get the bbcode_order value for a bbcode |
||
256 | * |
||
257 | * @param int $bbcode_id ID of the bbcode |
||
258 | * @return int The bbcode's order |
||
259 | * @access protected |
||
260 | */ |
||
261 | protected function get_bbcode_order($bbcode_id) |
||
272 | 7 | ||
273 | 7 | /** |
|
274 | 7 | * Update the bbcode orders for bbcodes moved up/down |
|
275 | 7 | * |
|
276 | 7 | * @param int $bbcode_order Value of the bbcode order |
|
277 | 7 | * @param string $action The action move_up|move_down |
|
278 | * @return mixed Number of the affected rows by the last query |
||
279 | 7 | * false if no query has been run before |
|
280 | * @access protected |
||
281 | */ |
||
282 | protected function update_bbcode_orders($bbcode_order, $action) |
||
298 | |||
299 | /** |
||
300 | * Build SQL query to update a bbcode order value |
||
301 | * |
||
302 | * @param int $bbcode_id ID of the bbcode |
||
303 | * @param int $bbcode_order Value of the bbcode order |
||
304 | 3 | * @return string The SQL query to run |
|
305 | * @access protected |
||
306 | 3 | */ |
|
307 | 3 | protected function update_bbcode_order($bbcode_id, $bbcode_order) |
|
313 | |||
314 | /** |
||
315 | * Retrieve the maximum value in a column from the bbcodes table |
||
316 | * |
||
317 | * @param string $column Name of the column (bbcode_id|bbcode_order) |
||
318 | * @return int The maximum value in the column |
||
319 | * @access protected |
||
320 | */ |
||
321 | 9 | View Code Duplication | protected function get_max_column_value($column) |
331 | |||
332 | /** |
||
333 | * Send a JSON response |
||
334 | * |
||
335 | * @param bool $content The content of the JSON response (true|false) |
||
336 | * @access protected |
||
337 | */ |
||
338 | protected function send_json_response($content) |
||
348 | } |
||
349 |