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 qte 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 qte, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class qte |
||
| 16 | { |
||
| 17 | const KEEP = -2; |
||
| 18 | const REMOVE = -1; |
||
| 19 | |||
| 20 | /** @var \phpbb\request\request */ |
||
| 21 | protected $request; |
||
| 22 | |||
| 23 | /** @var \phpbb\cache\driver\driver_interface */ |
||
| 24 | protected $cache; |
||
| 25 | |||
| 26 | /** @var \phpbb\config\config */ |
||
| 27 | protected $config; |
||
| 28 | |||
| 29 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 30 | protected $db; |
||
| 31 | |||
| 32 | /** @var \phpbb\template\template */ |
||
| 33 | protected $template; |
||
| 34 | |||
| 35 | /** @var \phpbb\user */ |
||
| 36 | protected $user; |
||
| 37 | |||
| 38 | /** @var \phpbb\log\log */ |
||
| 39 | protected $log; |
||
| 40 | |||
| 41 | /** @var string */ |
||
| 42 | protected $root_path; |
||
| 43 | |||
| 44 | /** @var string */ |
||
| 45 | protected $php_ext; |
||
| 46 | |||
| 47 | /** @var string */ |
||
| 48 | protected $table_prefix; |
||
| 49 | |||
| 50 | /** @var array */ |
||
| 51 | private $_attr = array(); |
||
| 52 | |||
| 53 | /** @var array */ |
||
| 54 | private $_name = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor |
||
| 58 | * |
||
| 59 | * @param \phpbb\request\request $request Request object |
||
| 60 | * @param \phpbb\cache\driver\driver_interface $cache Cache object |
||
| 61 | * @param \phpbb\config\config $config Config object |
||
| 62 | * @param \phpbb\db\driver\driver_interface $db Database object |
||
| 63 | * @param \phpbb\template\template $template Template object |
||
| 64 | * @param \phpbb\user $user User object |
||
| 65 | * @param \phpbb\log\log $log Log object |
||
| 66 | * @param string $root_path phpBB root path |
||
| 67 | * @param string $php_ext phpEx |
||
| 68 | * @param string $table_prefix Prefix tables |
||
| 69 | */ |
||
| 70 | public function __construct(\phpbb\request\request $request, \phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\log\log $log, $root_path, $php_ext, $table_prefix) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get topic attributes username |
||
| 89 | * |
||
| 90 | * @param array $topic_list Topic ids |
||
| 91 | * |
||
| 92 | * @return null |
||
| 93 | */ |
||
| 94 | public function get_users_by_topic_id($topic_list) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get attribute name |
||
| 119 | * |
||
| 120 | * @param int $attr_id The attribute id |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function get_attr_name_by_id($attr_id) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get attribute author |
||
| 131 | * |
||
| 132 | * @param int $user_id User id |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function get_users_by_user_id($user_id) |
||
| 137 | { |
||
| 138 | $sql = 'SELECT user_id, username, user_colour |
||
| 139 | FROM ' . USERS_TABLE . ' |
||
| 140 | WHERE user_id = ' . (int) $user_id; |
||
| 141 | $result = $this->db->sql_query($sql); |
||
| 142 | |||
| 143 | $this->_name = array(); |
||
| 144 | while ( $row = $this->db->sql_fetchrow($result) ) |
||
| 145 | { |
||
| 146 | $this->_name[$row['user_id']] = array( |
||
| 147 | 'user_id' => (int) $row['user_id'], |
||
| 148 | 'username' => $row['username'], |
||
| 149 | 'user_colour' => $row['user_colour'], |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | $this->db->sql_freeresult(); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Generate a list of attributes based on permissions |
||
| 157 | * |
||
| 158 | * @param int $forum_id Forum id |
||
| 159 | * @param int $author_id Topic author id |
||
| 160 | * @param int $attribute_id Current attribute id |
||
| 161 | * @param array $hide_attr Groups which can't delete attribute in this forum |
||
| 162 | * @param string $viewtopic_url Topic's url |
||
| 163 | * |
||
| 164 | * @return null |
||
| 165 | */ |
||
| 166 | public function attr_select($forum_id = 0, $author_id = 0, $attribute_id = 0, $hide_attr = array(), $viewtopic_url = '') |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Generate a list of all attributes for search page |
||
| 237 | * |
||
| 238 | * @return null |
||
| 239 | */ |
||
| 240 | public function attr_search() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Generate a list of attributes for viewforum page |
||
| 290 | * |
||
| 291 | * @param int $forum_id Forum id |
||
| 292 | * @param int $attribute_id Current attribute id |
||
| 293 | * |
||
| 294 | * @return null |
||
| 295 | */ |
||
| 296 | View Code Duplication | public function attr_sort($forum_id = 0, $attribute_id = 0) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Generate a default attribute list for a forum |
||
| 353 | * |
||
| 354 | * @param int $forum_id Forum id |
||
| 355 | * @param int $attribute_id Current attribute id |
||
| 356 | * |
||
| 357 | * @return null |
||
| 358 | */ |
||
| 359 | View Code Duplication | public function attr_default($forum_id = 0, $attribute_id = 0) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Generate attribute for topic title |
||
| 416 | * |
||
| 417 | * @param int $attribute_id Current attribute id |
||
| 418 | * @param int $user_id Current attribute user id |
||
| 419 | * @param int $timestamp Attribute timestamp |
||
| 420 | * |
||
| 421 | * @return string Attribute html code |
||
| 422 | */ |
||
| 423 | public function attr_display($attribute_id = 0, $user_id = 0, $timestamp = 0) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Generate attribute for page title |
||
| 453 | * |
||
| 454 | * @param int $attribute_id Current attribute id |
||
| 455 | * @param int $user_id Current attribute user id |
||
| 456 | * @param int $timestamp Attribute timestamp |
||
| 457 | * |
||
| 458 | * @return string attribute html code |
||
| 459 | */ |
||
| 460 | public function attr_title($attribute_id = 0, $user_id = 0, $timestamp = 0) |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * Change topic attribute |
||
| 489 | * |
||
| 490 | * @param int $attribute_id New attribute id |
||
| 491 | * @param int $topic_id The id of the topic |
||
| 492 | * @param int $forum_id The id of the forum |
||
| 493 | * @param int $topic_attribute Current attribute id |
||
| 494 | * @param array $hide_attr Groups which can't delete attribute in this forum |
||
| 495 | * |
||
| 496 | * @return null |
||
| 497 | */ |
||
| 498 | public function attr_apply($attribute_id = 0, $topic_id = 0, $forum_id = 0, $topic_attribute = 0, $hide_attr = array()) |
||
| 499 | { |
||
| 500 | if (empty($topic_id) || empty($forum_id) || empty($attribute_id)) |
||
| 501 | { |
||
| 502 | return; |
||
| 503 | } |
||
| 504 | |||
| 505 | if ($attribute_id == \ernadoo\qte\qte::REMOVE && !$this->_check_auth_remove_attr($user_groups, $hide_attr)) |
||
|
|
|||
| 506 | { |
||
| 507 | return; |
||
| 508 | } |
||
| 509 | |||
| 510 | // time ! |
||
| 511 | $current_time = time(); |
||
| 512 | |||
| 513 | if ($attribute_id == \ernadoo\qte\qte::REMOVE) |
||
| 514 | { |
||
| 515 | $fields = array( |
||
| 516 | 'topic_attr_id' => 0, |
||
| 517 | 'topic_attr_user' => 0, |
||
| 518 | 'topic_attr_time' => 0, |
||
| 519 | ); |
||
| 520 | } |
||
| 521 | else |
||
| 522 | { |
||
| 523 | $fields = array( |
||
| 524 | 'topic_attr_id' => $attribute_id, |
||
| 525 | 'topic_attr_user' => $this->user->data['user_id'], |
||
| 526 | 'topic_attr_time' => $current_time, |
||
| 527 | ); |
||
| 528 | } |
||
| 529 | |||
| 530 | $sql = 'UPDATE ' . TOPICS_TABLE . ' |
||
| 531 | SET ' . $this->db->sql_build_array('UPDATE', $fields) . ' |
||
| 532 | WHERE topic_id = ' . (int) $topic_id; |
||
| 533 | $this->db->sql_query($sql); |
||
| 534 | |||
| 535 | $sql = 'SELECT topic_id |
||
| 536 | FROM ' . TOPICS_TABLE . ' |
||
| 537 | WHERE topic_moved_id = ' . (int) $topic_id; |
||
| 538 | $result = $this->db->sql_query($sql); |
||
| 539 | $shadow_topic_id = (int) $this->db->sql_fetchfield('topic_id'); |
||
| 540 | $this->db->sql_freeresult($result); |
||
| 541 | |||
| 542 | if (!empty($shadow_topic_id)) |
||
| 543 | { |
||
| 544 | $sql = 'UPDATE ' . TOPICS_TABLE . ' |
||
| 545 | SET ' . $this->db->sql_build_array('UPDATE', $fields) . ' |
||
| 546 | WHERE topic_id = ' . $shadow_topic_id; |
||
| 547 | $this->db->sql_query($sql); |
||
| 548 | } |
||
| 549 | |||
| 550 | $meta_url = append_sid($this->root_path . 'viewtopic.' . $this->php_ext, array('f' => $forum_id, 't' => $topic_id)); |
||
| 551 | meta_refresh(3, $meta_url); |
||
| 552 | |||
| 553 | // load language |
||
| 554 | $this->user->add_lang('posting'); |
||
| 555 | $this->user->add_lang_ext('ernadoo/qte', 'attributes'); |
||
| 556 | |||
| 557 | $message = $this->user->lang['QTE_ATTRIBUTE_' . ($attribute_id == -1 ? 'REMOVED' : (empty($topic_attribute) ? 'ADDED' : 'UPDATED'))] . '<br /><br />' . sprintf($this->user->lang['VIEW_MESSAGE'], '<a href="' . $meta_url . '">', '</a>'); |
||
| 558 | $message .= '<br /><br />' . sprintf($this->user->lang['RETURN_FORUM'], '<a href="' . append_sid($this->root_path . 'viewforum.' . $this->php_ext, array('f' => $forum_id)) . '">', '</a>'); |
||
| 559 | |||
| 560 | if ($this->request->is_ajax()) |
||
| 561 | { |
||
| 562 | $json_response = new \phpbb\json_response; |
||
| 563 | $json_response->send(array( |
||
| 564 | 'success' => true, |
||
| 565 | |||
| 566 | 'MESSAGE_TITLE' => $this->user->lang['INFORMATION'], |
||
| 567 | 'MESSAGE_TEXT' => $message, |
||
| 568 | 'NEW_ATTRIBUTE' => $this->attr_display($attribute_id, $this->user->data['user_id'], $current_time), |
||
| 569 | )); |
||
| 570 | } |
||
| 571 | |||
| 572 | trigger_error($message); |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Change topic attribute in mcp |
||
| 577 | * |
||
| 578 | * @param int $attribute_id New attribute id |
||
| 579 | * @param array $topic_ids Topics ids |
||
| 580 | * |
||
| 581 | * @return null |
||
| 582 | */ |
||
| 583 | public function mcp_attr_apply($attribute_id = 0, $topic_ids = array()) |
||
| 584 | { |
||
| 585 | // load language |
||
| 586 | $this->user->add_lang_ext('ernadoo/qte', 'attributes'); |
||
| 587 | |||
| 588 | if (!sizeof($topic_ids)) |
||
| 589 | { |
||
| 590 | trigger_error('NO_TOPIC_SELECTED'); |
||
| 591 | } |
||
| 592 | |||
| 593 | if (!phpbb_check_ids($topic_ids, TOPICS_TABLE, 'topic_id')) |
||
| 594 | { |
||
| 595 | return; |
||
| 596 | } |
||
| 597 | |||
| 598 | // time ! |
||
| 599 | $current_time = time(); |
||
| 600 | |||
| 601 | $sql = 'SELECT topic_id, forum_id, topic_title, topic_attr_id |
||
| 602 | FROM ' . TOPICS_TABLE . ' |
||
| 603 | WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $topic_ids)); |
||
| 604 | $result = $this->db->sql_query($sql); |
||
| 605 | |||
| 606 | // log this action |
||
| 607 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 608 | { |
||
| 609 | $message = ($attribute_id == -1) ? 'REMOVED' : (empty($row['topic_attr_id']) ? 'ADDED' : 'UPDATED'); |
||
| 610 | $additional_data = array( |
||
| 611 | 'forum_id' => $row['forum_id'], |
||
| 612 | 'topic_id' => $row['topic_id'], |
||
| 613 | $row['topic_title'], |
||
| 614 | ); |
||
| 615 | $this->log->add('mod', $this->user->data['user_id'], $this->user->ip, 'MCP_ATTRIBUTE_' . $message, $current_time, $additional_data); |
||
| 616 | } |
||
| 617 | $this->db->sql_freeresult($result); |
||
| 618 | |||
| 619 | if ($attribute_id == -1) |
||
| 620 | { |
||
| 621 | $fields = array( |
||
| 622 | 'topic_attr_id' => 0, |
||
| 623 | 'topic_attr_user' => 0, |
||
| 624 | 'topic_attr_time' => 0, |
||
| 625 | ); |
||
| 626 | } |
||
| 627 | else |
||
| 628 | { |
||
| 629 | $fields = array( |
||
| 630 | 'topic_attr_id' => $attribute_id, |
||
| 631 | 'topic_attr_user' => $this->user->data['user_id'], |
||
| 632 | 'topic_attr_time' => $current_time, |
||
| 633 | ); |
||
| 634 | } |
||
| 635 | |||
| 636 | $sql = 'UPDATE ' . TOPICS_TABLE . ' |
||
| 637 | SET ' . $this->db->sql_build_array('UPDATE', $fields) . ' |
||
| 638 | WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $topic_ids)); |
||
| 639 | $this->db->sql_query($sql); |
||
| 640 | |||
| 641 | $sql = 'SELECT topic_id |
||
| 642 | FROM ' . TOPICS_TABLE . ' |
||
| 643 | WHERE ' . $this->db->sql_in_set('topic_moved_id', array_map('intval', $topic_ids)); |
||
| 644 | $result = $this->db->sql_query($sql); |
||
| 645 | |||
| 646 | $shadow_topic_ids = array(); |
||
| 647 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 648 | { |
||
| 649 | $shadow_topic_ids[] = (int) $row['topic_id']; |
||
| 650 | } |
||
| 651 | $this->db->sql_freeresult($result); |
||
| 652 | |||
| 653 | if (sizeof($shadow_topic_ids)) |
||
| 654 | { |
||
| 655 | $sql = 'UPDATE ' . TOPICS_TABLE . ' |
||
| 656 | SET ' . $this->db->sql_build_array('UPDATE', $fields) . ' |
||
| 657 | WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $shadow_topic_ids)); |
||
| 658 | $this->db->sql_query($sql); |
||
| 659 | } |
||
| 660 | |||
| 661 | $redirect = $this->request->variable('redirect', $this->user->data['session_page']); |
||
| 662 | |||
| 663 | meta_refresh(3, $redirect); |
||
| 664 | trigger_error($this->user->lang['QTE_TOPIC' . (sizeof($topic_ids) == 1 ? '' : 'S') . '_ATTRIBUTE_' . (isset($message) ? $message : 'ADDED')] . '<br /><br />' . sprintf($this->user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>')); |
||
| 665 | |||
| 666 | return; |
||
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Getter... |
||
| 671 | * |
||
| 672 | * @return array |
||
| 673 | */ |
||
| 674 | public function getAttr() |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Generate list of groups |
||
| 681 | * |
||
| 682 | * @param int $group_ids The default groups id to mark as selected |
||
| 683 | * @param array|bool $exclude_ids The group ids to exclude from the list, false (default) if you whish to exclude no id |
||
| 684 | * @param bool $manage_founder If set to false (default) all groups are returned, if 0 only those groups returned not being managed by founders only, if 1 only those groups returned managed by founders only. |
||
| 685 | * |
||
| 686 | * @return string The list of options. |
||
| 687 | */ |
||
| 688 | public function qte_group_select($group_ids, $exclude_ids = array(), $manage_founder = false) |
||
| 712 | |||
| 713 | /** |
||
| 714 | * borrowed from "Categories Hierarchy" : used to check if a language key exists |
||
| 715 | * @todo delete |
||
| 716 | */ |
||
| 717 | public function attr_lng_key($key) |
||
| 724 | |||
| 725 | // borrowed from "Categories Hierarchy" : used to check if a image key exists |
||
| 726 | public function attr_img_key($key, $alt) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Build class and style attribute |
||
| 733 | * |
||
| 734 | * @param string $a_name Attribute name |
||
| 735 | * @param string $a_colour Attribute color |
||
| 736 | * @return string html code |
||
| 737 | */ |
||
| 738 | public function attr_colour($a_name, $a_colour) |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Check if user can apply an attribute |
||
| 751 | * |
||
| 752 | * @param array $attr_auth Forum auth |
||
| 753 | * @param int $forum_id Forum id |
||
| 754 | * @param array $user_groups User's groups |
||
| 755 | * @param int $author_id Topic author id |
||
| 756 | * @return bool |
||
| 757 | */ |
||
| 758 | private function _check_auth_attribute($attr_auth, $forum_id, $user_groups, $author_id) |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Check if user can delete an attribute |
||
| 776 | * |
||
| 777 | * @param array $user_groups User's groups |
||
| 778 | * @param array $hide_attr Groups which can't delete attribute in a forum |
||
| 779 | * @return bool |
||
| 780 | */ |
||
| 781 | private function _check_auth_remove_attr(&$user_groups, $hide_attr) |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Get attributes from database |
||
| 812 | * |
||
| 813 | * @return null |
||
| 814 | */ |
||
| 815 | private function _get_attributes() |
||
| 844 | } |
||
| 845 |
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.