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 | * Update BBCode order fields in the db on move up/down |
||
| 55 | * |
||
| 56 | * @param string $action The action move_up|move_down |
||
| 57 | * @access public |
||
| 58 | 10 | */ |
|
| 59 | public function move($action) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Update BBCode order fields in the db on drag-n-drop |
||
| 85 | * |
||
| 86 | * @access public |
||
| 87 | 3 | */ |
|
| 88 | public function move_drag() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Retrieve the maximum value from the bbcode_order field stored in the db |
||
| 118 | * |
||
| 119 | * @return int The maximum order |
||
| 120 | * @access public |
||
| 121 | 1 | */ |
|
| 122 | public function get_max_bbcode_order() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the bbcode_group data from the posted form |
||
| 129 | * |
||
| 130 | * @return string The usergroup id numbers, comma delimited, or empty |
||
| 131 | * @access public |
||
| 132 | 3 | */ |
|
| 133 | public function get_bbcode_group_form_data() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the bbcode_group data from the database |
||
| 143 | * |
||
| 144 | * @param int $bbcode_id Custom BBCode id |
||
| 145 | * @return array Custom BBCode user group ids |
||
| 146 | * @access public |
||
| 147 | 6 | */ |
|
| 148 | View Code Duplication | public function get_bbcode_group_data($bbcode_id) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Get the bbcode_group data from the database, |
||
| 162 | * for every BBCode that has groups assigned |
||
| 163 | * |
||
| 164 | * @return array Custom BBCode user group ids for each BBCode, by name |
||
| 165 | * @access public |
||
| 166 | 1 | */ |
|
| 167 | public function get_bbcode_groups_data() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Generate a select box containing user groups |
||
| 185 | * |
||
| 186 | * @param array $select_id The user groups to mark as selected |
||
| 187 | * @return string HTML markup of user groups select box for the form |
||
| 188 | * @access public |
||
| 189 | 5 | */ |
|
| 190 | public function bbcode_group_select_options(array $select_id = array()) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Resynchronize the Custom BBCodes order field |
||
| 212 | * (Originally based on Custom BBCode Sorting MOD by RMcGirr83) |
||
| 213 | * |
||
| 214 | * @access public |
||
| 215 | 14 | */ |
|
| 216 | public function resynchronize_bbcode_order() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get the bbcode_order value for a bbcode |
||
| 240 | * |
||
| 241 | * @param int $bbcode_id ID of the bbcode |
||
| 242 | * @return int The bbcode's order |
||
| 243 | * @access protected |
||
| 244 | 9 | */ |
|
| 245 | protected function get_bbcode_order($bbcode_id) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Update the bbcode orders for bbcodes moved up/down |
||
| 259 | * |
||
| 260 | * @param int $bbcode_order Value of the bbcode order |
||
| 261 | * @param string $action The action move_up|move_down |
||
| 262 | * @return mixed Number of the affected rows by the last query |
||
| 263 | * false if no query has been run before |
||
| 264 | * @access protected |
||
| 265 | 7 | */ |
|
| 266 | protected function update_bbcode_orders($bbcode_order, $action) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Build SQL query to update a bbcode order value |
||
| 285 | * |
||
| 286 | * @param int $bbcode_id ID of the bbcode |
||
| 287 | * @param int $bbcode_order Value of the bbcode order |
||
| 288 | * @return string The SQL query to run |
||
| 289 | * @access protected |
||
| 290 | 8 | */ |
|
| 291 | protected function update_bbcode_order($bbcode_id, $bbcode_order) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Retrieve the maximum value in a column from the bbcodes table |
||
| 300 | * |
||
| 301 | * @param string $column Name of the column (bbcode_id|bbcode_order) |
||
| 302 | * @return int The maximum value in the column |
||
| 303 | * @access protected |
||
| 304 | 3 | */ |
|
| 305 | View Code Duplication | protected function get_max_column_value($column) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Send a JSON response |
||
| 318 | * |
||
| 319 | * @param bool $content The content of the JSON response (true|false) |
||
| 320 | * @access protected |
||
| 321 | 9 | */ |
|
| 322 | protected function send_json_response($content) |
||
| 332 | } |
||
| 333 |