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 | * @since 4.07 |
||
| 18 | */ |
||
| 19 | public $total_items = false; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Various information about the current table |
||
| 23 | * |
||
| 24 | * @since 2.0.18 |
||
| 25 | * @var array |
||
| 26 | * @access protected |
||
| 27 | */ |
||
| 28 | protected $_args; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Various information needed for displaying the pagination |
||
| 32 | * |
||
| 33 | * @since 2.0.18 |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $_pagination_args = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The current screen |
||
| 40 | * |
||
| 41 | * @since 2.0.18 |
||
| 42 | * @var object |
||
| 43 | * @access protected |
||
| 44 | */ |
||
| 45 | protected $screen; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Cached bulk actions |
||
| 49 | * |
||
| 50 | * @since 2.0.18 |
||
| 51 | * @var array |
||
| 52 | * @access private |
||
| 53 | */ |
||
| 54 | private $_actions; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Cached pagination output |
||
| 58 | * |
||
| 59 | * @since 2.0.18 |
||
| 60 | * @var string |
||
| 61 | * @access private |
||
| 62 | */ |
||
| 63 | private $_pagination; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The view switcher modes. |
||
| 67 | * |
||
| 68 | * @since 2.0.18 |
||
| 69 | * @var array |
||
| 70 | * @access protected |
||
| 71 | */ |
||
| 72 | protected $modes = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $params; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Stores the value returned by ->get_column_info() |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $_column_headers; |
||
| 86 | |||
| 87 | protected $compat_fields = array( '_args', '_pagination_args', 'screen', '_actions', '_pagination' ); |
||
| 88 | |||
| 89 | protected $compat_methods = array( |
||
| 90 | 'set_pagination_args', |
||
| 91 | 'get_views', |
||
| 92 | 'get_bulk_actions', |
||
| 93 | 'bulk_actions', |
||
| 94 | 'row_actions', |
||
| 95 | 'view_switcher', |
||
| 96 | 'get_items_per_page', |
||
| 97 | 'pagination', |
||
| 98 | 'get_sortable_columns', |
||
| 99 | 'get_column_info', |
||
| 100 | 'get_table_classes', |
||
| 101 | 'display_tablenav', |
||
| 102 | 'extra_tablenav', |
||
| 103 | 'single_row_columns', |
||
| 104 | ); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Construct the table object |
||
| 108 | */ |
||
| 109 | public function __construct( $args ) { |
||
| 148 | |||
| 149 | public function ajax_user_can() { |
||
| 152 | |||
| 153 | public function get_columns() { |
||
| 156 | |||
| 157 | public function display_rows() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Prepares the list of items for displaying. |
||
| 165 | * |
||
| 166 | * @uses FrmListHelper::set_pagination_args() |
||
| 167 | * |
||
| 168 | * @since 2.0.18 |
||
| 169 | * @access public |
||
| 170 | * @abstract |
||
| 171 | */ |
||
| 172 | public function prepare_items() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @since 3.0 |
||
| 178 | */ |
||
| 179 | protected function get_param( $args ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * An internal method that sets all the necessary pagination arguments |
||
| 192 | * |
||
| 193 | * @param array $args An associative array with information about the pagination |
||
| 194 | * |
||
| 195 | * @access protected |
||
| 196 | * |
||
| 197 | * @param array|string $args |
||
| 198 | */ |
||
| 199 | protected function set_pagination_args( $args ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Access the pagination args. |
||
| 224 | * |
||
| 225 | * @since 2.0.18 |
||
| 226 | * @access public |
||
| 227 | * |
||
| 228 | * @param string $key Pagination argument to retrieve. Common values include 'total_items', |
||
| 229 | * 'total_pages', 'per_page', or 'infinite_scroll'. |
||
| 230 | * |
||
| 231 | * @return int Number of items that correspond to the given pagination argument. |
||
| 232 | */ |
||
| 233 | public function get_pagination_arg( $key ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Whether the table has items to display or not |
||
| 245 | * |
||
| 246 | * @since 2.0.18 |
||
| 247 | * @access public |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function has_items() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Message to be displayed when there are no items |
||
| 257 | * |
||
| 258 | * @since 2.0.18 |
||
| 259 | * @access public |
||
| 260 | */ |
||
| 261 | public function no_items() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Display the search box. |
||
| 267 | * |
||
| 268 | * @since 2.0.18 |
||
| 269 | * @access public |
||
| 270 | * |
||
| 271 | * @param string $text The search button text |
||
| 272 | * @param string $input_id The search input id |
||
| 273 | */ |
||
| 274 | public function search_box( $text, $input_id ) { |
||
| 285 | |||
| 286 | private function hidden_search_inputs( $param_name ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get an associative array ( id => link ) with the list |
||
| 295 | * of views available on this table. |
||
| 296 | * |
||
| 297 | * @since 2.0.18 |
||
| 298 | * @access protected |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | protected function get_views() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Display the list of views available on this table. |
||
| 308 | * |
||
| 309 | * @since 2.0.18 |
||
| 310 | * @access public |
||
| 311 | */ |
||
| 312 | public function views() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get an associative array ( option_name => option_title ) with the list |
||
| 340 | * of bulk actions available on this table. |
||
| 341 | * |
||
| 342 | * @since 2.0.18 |
||
| 343 | * @access protected |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | protected function get_bulk_actions() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Display the bulk actions dropdown. |
||
| 353 | * |
||
| 354 | * @since 2.0.18 |
||
| 355 | * @access protected |
||
| 356 | * |
||
| 357 | * @param string $which The location of the bulk actions: 'top' or 'bottom'. |
||
| 358 | * This is designated as optional for backwards-compatibility. |
||
| 359 | */ |
||
| 360 | protected function bulk_actions( $which = '' ) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get the current action selected from the bulk actions dropdown. |
||
| 407 | * |
||
| 408 | * @since 2.0.18 |
||
| 409 | * @access public |
||
| 410 | * |
||
| 411 | * @return string|false The action name or False if no action was selected |
||
| 412 | */ |
||
| 413 | public function current_action() { |
||
| 425 | |||
| 426 | private static function get_bulk_action( $action_name ) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Generate row actions div |
||
| 443 | * |
||
| 444 | * @since 2.0.18 |
||
| 445 | * @access protected |
||
| 446 | * |
||
| 447 | * @param array $actions The list of actions |
||
| 448 | * @param bool $always_visible Whether the actions should be always visible |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | protected function row_actions( $actions, $always_visible = false ) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Display a view switcher |
||
| 476 | * |
||
| 477 | * @since 2.0.18 |
||
| 478 | * @access protected |
||
| 479 | * |
||
| 480 | * @param string $current_mode |
||
| 481 | */ |
||
| 482 | protected function view_switcher( $current_mode ) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Get the current page number |
||
| 507 | * |
||
| 508 | * @since 2.0.18 |
||
| 509 | * @access public |
||
| 510 | * |
||
| 511 | * @return int |
||
| 512 | */ |
||
| 513 | public function get_pagenum() { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get number of items to display on a single page |
||
| 525 | * |
||
| 526 | * @since 2.0.18 |
||
| 527 | * @access protected |
||
| 528 | * |
||
| 529 | * @param string $option |
||
| 530 | * @param int $default |
||
| 531 | * |
||
| 532 | * @return int |
||
| 533 | */ |
||
| 534 | protected function get_items_per_page( $option, $default = 20 ) { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Display the pagination. |
||
| 558 | * |
||
| 559 | * @since 2.0.18 |
||
| 560 | * @access protected |
||
| 561 | * |
||
| 562 | * @param string $which |
||
| 563 | */ |
||
| 564 | protected function pagination( $which ) { |
||
| 655 | |||
| 656 | private function disabled_pages( $total_pages ) { |
||
| 681 | |||
| 682 | private function link_label( $link ) { |
||
| 692 | |||
| 693 | private function current_url() { |
||
| 698 | |||
| 699 | private function add_page_link( $atts ) { |
||
| 708 | |||
| 709 | private function add_disabled_link( $label ) { |
||
| 712 | |||
| 713 | private function add_active_link( $atts ) { |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Get a list of sortable columns. The format is: |
||
| 728 | * 'internal-name' => 'orderby' |
||
| 729 | * or |
||
| 730 | * 'internal-name' => array( 'orderby', true ) |
||
| 731 | * |
||
| 732 | * The second format will make the initial sorting order be descending |
||
| 733 | * |
||
| 734 | * @since 2.0.18 |
||
| 735 | * @access protected |
||
| 736 | * |
||
| 737 | * @return array |
||
| 738 | */ |
||
| 739 | protected function get_sortable_columns() { |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Gets the name of the default primary column. |
||
| 745 | * |
||
| 746 | * @since 4.3.0 |
||
| 747 | * @access protected |
||
| 748 | * |
||
| 749 | * @return string Name of the default primary column, in this case, an empty string. |
||
| 750 | */ |
||
| 751 | protected function get_default_primary_column_name() { |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Gets the name of the primary column. |
||
| 771 | * |
||
| 772 | * @since 4.3.0 |
||
| 773 | * @access protected |
||
| 774 | * |
||
| 775 | * @return string The name of the primary column. |
||
| 776 | */ |
||
| 777 | protected function get_primary_column_name() { |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Get a list of all, hidden and sortable columns, with filter applied |
||
| 806 | * |
||
| 807 | * @since 2.0.18 |
||
| 808 | * @access protected |
||
| 809 | * |
||
| 810 | * @return array |
||
| 811 | */ |
||
| 812 | protected function get_column_info() { |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Return number of visible columns |
||
| 864 | * |
||
| 865 | * @since 2.0.18 |
||
| 866 | * @access public |
||
| 867 | * |
||
| 868 | * @return int |
||
| 869 | */ |
||
| 870 | public function get_column_count() { |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Print column headers, accounting for hidden and sortable columns. |
||
| 879 | * |
||
| 880 | * @since 2.0.18 |
||
| 881 | * @access public |
||
| 882 | * |
||
| 883 | * @staticvar int $cb_counter |
||
| 884 | * |
||
| 885 | * @param bool $with_id Whether to set the id attribute or not |
||
| 886 | */ |
||
| 887 | public function print_column_headers( $with_id = true ) { |
||
| 960 | |||
| 961 | /** |
||
| 962 | * Display the table |
||
| 963 | * |
||
| 964 | * @since 2.0.18 |
||
| 965 | * @access public |
||
| 966 | */ |
||
| 967 | public function display() { |
||
| 996 | |||
| 997 | /** |
||
| 998 | * Get a list of CSS classes for the list table table tag. |
||
| 999 | * |
||
| 1000 | * @since 2.0.18 |
||
| 1001 | * @access protected |
||
| 1002 | * |
||
| 1003 | * @return array List of CSS classes for the table tag. |
||
| 1004 | */ |
||
| 1005 | protected function get_table_classes() { |
||
| 1008 | |||
| 1009 | /** |
||
| 1010 | * Generate the table navigation above or below the table |
||
| 1011 | * |
||
| 1012 | * @since 2.0.18 |
||
| 1013 | * @access protected |
||
| 1014 | * |
||
| 1015 | * @param string $which |
||
| 1016 | */ |
||
| 1017 | protected function display_tablenav( $which ) { |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Use this to exclude the footer labels and bulk items. |
||
| 1046 | * When close together, it feels like duplicates. |
||
| 1047 | * |
||
| 1048 | * @since 4.07 |
||
| 1049 | */ |
||
| 1050 | protected function has_min_items( $limit = 5 ) { |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Extra controls to be displayed between bulk actions and pagination |
||
| 1056 | * |
||
| 1057 | * @since 2.0.18 |
||
| 1058 | * @access protected |
||
| 1059 | * |
||
| 1060 | * @param string $which |
||
| 1061 | */ |
||
| 1062 | protected function extra_tablenav( $which ) { |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Generate the tbody element for the list table. |
||
| 1067 | * |
||
| 1068 | * @since 2.0.18 |
||
| 1069 | * @access public |
||
| 1070 | */ |
||
| 1071 | public function display_rows_or_placeholder() { |
||
| 1080 | |||
| 1081 | /** |
||
| 1082 | * Generates content for a single row of the table |
||
| 1083 | * |
||
| 1084 | * @since 2.0.18 |
||
| 1085 | * @access public |
||
| 1086 | * |
||
| 1087 | * @param object $item The current item |
||
| 1088 | */ |
||
| 1089 | public function single_row( $item ) { |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * Generates the columns for a single row of the table |
||
| 1097 | * |
||
| 1098 | * @since 2.0.18 |
||
| 1099 | * @access protected |
||
| 1100 | * |
||
| 1101 | * @param object $item The current item |
||
| 1102 | */ |
||
| 1103 | protected function single_row_columns( $item ) { |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * Generates and display row actions links for the list table. |
||
| 1147 | * |
||
| 1148 | * @since 4.3.0 |
||
| 1149 | * @access protected |
||
| 1150 | * |
||
| 1151 | * @param object $item The item being acted upon. |
||
| 1152 | * @param string $column_name Current column name. |
||
| 1153 | * @param string $primary Primary column name. |
||
| 1154 | * |
||
| 1155 | * @return string The row actions output. In this case, an empty string. |
||
| 1156 | */ |
||
| 1157 | protected function handle_row_actions( $item, $column_name, $primary ) { |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * Handle an incoming ajax request (called from admin-ajax.php) |
||
| 1163 | * |
||
| 1164 | * @since 2.0.18 |
||
| 1165 | * @access public |
||
| 1166 | */ |
||
| 1167 | public function ajax_response() { |
||
| 1195 | |||
| 1196 | /** |
||
| 1197 | * Send required variables to JavaScript land |
||
| 1198 | * |
||
| 1199 | * @access public |
||
| 1200 | */ |
||
| 1201 | public function _js_vars() { |
||
| 1212 | } |
||
| 1213 |