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 |
||
| 16 | class admin_controller |
||
| 17 | { |
||
| 18 | const MAX_NAME_LENGTH = 255; |
||
| 19 | |||
| 20 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 21 | protected $db; |
||
| 22 | |||
| 23 | /** @var \phpbb\template\template */ |
||
| 24 | protected $template; |
||
| 25 | |||
| 26 | /** @var \phpbb\user */ |
||
| 27 | protected $user; |
||
| 28 | |||
| 29 | /** @var \phpbb\request\request */ |
||
| 30 | protected $request; |
||
| 31 | |||
| 32 | /** @var string ads_table */ |
||
| 33 | protected $ads_table; |
||
| 34 | |||
| 35 | /** @var string php_ext */ |
||
| 36 | protected $php_ext; |
||
| 37 | |||
| 38 | /** @var string phpbb_admin_path */ |
||
| 39 | protected $phpbb_admin_path; |
||
| 40 | |||
| 41 | /** @var string Custom form action */ |
||
| 42 | protected $u_action; |
||
| 43 | |||
| 44 | /** @var array Form validation errors */ |
||
| 45 | protected $errors = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor |
||
| 49 | * |
||
| 50 | * @param \phpbb\db\driver\driver_interface $db DB driver interface |
||
| 51 | * @param \phpbb\template\template $template Template object |
||
| 52 | * @param \phpbb\user $user User object |
||
| 53 | * @param \phpbb\request\request $request Request object |
||
| 54 | * @param string $ads_table Ads table |
||
| 55 | * @param string $php_ext PHP extension |
||
| 56 | * @param string $phpbb_admin_path Path to admin |
||
| 57 | */ |
||
| 58 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, $ads_table, $php_ext, $phpbb_admin_path) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Set page url |
||
| 71 | * |
||
| 72 | * @param string $u_action Custom form action |
||
| 73 | * @return void |
||
| 74 | */ |
||
| 75 | public function set_page_url($u_action) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Load module-specific language |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | public function load_lang() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get ACP page title for Ads module |
||
| 92 | * |
||
| 93 | * @return string Language string for Ads ACP module |
||
| 94 | */ |
||
| 95 | public function get_page_title() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get action |
||
| 102 | * |
||
| 103 | * @return string Ads module action |
||
| 104 | */ |
||
| 105 | public function get_action() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Add an advertisement |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function action_add() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Edit an advertisement |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | public function action_edit() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Enable/disable an advertisement |
||
| 210 | * |
||
| 211 | * @param bool $enable Enable or disable the advertisement? |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | public function ad_enable($enable) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Delete an advertisement |
||
| 245 | * |
||
| 246 | * @return void |
||
| 247 | */ |
||
| 248 | public function action_delete() |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * Display the ads |
||
| 280 | * |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | public function list_ads() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Check the form key. |
||
| 312 | * |
||
| 313 | * @param string $form_name The name of the form. |
||
| 314 | * @return void |
||
| 315 | */ |
||
| 316 | protected function check_form_key($form_name) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get admin form data. |
||
| 326 | * |
||
| 327 | * @return void |
||
| 328 | */ |
||
| 329 | protected function get_form_data() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Validate form data. |
||
| 341 | * |
||
| 342 | * @param array $data The form data. |
||
| 343 | * @return void |
||
| 344 | */ |
||
| 345 | protected function validate($data) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Assign errors to the template. |
||
| 359 | * |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | protected function assign_errors() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Assign form data to the template. |
||
| 372 | * |
||
| 373 | * @param array $data The form data. |
||
| 374 | * @return void |
||
| 375 | */ |
||
| 376 | protected function assign_form_data($data) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Print success message. |
||
| 388 | * |
||
| 389 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 390 | */ |
||
| 391 | protected function success() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Print error message. |
||
| 398 | * |
||
| 399 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 400 | */ |
||
| 401 | protected function error() |
||
| 405 | } |
||
| 406 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.