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 ) { |
||
| 600 | |||
| 601 | private function disabled_pages( $total_pages ) { |
||
| 622 | |||
| 623 | private function link_label( $link ) { |
||
| 632 | |||
| 633 | private function current_url() { |
||
| 638 | |||
| 639 | private function add_page_link( $atts ) { |
||
| 647 | |||
| 648 | private function add_disabled_link( $label ) { |
||
| 651 | |||
| 652 | private function add_active_link( $atts ) { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Get a list of sortable columns. The format is: |
||
| 663 | * 'internal-name' => 'orderby' |
||
| 664 | * or |
||
| 665 | * 'internal-name' => array( 'orderby', true ) |
||
| 666 | * |
||
| 667 | * The second format will make the initial sorting order be descending |
||
| 668 | * |
||
| 669 | * @since 2.0.18 |
||
| 670 | * @access protected |
||
| 671 | * |
||
| 672 | * @return array |
||
| 673 | */ |
||
| 674 | protected function get_sortable_columns() { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Gets the name of the default primary column. |
||
| 680 | * |
||
| 681 | * @since 4.3.0 |
||
| 682 | * @access protected |
||
| 683 | * |
||
| 684 | * @return string Name of the default primary column, in this case, an empty string. |
||
| 685 | */ |
||
| 686 | protected function get_default_primary_column_name() { |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Gets the name of the primary column. |
||
| 706 | * |
||
| 707 | * @since 4.3.0 |
||
| 708 | * @access protected |
||
| 709 | * |
||
| 710 | * @return string The name of the primary column. |
||
| 711 | */ |
||
| 712 | protected function get_primary_column_name() { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Get a list of all, hidden and sortable columns, with filter applied |
||
| 741 | * |
||
| 742 | * @since 2.0.18 |
||
| 743 | * @access protected |
||
| 744 | * |
||
| 745 | * @return array |
||
| 746 | */ |
||
| 747 | protected function get_column_info() { |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Return number of visible columns |
||
| 798 | * |
||
| 799 | * @since 2.0.18 |
||
| 800 | * @access public |
||
| 801 | * |
||
| 802 | * @return int |
||
| 803 | */ |
||
| 804 | public function get_column_count() { |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Print column headers, accounting for hidden and sortable columns. |
||
| 812 | * |
||
| 813 | * @since 2.0.18 |
||
| 814 | * @access public |
||
| 815 | * |
||
| 816 | * @staticvar int $cb_counter |
||
| 817 | * |
||
| 818 | * @param bool $with_id Whether to set the id attribute or not |
||
| 819 | */ |
||
| 820 | public function print_column_headers( $with_id = true ) { |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Display the table |
||
| 892 | * |
||
| 893 | * @since 2.0.18 |
||
| 894 | * @access public |
||
| 895 | */ |
||
| 896 | public function display() { |
||
| 925 | |||
| 926 | /** |
||
| 927 | * Get a list of CSS classes for the list table table tag. |
||
| 928 | * |
||
| 929 | * @since 2.0.18 |
||
| 930 | * @access protected |
||
| 931 | * |
||
| 932 | * @return array List of CSS classes for the table tag. |
||
| 933 | */ |
||
| 934 | protected function get_table_classes() { |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Generate the table navigation above or below the table |
||
| 940 | * |
||
| 941 | * @since 2.0.18 |
||
| 942 | * @access protected |
||
| 943 | * @param string $which |
||
| 944 | */ |
||
| 945 | protected function display_tablenav( $which ) { |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Extra controls to be displayed between bulk actions and pagination |
||
| 967 | * |
||
| 968 | * @since 2.0.18 |
||
| 969 | * @access protected |
||
| 970 | * |
||
| 971 | * @param string $which |
||
| 972 | */ |
||
| 973 | protected function extra_tablenav( $which ) {} |
||
| 974 | |||
| 975 | /** |
||
| 976 | * Generate the tbody element for the list table. |
||
| 977 | * |
||
| 978 | * @since 2.0.18 |
||
| 979 | * @access public |
||
| 980 | */ |
||
| 981 | public function display_rows_or_placeholder() { |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Generates content for a single row of the table |
||
| 993 | * |
||
| 994 | * @since 2.0.18 |
||
| 995 | * @access public |
||
| 996 | * |
||
| 997 | * @param object $item The current item |
||
| 998 | */ |
||
| 999 | public function single_row( $item ) { |
||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * Generates the columns for a single row of the table |
||
| 1007 | * |
||
| 1008 | * @since 2.0.18 |
||
| 1009 | * @access protected |
||
| 1010 | * |
||
| 1011 | * @param object $item The current item |
||
| 1012 | */ |
||
| 1013 | protected function single_row_columns( $item ) { |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Generates and display row actions links for the list table. |
||
| 1057 | * |
||
| 1058 | * @since 4.3.0 |
||
| 1059 | * @access protected |
||
| 1060 | * |
||
| 1061 | * @param object $item The item being acted upon. |
||
| 1062 | * @param string $column_name Current column name. |
||
| 1063 | * @param string $primary Primary column name. |
||
| 1064 | * @return string The row actions output. In this case, an empty string. |
||
| 1065 | */ |
||
| 1066 | protected function handle_row_actions( $item, $column_name, $primary ) { |
||
| 1069 | |||
| 1070 | /** |
||
| 1071 | * Handle an incoming ajax request (called from admin-ajax.php) |
||
| 1072 | * |
||
| 1073 | * @since 2.0.18 |
||
| 1074 | * @access public |
||
| 1075 | */ |
||
| 1076 | public function ajax_response() { |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Send required variables to JavaScript land |
||
| 1106 | * |
||
| 1107 | * @access public |
||
| 1108 | */ |
||
| 1109 | public function _js_vars() { |
||
| 1120 | } |
||
| 1121 |