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 | 30 | 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 | 24 | 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() |
|
| 185 | { |
||
| 186 | 6 | add_form_key('phpbb/ads/add'); |
|
| 187 | |||
| 188 | 6 | $action = $this->get_submitted_action(); |
|
| 189 | if ($action) |
||
|
|
|||
| 190 | 6 | { |
|
| 191 | 5 | $this->data = $this->input->get_form_data('phpbb/ads/add'); |
|
| 192 | 5 | $this->{$action}(); |
|
| 193 | 4 | $this->assign_data(); |
|
| 194 | 4 | } |
|
| 195 | else |
||
| 196 | 9 | { |
|
| 197 | 1 | $this->helper->assign_locations(); |
|
| 198 | } |
||
| 199 | |||
| 200 | // Set output vars for display in the template |
||
| 201 | 5 | $this->template->assign_vars(array( |
|
| 202 | 5 | 'S_ADD_AD' => true, |
|
| 203 | 5 | 'U_BACK' => $this->u_action, |
|
| 204 | 5 | 'U_ACTION' => "{$this->u_action}&action=add", |
|
| 205 | 5 | 'PICKER_DATE_FORMAT' => input::DATE_FORMAT, |
|
| 206 | 5 | 'U_FIND_USERNAME' => $this->helper->get_find_username_link(), |
|
| 207 | 5 | )); |
|
| 208 | 5 | } |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Edit an advertisement |
||
| 212 | * |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | 4 | public function action_edit() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Enable an advertisement |
||
| 252 | * |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | 4 | public function action_enable() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Disable an advertisement |
||
| 262 | * |
||
| 263 | * @return void |
||
| 264 | */ |
||
| 265 | 4 | public function action_disable() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Delete an advertisement |
||
| 272 | * |
||
| 273 | * @return void |
||
| 274 | */ |
||
| 275 | 3 | public function action_delete() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Display the ads |
||
| 318 | * |
||
| 319 | * @return void |
||
| 320 | */ |
||
| 321 | 1 | public function list_ads() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Perform general tasks |
||
| 360 | * |
||
| 361 | * @return void |
||
| 362 | */ |
||
| 363 | 9 | protected function setup() |
|
| 375 | |||
| 376 | 10 | protected function get_submitted_action() |
|
| 389 | |||
| 390 | 5 | protected function assign_data() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Enable/disable an advertisement |
||
| 399 | * |
||
| 400 | * @param bool $enable Enable or disable the advertisement? |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | 6 | protected function ad_enable($enable) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Prepare advertisement preview |
||
| 434 | * |
||
| 435 | * @return void |
||
| 436 | */ |
||
| 437 | 1 | protected function preview() |
|
| 441 | |||
| 442 | 1 | protected function upload_banner() |
|
| 446 | |||
| 447 | 1 | protected function analyse_ad_code() |
|
| 451 | |||
| 452 | 2 | protected function submit() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Print success message. |
||
| 486 | * |
||
| 487 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 488 | */ |
||
| 489 | 5 | protected function success() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Print error message. |
||
| 496 | * |
||
| 497 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 498 | */ |
||
| 499 | 4 | protected function error() |
|
| 503 | } |
||
| 504 |
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: