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 | * @since 3.0 |
||
| 159 | */ |
||
| 160 | protected function get_param( $args ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * An internal method that sets all the necessary pagination arguments |
||
| 171 | * |
||
| 172 | * @param array $args An associative array with information about the pagination |
||
| 173 | * @access protected |
||
| 174 | * |
||
| 175 | * @param array|string $args |
||
| 176 | */ |
||
| 177 | protected function set_pagination_args( $args ) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Access the pagination args. |
||
| 199 | * |
||
| 200 | * @since 2.0.18 |
||
| 201 | * @access public |
||
| 202 | * |
||
| 203 | * @param string $key Pagination argument to retrieve. Common values include 'total_items', |
||
| 204 | * 'total_pages', 'per_page', or 'infinite_scroll'. |
||
| 205 | * @return int Number of items that correspond to the given pagination argument. |
||
| 206 | */ |
||
| 207 | public function get_pagination_arg( $key ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Whether the table has items to display or not |
||
| 219 | * |
||
| 220 | * @since 2.0.18 |
||
| 221 | * @access public |
||
| 222 | * |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | public function has_items() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Message to be displayed when there are no items |
||
| 231 | * |
||
| 232 | * @since 2.0.18 |
||
| 233 | * @access public |
||
| 234 | */ |
||
| 235 | public function no_items() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Display the search box. |
||
| 241 | * |
||
| 242 | * @since 2.0.18 |
||
| 243 | * @access public |
||
| 244 | * |
||
| 245 | * @param string $text The search button text |
||
| 246 | * @param string $input_id The search input id |
||
| 247 | */ |
||
| 248 | public function search_box( $text, $input_id ) { |
||
| 266 | |||
| 267 | private function hidden_search_inputs( $param_name ) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get an associative array ( id => link ) with the list |
||
| 275 | * of views available on this table. |
||
| 276 | * |
||
| 277 | * @since 2.0.18 |
||
| 278 | * @access protected |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | protected function get_views() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Display the list of views available on this table. |
||
| 288 | * |
||
| 289 | * @since 2.0.18 |
||
| 290 | * @access public |
||
| 291 | */ |
||
| 292 | public function views() { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get an associative array ( option_name => option_title ) with the list |
||
| 320 | * of bulk actions available on this table. |
||
| 321 | * |
||
| 322 | * @since 2.0.18 |
||
| 323 | * @access protected |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | protected function get_bulk_actions() { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Display the bulk actions dropdown. |
||
| 333 | * |
||
| 334 | * @since 2.0.18 |
||
| 335 | * @access protected |
||
| 336 | * |
||
| 337 | * @param string $which The location of the bulk actions: 'top' or 'bottom'. |
||
| 338 | * This is designated as optional for backwards-compatibility. |
||
| 339 | */ |
||
| 340 | protected function bulk_actions( $which = '' ) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Get the current action selected from the bulk actions dropdown. |
||
| 386 | * |
||
| 387 | * @since 2.0.18 |
||
| 388 | * @access public |
||
| 389 | * |
||
| 390 | * @return string|false The action name or False if no action was selected |
||
| 391 | */ |
||
| 392 | public function current_action() { |
||
| 404 | |||
| 405 | private static function get_bulk_action( $action_name ) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Generate row actions div |
||
| 416 | * |
||
| 417 | * @since 2.0.18 |
||
| 418 | * @access protected |
||
| 419 | * |
||
| 420 | * @param array $actions The list of actions |
||
| 421 | * @param bool $always_visible Whether the actions should be always visible |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | protected function row_actions( $actions, $always_visible = false ) { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Display a view switcher |
||
| 447 | * |
||
| 448 | * @since 2.0.18 |
||
| 449 | * @access protected |
||
| 450 | * |
||
| 451 | * @param string $current_mode |
||
| 452 | */ |
||
| 453 | protected function view_switcher( $current_mode ) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get the current page number |
||
| 478 | * |
||
| 479 | * @since 2.0.18 |
||
| 480 | * @access public |
||
| 481 | * |
||
| 482 | * @return int |
||
| 483 | */ |
||
| 484 | public function get_pagenum() { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Get number of items to display on a single page |
||
| 496 | * |
||
| 497 | * @since 2.0.18 |
||
| 498 | * @access protected |
||
| 499 | * |
||
| 500 | * @param string $option |
||
| 501 | * @param int $default |
||
| 502 | * @return int |
||
| 503 | */ |
||
| 504 | protected function get_items_per_page( $option, $default = 20 ) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Display the pagination. |
||
| 528 | * |
||
| 529 | * @since 2.0.18 |
||
| 530 | * @access protected |
||
| 531 | * |
||
| 532 | * @param string $which |
||
| 533 | */ |
||
| 534 | protected function pagination( $which ) { |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Get a list of sortable columns. The format is: |
||
| 650 | * 'internal-name' => 'orderby' |
||
| 651 | * or |
||
| 652 | * 'internal-name' => array( 'orderby', true ) |
||
| 653 | * |
||
| 654 | * The second format will make the initial sorting order be descending |
||
| 655 | * |
||
| 656 | * @since 2.0.18 |
||
| 657 | * @access protected |
||
| 658 | * |
||
| 659 | * @return array |
||
| 660 | */ |
||
| 661 | protected function get_sortable_columns() { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Gets the name of the default primary column. |
||
| 667 | * |
||
| 668 | * @since 4.3.0 |
||
| 669 | * @access protected |
||
| 670 | * |
||
| 671 | * @return string Name of the default primary column, in this case, an empty string. |
||
| 672 | */ |
||
| 673 | protected function get_default_primary_column_name() { |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Gets the name of the primary column. |
||
| 693 | * |
||
| 694 | * @since 4.3.0 |
||
| 695 | * @access protected |
||
| 696 | * |
||
| 697 | * @return string The name of the primary column. |
||
| 698 | */ |
||
| 699 | protected function get_primary_column_name() { |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Get a list of all, hidden and sortable columns, with filter applied |
||
| 728 | * |
||
| 729 | * @since 2.0.18 |
||
| 730 | * @access protected |
||
| 731 | * |
||
| 732 | * @return array |
||
| 733 | */ |
||
| 734 | protected function get_column_info() { |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Prevent too many columns from showing on the page |
||
| 786 | * @since 2.05.07 |
||
| 787 | */ |
||
| 788 | private function hide_extra_columns( $columns, &$hidden ) { |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Return number of visible columns |
||
| 809 | * |
||
| 810 | * @since 2.0.18 |
||
| 811 | * @access public |
||
| 812 | * |
||
| 813 | * @return int |
||
| 814 | */ |
||
| 815 | public function get_column_count() { |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Print column headers, accounting for hidden and sortable columns. |
||
| 823 | * |
||
| 824 | * @since 2.0.18 |
||
| 825 | * @access public |
||
| 826 | * |
||
| 827 | * @staticvar int $cb_counter |
||
| 828 | * |
||
| 829 | * @param bool $with_id Whether to set the id attribute or not |
||
| 830 | */ |
||
| 831 | public function print_column_headers( $with_id = true ) { |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Display the table |
||
| 903 | * |
||
| 904 | * @since 2.0.18 |
||
| 905 | * @access public |
||
| 906 | */ |
||
| 907 | public function display() { |
||
| 936 | |||
| 937 | /** |
||
| 938 | * Get a list of CSS classes for the list table table tag. |
||
| 939 | * |
||
| 940 | * @since 2.0.18 |
||
| 941 | * @access protected |
||
| 942 | * |
||
| 943 | * @return array List of CSS classes for the table tag. |
||
| 944 | */ |
||
| 945 | protected function get_table_classes() { |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Generate the table navigation above or below the table |
||
| 951 | * |
||
| 952 | * @since 2.0.18 |
||
| 953 | * @access protected |
||
| 954 | * @param string $which |
||
| 955 | */ |
||
| 956 | protected function display_tablenav( $which ) { |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Extra controls to be displayed between bulk actions and pagination |
||
| 978 | * |
||
| 979 | * @since 2.0.18 |
||
| 980 | * @access protected |
||
| 981 | * |
||
| 982 | * @param string $which |
||
| 983 | */ |
||
| 984 | protected function extra_tablenav( $which ) {} |
||
| 985 | |||
| 986 | /** |
||
| 987 | * Generate the tbody element for the list table. |
||
| 988 | * |
||
| 989 | * @since 2.0.18 |
||
| 990 | * @access public |
||
| 991 | */ |
||
| 992 | public function display_rows_or_placeholder() { |
||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Generates content for a single row of the table |
||
| 1004 | * |
||
| 1005 | * @since 2.0.18 |
||
| 1006 | * @access public |
||
| 1007 | * |
||
| 1008 | * @param object $item The current item |
||
| 1009 | */ |
||
| 1010 | public function single_row( $item ) { |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Generates the columns for a single row of the table |
||
| 1018 | * |
||
| 1019 | * @since 2.0.18 |
||
| 1020 | * @access protected |
||
| 1021 | * |
||
| 1022 | * @param object $item The current item |
||
| 1023 | */ |
||
| 1024 | protected function single_row_columns( $item ) { |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * Generates and display row actions links for the list table. |
||
| 1068 | * |
||
| 1069 | * @since 4.3.0 |
||
| 1070 | * @access protected |
||
| 1071 | * |
||
| 1072 | * @param object $item The item being acted upon. |
||
| 1073 | * @param string $column_name Current column name. |
||
| 1074 | * @param string $primary Primary column name. |
||
| 1075 | * @return string The row actions output. In this case, an empty string. |
||
| 1076 | */ |
||
| 1077 | protected function handle_row_actions( $item, $column_name, $primary ) { |
||
| 1080 | |||
| 1081 | /** |
||
| 1082 | * Handle an incoming ajax request (called from admin-ajax.php) |
||
| 1083 | * |
||
| 1084 | * @since 2.0.18 |
||
| 1085 | * @access public |
||
| 1086 | */ |
||
| 1087 | public function ajax_response() { |
||
| 1114 | |||
| 1115 | /** |
||
| 1116 | * Send required variables to JavaScript land |
||
| 1117 | * |
||
| 1118 | * @access public |
||
| 1119 | */ |
||
| 1120 | public function _js_vars() { |
||
| 1131 | } |
||
| 1132 |