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 |
||
| 21 | class acp_manager |
||
| 22 | { |
||
| 23 | /** @var driver_interface */ |
||
| 24 | protected $db; |
||
| 25 | |||
| 26 | /** @var helper */ |
||
| 27 | protected $group_helper; |
||
| 28 | |||
| 29 | /** @var request */ |
||
| 30 | protected $request; |
||
| 31 | |||
| 32 | /** @var user */ |
||
| 33 | protected $user; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Constructor |
||
| 37 | * |
||
| 38 | * @param driver_interface $db |
||
| 39 | * @param helper $group_helper |
||
| 40 | * @param request $request |
||
| 41 | * @param user $user |
||
| 42 | * @access public |
||
| 43 | */ |
||
| 44 | 34 | public function __construct(driver_interface $db, helper $group_helper, request $request, user $user) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Update BBCode order fields in the db on move up/down |
||
| 54 | * |
||
| 55 | * @param string $action The action move_up|move_down |
||
| 56 | * @access public |
||
| 57 | */ |
||
| 58 | 10 | public function move($action) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Update BBCode order fields in the db on drag_drop |
||
| 84 | * |
||
| 85 | * @access public |
||
| 86 | */ |
||
| 87 | 3 | public function drag_drop() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Retrieve the maximum value from the bbcode_order field stored in the db |
||
| 117 | * |
||
| 118 | * @return int The maximum order |
||
| 119 | * @access public |
||
| 120 | */ |
||
| 121 | 1 | public function get_max_bbcode_order() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get the bbcode_group data from the posted form |
||
| 128 | * |
||
| 129 | * @return string The usergroup id numbers, comma delimited, or empty |
||
| 130 | * @access public |
||
| 131 | */ |
||
| 132 | 3 | public function get_bbcode_group_form_data() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Get the bbcode_group data from the database |
||
| 142 | * |
||
| 143 | * @param int $bbcode_id Custom BBCode id |
||
| 144 | * @return array Custom BBCode user group ids |
||
| 145 | * @access public |
||
| 146 | */ |
||
| 147 | 6 | View Code Duplication | public function get_bbcode_group_data($bbcode_id) |
| 148 | { |
||
| 149 | $sql = 'SELECT bbcode_group |
||
| 150 | 6 | FROM ' . BBCODES_TABLE . ' |
|
| 151 | 6 | WHERE bbcode_id = ' . (int) $bbcode_id; |
|
| 152 | 6 | $result = $this->db->sql_query($sql); |
|
| 153 | 6 | $row = $this->db->sql_fetchrow($result); |
|
| 154 | 6 | $this->db->sql_freeresult($result); |
|
| 155 | |||
| 156 | 6 | return explode(',', $row['bbcode_group']); |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the bbcode_group data from the database, |
||
| 161 | * for every BBCode that has groups assigned |
||
| 162 | * |
||
| 163 | * @return array Custom BBCode user group ids for each BBCode, by name |
||
| 164 | * @access public |
||
| 165 | */ |
||
| 166 | 1 | public function get_bbcode_groups_data() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Generate a select box containing user groups |
||
| 184 | * |
||
| 185 | * @param array $select_id The user groups to mark as selected |
||
| 186 | * @return string HTML markup of user groups select box for the form |
||
| 187 | * @access public |
||
| 188 | */ |
||
| 189 | 5 | public function bbcode_group_select_options(array $select_id = array()) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Resynchronize the Custom BBCodes order field |
||
| 211 | * (Originally based on Custom BBCode Sorting MOD by RMcGirr83) |
||
| 212 | * |
||
| 213 | * @access public |
||
| 214 | */ |
||
| 215 | 14 | public function resynchronize_bbcode_order() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Get the bbcode_order value for a bbcode |
||
| 239 | * |
||
| 240 | * @param int $bbcode_id ID of the bbcode |
||
| 241 | * @return int The bbcode's order |
||
| 242 | * @access protected |
||
| 243 | */ |
||
| 244 | 9 | protected function get_bbcode_order($bbcode_id) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Update the bbcode orders for bbcodes moved up/down |
||
| 258 | * |
||
| 259 | * @param int $bbcode_order Value of the bbcode order |
||
| 260 | * @param string $action The action move_up|move_down |
||
| 261 | * @return mixed Number of the affected rows by the last query |
||
| 262 | * false if no query has been run before |
||
| 263 | * @access protected |
||
| 264 | */ |
||
| 265 | 7 | protected function update_bbcode_orders($bbcode_order, $action) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Build SQL query to update a bbcode order value |
||
| 284 | * |
||
| 285 | * @param int $bbcode_id ID of the bbcode |
||
| 286 | * @param int $bbcode_order Value of the bbcode order |
||
| 287 | * @return string The SQL query to run |
||
| 288 | * @access protected |
||
| 289 | */ |
||
| 290 | 8 | protected function update_bbcode_order($bbcode_id, $bbcode_order) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Retrieve the maximum value in a column from the bbcodes table |
||
| 299 | * |
||
| 300 | * @param string $column Name of the column (bbcode_id|bbcode_order) |
||
| 301 | * @return int The maximum value in the column |
||
| 302 | * @access protected |
||
| 303 | */ |
||
| 304 | 3 | View Code Duplication | protected function get_max_column_value($column) |
| 305 | { |
||
| 306 | 3 | $sql = 'SELECT MAX(' . $this->db->sql_escape($column) . ') AS maximum |
|
| 307 | 3 | FROM ' . BBCODES_TABLE; |
|
| 308 | 3 | $result = $this->db->sql_query($sql); |
|
| 309 | 3 | $maximum = $this->db->sql_fetchfield('maximum'); |
|
| 310 | 3 | $this->db->sql_freeresult($result); |
|
| 311 | |||
| 312 | 3 | return (int) $maximum; |
|
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Send a JSON response |
||
| 317 | * |
||
| 318 | * @param bool $content The content of the JSON response (true|false) |
||
| 319 | * @access protected |
||
| 320 | */ |
||
| 321 | 9 | protected function send_json_response($content) |
|
| 331 | } |
||
| 332 |