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 FrmListHelper 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 FrmListHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmListHelper { |
||
| 7 | /** |
||
| 8 | * The current list of items |
||
| 9 | * |
||
| 10 | * @since 2.0.18 |
||
| 11 | * @var array |
||
| 12 | * @access public |
||
| 13 | */ |
||
| 14 | public $items; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Various information about the current table |
||
| 18 | * |
||
| 19 | * @since 2.0.18 |
||
| 20 | * @var array |
||
| 21 | * @access protected |
||
| 22 | */ |
||
| 23 | protected $_args; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Various information needed for displaying the pagination |
||
| 27 | * |
||
| 28 | * @since 2.0.18 |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $_pagination_args = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The current screen |
||
| 35 | * |
||
| 36 | * @since 2.0.18 |
||
| 37 | * @var object |
||
| 38 | * @access protected |
||
| 39 | */ |
||
| 40 | protected $screen; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Cached bulk actions |
||
| 44 | * |
||
| 45 | * @since 2.0.18 |
||
| 46 | * @var array |
||
| 47 | * @access private |
||
| 48 | */ |
||
| 49 | private $_actions; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Cached pagination output |
||
| 53 | * |
||
| 54 | * @since 2.0.18 |
||
| 55 | * @var string |
||
| 56 | * @access private |
||
| 57 | */ |
||
| 58 | private $_pagination; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The view switcher modes. |
||
| 62 | * |
||
| 63 | * @since 2.0.18 |
||
| 64 | * @var array |
||
| 65 | * @access protected |
||
| 66 | */ |
||
| 67 | protected $modes = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $params; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Stores the value returned by ->get_column_info() |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $_column_headers; |
||
| 81 | |||
| 82 | protected $compat_fields = array( '_args', '_pagination_args', 'screen', '_actions', '_pagination' ); |
||
| 83 | |||
| 84 | protected $compat_methods = array( |
||
| 85 | 'set_pagination_args', 'get_views', 'get_bulk_actions', 'bulk_actions', |
||
| 86 | 'row_actions', 'view_switcher', 'get_items_per_page', 'pagination', |
||
| 87 | 'get_sortable_columns', 'get_column_info', 'get_table_classes', 'display_tablenav', 'extra_tablenav', |
||
| 88 | 'single_row_columns', |
||
| 89 | ); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Construct the table object |
||
| 93 | */ |
||
| 94 | public function __construct( $args ) { |
||
| 130 | |||
| 131 | public function ajax_user_can() { |
||
| 134 | |||
| 135 | public function get_columns() { |
||
| 138 | |||
| 139 | public function display_rows() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Prepares the list of items for displaying. |
||
| 147 | * @uses FrmListHelper::set_pagination_args() |
||
| 148 | * |
||
| 149 | * @since 2.0.18 |
||
| 150 | * @access public |
||
| 151 | * @abstract |
||
| 152 | */ |
||
| 153 | public function prepare_items() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * An internal method that sets all the necessary pagination arguments |
||
| 159 | * |
||
| 160 | * @param array $args An associative array with information about the pagination |
||
| 161 | * @access protected |
||
| 162 | * |
||
| 163 | * @param array|string $args |
||
| 164 | */ |
||
| 165 | protected function set_pagination_args( $args ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Access the pagination args. |
||
| 187 | * |
||
| 188 | * @since 2.0.18 |
||
| 189 | * @access public |
||
| 190 | * |
||
| 191 | * @param string $key Pagination argument to retrieve. Common values include 'total_items', |
||
| 192 | * 'total_pages', 'per_page', or 'infinite_scroll'. |
||
| 193 | * @return int Number of items that correspond to the given pagination argument. |
||
| 194 | */ |
||
| 195 | public function get_pagination_arg( $key ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Whether the table has items to display or not |
||
| 207 | * |
||
| 208 | * @since 2.0.18 |
||
| 209 | * @access public |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | public function has_items() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Message to be displayed when there are no items |
||
| 219 | * |
||
| 220 | * @since 2.0.18 |
||
| 221 | * @access public |
||
| 222 | */ |
||
| 223 | public function no_items() { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Display the search box. |
||
| 229 | * |
||
| 230 | * @since 2.0.18 |
||
| 231 | * @access public |
||
| 232 | * |
||
| 233 | * @param string $text The search button text |
||
| 234 | * @param string $input_id The search input id |
||
| 235 | */ |
||
| 236 | public function search_box( $text, $input_id ) { |
||
| 254 | |||
| 255 | private function hidden_search_inputs( $param_name ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get an associative array ( id => link ) with the list |
||
| 263 | * of views available on this table. |
||
| 264 | * |
||
| 265 | * @since 2.0.18 |
||
| 266 | * @access protected |
||
| 267 | * |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | protected function get_views() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Display the list of views available on this table. |
||
| 276 | * |
||
| 277 | * @since 2.0.18 |
||
| 278 | * @access public |
||
| 279 | */ |
||
| 280 | public function views() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get an associative array ( option_name => option_title ) with the list |
||
| 308 | * of bulk actions available on this table. |
||
| 309 | * |
||
| 310 | * @since 2.0.18 |
||
| 311 | * @access protected |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | protected function get_bulk_actions() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Display the bulk actions dropdown. |
||
| 321 | * |
||
| 322 | * @since 2.0.18 |
||
| 323 | * @access protected |
||
| 324 | * |
||
| 325 | * @param string $which The location of the bulk actions: 'top' or 'bottom'. |
||
| 326 | * This is designated as optional for backwards-compatibility. |
||
| 327 | */ |
||
| 328 | protected function bulk_actions( $which = '' ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get the current action selected from the bulk actions dropdown. |
||
| 374 | * |
||
| 375 | * @since 2.0.18 |
||
| 376 | * @access public |
||
| 377 | * |
||
| 378 | * @return string|false The action name or False if no action was selected |
||
| 379 | */ |
||
| 380 | public function current_action() { |
||
| 392 | |||
| 393 | private static function get_bulk_action( $action_name ) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Generate row actions div |
||
| 403 | * |
||
| 404 | * @since 2.0.18 |
||
| 405 | * @access protected |
||
| 406 | * |
||
| 407 | * @param array $actions The list of actions |
||
| 408 | * @param bool $always_visible Whether the actions should be always visible |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | protected function row_actions( $actions, $always_visible = false ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Display a view switcher |
||
| 434 | * |
||
| 435 | * @since 2.0.18 |
||
| 436 | * @access protected |
||
| 437 | * |
||
| 438 | * @param string $current_mode |
||
| 439 | */ |
||
| 440 | protected function view_switcher( $current_mode ) { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Get the current page number |
||
| 465 | * |
||
| 466 | * @since 2.0.18 |
||
| 467 | * @access public |
||
| 468 | * |
||
| 469 | * @return int |
||
| 470 | */ |
||
| 471 | public function get_pagenum() { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get number of items to display on a single page |
||
| 483 | * |
||
| 484 | * @since 2.0.18 |
||
| 485 | * @access protected |
||
| 486 | * |
||
| 487 | * @param string $option |
||
| 488 | * @param int $default |
||
| 489 | * @return int |
||
| 490 | */ |
||
| 491 | protected function get_items_per_page( $option, $default = 20 ) { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Display the pagination. |
||
| 515 | * |
||
| 516 | * @since 2.0.18 |
||
| 517 | * @access protected |
||
| 518 | * |
||
| 519 | * @param string $which |
||
| 520 | */ |
||
| 521 | protected function pagination( $which ) { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Get a list of sortable columns. The format is: |
||
| 637 | * 'internal-name' => 'orderby' |
||
| 638 | * or |
||
| 639 | * 'internal-name' => array( 'orderby', true ) |
||
| 640 | * |
||
| 641 | * The second format will make the initial sorting order be descending |
||
| 642 | * |
||
| 643 | * @since 2.0.18 |
||
| 644 | * @access protected |
||
| 645 | * |
||
| 646 | * @return array |
||
| 647 | */ |
||
| 648 | protected function get_sortable_columns() { |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Gets the name of the default primary column. |
||
| 654 | * |
||
| 655 | * @since 4.3.0 |
||
| 656 | * @access protected |
||
| 657 | * |
||
| 658 | * @return string Name of the default primary column, in this case, an empty string. |
||
| 659 | */ |
||
| 660 | protected function get_default_primary_column_name() { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Gets the name of the primary column. |
||
| 680 | * |
||
| 681 | * @since 4.3.0 |
||
| 682 | * @access protected |
||
| 683 | * |
||
| 684 | * @return string The name of the primary column. |
||
| 685 | */ |
||
| 686 | protected function get_primary_column_name() { |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Get a list of all, hidden and sortable columns, with filter applied |
||
| 715 | * |
||
| 716 | * @since 2.0.18 |
||
| 717 | * @access protected |
||
| 718 | * |
||
| 719 | * @return array |
||
| 720 | */ |
||
| 721 | protected function get_column_info() { |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Prevent too many columns from showing on the page |
||
| 773 | * @since 2.05.07 |
||
| 774 | */ |
||
| 775 | private function hide_extra_columns( $columns, &$hidden ) { |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Return number of visible columns |
||
| 796 | * |
||
| 797 | * @since 2.0.18 |
||
| 798 | * @access public |
||
| 799 | * |
||
| 800 | * @return int |
||
| 801 | */ |
||
| 802 | public function get_column_count() { |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Print column headers, accounting for hidden and sortable columns. |
||
| 810 | * |
||
| 811 | * @since 2.0.18 |
||
| 812 | * @access public |
||
| 813 | * |
||
| 814 | * @staticvar int $cb_counter |
||
| 815 | * |
||
| 816 | * @param bool $with_id Whether to set the id attribute or not |
||
| 817 | */ |
||
| 818 | public function print_column_headers( $with_id = true ) { |
||
| 887 | |||
| 888 | /** |
||
| 889 | * Display the table |
||
| 890 | * |
||
| 891 | * @since 2.0.18 |
||
| 892 | * @access public |
||
| 893 | */ |
||
| 894 | public function display() { |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Get a list of CSS classes for the list table table tag. |
||
| 926 | * |
||
| 927 | * @since 2.0.18 |
||
| 928 | * @access protected |
||
| 929 | * |
||
| 930 | * @return array List of CSS classes for the table tag. |
||
| 931 | */ |
||
| 932 | protected function get_table_classes() { |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Generate the table navigation above or below the table |
||
| 938 | * |
||
| 939 | * @since 2.0.18 |
||
| 940 | * @access protected |
||
| 941 | * @param string $which |
||
| 942 | */ |
||
| 943 | protected function display_tablenav( $which ) { |
||
| 962 | |||
| 963 | /** |
||
| 964 | * Extra controls to be displayed between bulk actions and pagination |
||
| 965 | * |
||
| 966 | * @since 2.0.18 |
||
| 967 | * @access protected |
||
| 968 | * |
||
| 969 | * @param string $which |
||
| 970 | */ |
||
| 971 | protected function extra_tablenav( $which ) {} |
||
| 972 | |||
| 973 | /** |
||
| 974 | * Generate the tbody element for the list table. |
||
| 975 | * |
||
| 976 | * @since 2.0.18 |
||
| 977 | * @access public |
||
| 978 | */ |
||
| 979 | public function display_rows_or_placeholder() { |
||
| 988 | |||
| 989 | /** |
||
| 990 | * Generates content for a single row of the table |
||
| 991 | * |
||
| 992 | * @since 2.0.18 |
||
| 993 | * @access public |
||
| 994 | * |
||
| 995 | * @param object $item The current item |
||
| 996 | */ |
||
| 997 | public function single_row( $item ) { |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Generates the columns for a single row of the table |
||
| 1005 | * |
||
| 1006 | * @since 2.0.18 |
||
| 1007 | * @access protected |
||
| 1008 | * |
||
| 1009 | * @param object $item The current item |
||
| 1010 | */ |
||
| 1011 | protected function single_row_columns( $item ) { |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Generates and display row actions links for the list table. |
||
| 1055 | * |
||
| 1056 | * @since 4.3.0 |
||
| 1057 | * @access protected |
||
| 1058 | * |
||
| 1059 | * @param object $item The item being acted upon. |
||
| 1060 | * @param string $column_name Current column name. |
||
| 1061 | * @param string $primary Primary column name. |
||
| 1062 | * @return string The row actions output. In this case, an empty string. |
||
| 1063 | */ |
||
| 1064 | protected function handle_row_actions( $item, $column_name, $primary ) { |
||
| 1067 | |||
| 1068 | /** |
||
| 1069 | * Handle an incoming ajax request (called from admin-ajax.php) |
||
| 1070 | * |
||
| 1071 | * @since 2.0.18 |
||
| 1072 | * @access public |
||
| 1073 | */ |
||
| 1074 | public function ajax_response() { |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Send required variables to JavaScript land |
||
| 1104 | * |
||
| 1105 | * @access public |
||
| 1106 | */ |
||
| 1107 | public function _js_vars() { |
||
| 1118 | } |
||
| 1119 |