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:
Complex classes like admin_controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use admin_controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class admin_controller |
||
| 19 | { |
||
| 20 | private $data = array(); |
||
| 21 | |||
| 22 | /** @var \phpbb\template\template */ |
||
| 23 | protected $template; |
||
| 24 | |||
| 25 | /** @var \phpbb\language\language */ |
||
| 26 | protected $language; |
||
| 27 | |||
| 28 | /** @var \phpbb\request\request */ |
||
| 29 | protected $request; |
||
| 30 | |||
| 31 | /** @var \phpbb\ads\ad\manager */ |
||
| 32 | protected $manager; |
||
| 33 | |||
| 34 | /** @var \phpbb\config\db_text */ |
||
| 35 | protected $config_text; |
||
| 36 | |||
| 37 | /** @var \phpbb\config\config */ |
||
| 38 | protected $config; |
||
| 39 | |||
| 40 | /** @var \phpbb\group\helper */ |
||
| 41 | protected $group_helper; |
||
| 42 | |||
| 43 | /** @var \phpbb\ads\controller\admin_input */ |
||
| 44 | protected $input; |
||
| 45 | |||
| 46 | /** @var \phpbb\ads\controller\admin_helper */ |
||
| 47 | protected $helper; |
||
| 48 | |||
| 49 | /** @var \phpbb\ads\analyser\manager */ |
||
| 50 | protected $analyser; |
||
| 51 | |||
| 52 | /** @var string root_path */ |
||
| 53 | protected $root_path; |
||
| 54 | |||
| 55 | /** @var string php_ext */ |
||
| 56 | protected $php_ext; |
||
| 57 | |||
| 58 | /** @var string Custom form action */ |
||
| 59 | protected $u_action; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param \phpbb\template\template $template Template object |
||
| 65 | * @param \phpbb\language\language $language Language object |
||
| 66 | * @param \phpbb\request\request $request Request object |
||
| 67 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
| 68 | * @param \phpbb\config\db_text $config_text Config text object |
||
| 69 | * @param \phpbb\config\config $config Config object |
||
| 70 | * @param \phpbb\group\helper $group_helper Group helper object |
||
| 71 | * @param \phpbb\ads\controller\admin_input $input Admin input object |
||
| 72 | * @param \phpbb\ads\controller\admin_helper $helper Admin helper object |
||
| 73 | * @param \phpbb\ads\analyser\manager $analyser Ad code analyser object |
||
| 74 | * @param string $root_path phpBB root path |
||
| 75 | * @param string $php_ext PHP extension |
||
| 76 | */ |
||
| 77 | 32 | public function __construct(\phpbb\template\template $template, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\group\helper $group_helper, \phpbb\ads\controller\admin_input $input, \phpbb\ads\controller\admin_helper $helper, \phpbb\ads\analyser\manager $analyser, $root_path, $php_ext) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Process user request for manage mode |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | 6 | public function mode_manage() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Process user request for settings mode |
||
| 115 | * |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | 3 | public function mode_settings() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Set page url |
||
| 160 | * |
||
| 161 | * @param string $u_action Custom form action |
||
| 162 | * @return void |
||
| 163 | */ |
||
| 164 | 26 | public function set_page_url($u_action) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Get ACP page title for Ads module |
||
| 171 | * |
||
| 172 | * @return string Language string for Ads ACP module |
||
| 173 | */ |
||
| 174 | 1 | public function get_page_title() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Add an advertisement |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | 9 | public function action_add() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Edit an advertisement |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | 7 | public function action_edit() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Enable an advertisement |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | 4 | public function action_enable() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Disable an advertisement |
||
| 247 | * |
||
| 248 | * @return void |
||
| 249 | */ |
||
| 250 | 4 | public function action_disable() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Delete an advertisement |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | 3 | public function action_delete() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Display the ads |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | 1 | public function list_ads() |
|
| 329 | |||
| 330 | 1 | protected function assign_ad_template_vars($row, $ad_expired, $ad_end_date, $ad_enabled) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Perform general tasks |
||
| 350 | * |
||
| 351 | * @return void |
||
| 352 | */ |
||
| 353 | 9 | protected function setup() |
|
| 365 | |||
| 366 | 12 | protected function get_submitted_action() |
|
| 379 | |||
| 380 | 8 | protected function assign_data() |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Enable/disable an advertisement |
||
| 389 | * |
||
| 390 | * @param bool $enable Enable or disable the advertisement? |
||
| 391 | * @return void |
||
| 392 | */ |
||
| 393 | 6 | protected function ad_enable($enable) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Prepare advertisement preview |
||
| 424 | * |
||
| 425 | * @return void |
||
| 426 | */ |
||
| 427 | 2 | protected function preview() |
|
| 431 | |||
| 432 | 1 | protected function upload_banner() |
|
| 436 | |||
| 437 | 1 | protected function analyse_ad_code() |
|
| 441 | |||
| 442 | 6 | protected function submit() |
|
| 454 | |||
| 455 | 8 | protected function set_output_vars($action, $ad_id) |
|
| 467 | |||
| 468 | 1 | protected function add_ad() |
|
| 477 | |||
| 478 | protected function edit_ad($ad_id) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Print success message. |
||
| 498 | * |
||
| 499 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 500 | */ |
||
| 501 | 5 | protected function success() |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Print error message. |
||
| 508 | * |
||
| 509 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 510 | */ |
||
| 511 | 4 | protected function error() |
|
| 515 | } |
||
| 516 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: