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 |
||
| 16 | class admin_controller |
||
| 17 | { |
||
| 18 | const MAX_NAME_LENGTH = 255; |
||
| 19 | const DATE_FORMAT = 'Y-m-d'; |
||
| 20 | const DEFAULT_PRIORITY = 5; |
||
| 21 | |||
| 22 | /** @var \phpbb\template\template */ |
||
| 23 | protected $template; |
||
| 24 | |||
| 25 | /** @var \phpbb\user */ |
||
| 26 | protected $user; |
||
| 27 | |||
| 28 | /** @var \phpbb\request\request */ |
||
| 29 | protected $request; |
||
| 30 | |||
| 31 | /** @var \phpbb\ads\ad\manager */ |
||
| 32 | protected $manager; |
||
| 33 | |||
| 34 | /** @var \phpbb\ads\location\manager */ |
||
| 35 | protected $location_manager; |
||
| 36 | |||
| 37 | /** @var \phpbb\log\log */ |
||
| 38 | protected $log; |
||
| 39 | |||
| 40 | /** @var \phpbb\config\db_text */ |
||
| 41 | protected $config_text; |
||
| 42 | |||
| 43 | /** @var \phpbb\config\config */ |
||
| 44 | protected $config; |
||
| 45 | |||
| 46 | /** @var string root_path */ |
||
| 47 | protected $root_path; |
||
| 48 | |||
| 49 | /** @var string php_ext */ |
||
| 50 | protected $php_ext; |
||
| 51 | |||
| 52 | /** @var string ext_path */ |
||
| 53 | protected $ext_path; |
||
| 54 | |||
| 55 | /** @var string Custom form action */ |
||
| 56 | protected $u_action; |
||
| 57 | |||
| 58 | /** @var array Form validation errors */ |
||
| 59 | protected $errors = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param \phpbb\template\template $template Template object |
||
| 65 | * @param \phpbb\user $user User object |
||
| 66 | * @param \phpbb\request\request $request Request object |
||
| 67 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
| 68 | * @param \phpbb\ads\location\manager $location_manager Template location manager object |
||
| 69 | * @param \phpbb\log\log $log The phpBB log system |
||
| 70 | * @param \phpbb\config\db_text $config_text Config text object |
||
| 71 | * @param \phpbb\config\config $config Config object |
||
| 72 | * @param string $root_path phpBB root path |
||
| 73 | * @param string $php_ext PHP extension |
||
| 74 | * @param string $ext_path Path to this extension |
||
| 75 | */ |
||
| 76 | 46 | public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\log\log $log, \phpbb\config\db_text $config_text, \phpbb\config\config $config, $root_path, $php_ext, $ext_path) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Process user request for manage mode |
||
| 93 | * |
||
| 94 | * @return void |
||
| 95 | */ |
||
| 96 | 6 | public function mode_manage() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Process user request for settings mode |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | 3 | public function mode_settings() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Set page url |
||
| 173 | * |
||
| 174 | * @param string $u_action Custom form action |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | 40 | public function set_page_url($u_action) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Get ACP page title for Ads module |
||
| 184 | * |
||
| 185 | * @return string Language string for Ads ACP module |
||
| 186 | */ |
||
| 187 | 1 | public function get_page_title() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Add an advertisement |
||
| 194 | * |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | 13 | public function action_add() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Edit an advertisement |
||
| 241 | * |
||
| 242 | * @return void |
||
| 243 | */ |
||
| 244 | 15 | public function action_edit() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Enable an advertisement |
||
| 304 | * |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | 3 | public function action_enable() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Disable an advertisement |
||
| 314 | * |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | 3 | public function action_disable() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Delete an advertisement |
||
| 324 | * |
||
| 325 | * @return void |
||
| 326 | */ |
||
| 327 | 3 | public function action_delete() |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Display the ads |
||
| 370 | * |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | 1 | public function list_ads() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Perform general tasks |
||
| 411 | * |
||
| 412 | * @return void |
||
| 413 | */ |
||
| 414 | 9 | protected function setup() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Enable/disable an advertisement |
||
| 423 | * |
||
| 424 | * @param bool $enable Enable or disable the advertisement? |
||
| 425 | * @return void |
||
| 426 | */ |
||
| 427 | 4 | protected function ad_enable($enable) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Get admin form data. |
||
| 458 | * |
||
| 459 | * @param string $form_name The form name. |
||
| 460 | * @return array Form data |
||
| 461 | */ |
||
| 462 | 25 | protected function get_form_data($form_name) |
|
| 463 | { |
||
| 464 | $data = array( |
||
| 465 | 25 | 'ad_name' => $this->request->variable('ad_name', '', true), |
|
| 466 | 25 | 'ad_note' => $this->request->variable('ad_note', '', true), |
|
| 467 | 25 | 'ad_code' => $this->request->variable('ad_code', '', true), |
|
| 468 | 25 | 'ad_enabled' => $this->request->variable('ad_enabled', 0), |
|
| 469 | 25 | 'ad_locations' => $this->request->variable('ad_locations', array('')), |
|
| 470 | 25 | 'ad_end_date' => $this->request->variable('ad_end_date', ''), |
|
| 471 | 25 | 'ad_priority' => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY), |
|
| 472 | 25 | 'ad_views_limit' => $this->request->variable('ad_views_limit', 0), |
|
| 473 | 25 | 'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0), |
|
| 474 | 25 | 'ad_owner' => $this->request->variable('ad_owner', '', true), |
|
| 475 | 25 | ); |
|
| 476 | |||
| 477 | // Validate form key |
||
| 478 | 25 | if (!check_form_key($form_name)) |
|
| 479 | 25 | { |
|
| 480 | 2 | $this->errors[] = $this->user->lang('FORM_INVALID'); |
|
| 481 | 2 | } |
|
| 482 | |||
| 483 | // Validate ad name |
||
| 484 | 25 | if ($data['ad_name'] === '') |
|
| 485 | 25 | { |
|
| 486 | 2 | $this->errors[] = $this->user->lang('AD_NAME_REQUIRED'); |
|
| 487 | 2 | } |
|
| 488 | 25 | if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name']) |
|
| 489 | 25 | { |
|
| 490 | 2 | $this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH); |
|
| 491 | 2 | } |
|
| 492 | |||
| 493 | // Validate ad end date |
||
| 494 | 25 | if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $data['ad_end_date'])) |
|
| 495 | 25 | { |
|
| 496 | 4 | $data['ad_end_date'] = (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $data['ad_end_date']); |
|
| 497 | |||
| 498 | 4 | if ($data['ad_end_date'] < time()) |
|
| 499 | 4 | { |
|
| 500 | 2 | $this->errors[] = $this->user->lang('AD_END_DATE_INVALID'); |
|
| 501 | 2 | } |
|
| 502 | 4 | } |
|
| 503 | 21 | else if ($data['ad_end_date'] !== '') |
|
| 504 | 21 | { |
|
| 505 | 2 | $this->errors[] = $this->user->lang('AD_END_DATE_INVALID'); |
|
| 506 | 2 | } |
|
| 507 | else |
||
| 508 | { |
||
| 509 | 19 | $data['ad_end_date'] = 0; |
|
| 510 | } |
||
| 511 | |||
| 512 | // Validate ad priority |
||
| 513 | 25 | if ($data['ad_priority'] < 1 || $data['ad_priority'] > 10) |
|
| 514 | 25 | { |
|
| 515 | 6 | $this->errors[] = $this->user->lang('AD_PRIORITY_INVALID'); |
|
| 516 | 6 | } |
|
| 517 | |||
| 518 | // Validate ad views limit |
||
| 519 | 25 | if ($data['ad_views_limit'] < 0) |
|
| 520 | 25 | { |
|
| 521 | 2 | $this->errors[] = $this->user->lang('AD_VIEWS_LIMIT_INVALID'); |
|
| 522 | 2 | } |
|
| 523 | |||
| 524 | // Validate ad clicks limit |
||
| 525 | 25 | if ($data['ad_clicks_limit'] < 0) |
|
| 526 | 25 | { |
|
| 527 | 2 | $this->errors[] = $this->user->lang('AD_CLICKS_LIMIT_INVALID'); |
|
| 528 | 2 | } |
|
| 529 | |||
| 530 | // Validate ad owner. Username in $data['ad_owner'] will be replaced with user_id. |
||
| 531 | 25 | if (!empty($data['ad_owner'])) |
|
| 532 | 25 | { |
|
| 533 | // Function returns false if everything is OK. |
||
| 534 | 4 | if (user_get_id_name($ad_owner_id, $data['ad_owner'])) |
|
| 535 | 4 | { |
|
| 536 | 2 | $this->errors[] = $this->user->lang('AD_OWNER_INVALID'); |
|
| 537 | 2 | } |
|
| 538 | else |
||
| 539 | { |
||
| 540 | 2 | $data['ad_owner'] = $ad_owner_id[0]; |
|
|
|
|||
| 541 | } |
||
| 542 | 4 | } |
|
| 543 | else |
||
| 544 | { |
||
| 545 | 21 | $data['ad_owner'] = 0; |
|
| 546 | } |
||
| 547 | |||
| 548 | 25 | return $data; |
|
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Assign form data to the template. |
||
| 553 | * |
||
| 554 | * @param array $data The form data. |
||
| 555 | * @return void |
||
| 556 | */ |
||
| 557 | 23 | protected function assign_form_data($data) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * Prepare end date for display |
||
| 577 | * |
||
| 578 | * @param mixed $end_date End date. |
||
| 579 | * @return string End date prepared for display. |
||
| 580 | */ |
||
| 581 | 23 | protected function prepare_end_date($end_date) |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Prepare ad owner for display. Method takes user_id |
||
| 598 | * of the ad owner and returns his/her username. |
||
| 599 | * |
||
| 600 | * @param int $ad_owner User ID |
||
| 601 | * @return string Username belonging to $ad_owner. |
||
| 602 | */ |
||
| 603 | 23 | protected function prepare_ad_owner($ad_owner) |
|
| 616 | /** |
||
| 617 | * Assign template locations data to the template. |
||
| 618 | * |
||
| 619 | * @param mixed $data The form data or nothing. |
||
| 620 | * @return void |
||
| 621 | */ |
||
| 622 | 24 | protected function assign_locations($data = false) |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Prepare advertisement preview |
||
| 637 | * |
||
| 638 | * @param string $code Ad code to preview |
||
| 639 | * @return void |
||
| 640 | */ |
||
| 641 | 2 | protected function ad_preview($code) |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Print success message. |
||
| 648 | * |
||
| 649 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 650 | */ |
||
| 651 | 6 | protected function success() |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Print error message. |
||
| 658 | * |
||
| 659 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 660 | */ |
||
| 661 | 5 | protected function error() |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Log action |
||
| 668 | * |
||
| 669 | * @param string $action Performed action in uppercase |
||
| 670 | * @param string $ad_name Advertisement name |
||
| 671 | * @return void |
||
| 672 | */ |
||
| 673 | 3 | protected function log($action, $ad_name) |
|
| 677 | |||
| 678 | 24 | protected function get_find_username_link() |
|
| 682 | } |
||
| 683 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.