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() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Prepares the list of items for displaying. |
||
| 148 | * @uses FrmListHelper::set_pagination_args() |
||
| 149 | * |
||
| 150 | * @since 2.0.18 |
||
| 151 | * @access public |
||
| 152 | * @abstract |
||
| 153 | */ |
||
| 154 | public function prepare_items() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * An internal method that sets all the necessary pagination arguments |
||
| 160 | * |
||
| 161 | * @param array $args An associative array with information about the pagination |
||
| 162 | * @access protected |
||
| 163 | * |
||
| 164 | * @param array|string $args |
||
| 165 | */ |
||
| 166 | protected function set_pagination_args( $args ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Access the pagination args. |
||
| 188 | * |
||
| 189 | * @since 2.0.18 |
||
| 190 | * @access public |
||
| 191 | * |
||
| 192 | * @param string $key Pagination argument to retrieve. Common values include 'total_items', |
||
| 193 | * 'total_pages', 'per_page', or 'infinite_scroll'. |
||
| 194 | * @return int Number of items that correspond to the given pagination argument. |
||
| 195 | */ |
||
| 196 | public function get_pagination_arg( $key ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Whether the table has items to display or not |
||
| 208 | * |
||
| 209 | * @since 2.0.18 |
||
| 210 | * @access public |
||
| 211 | * |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | public function has_items() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Message to be displayed when there are no items |
||
| 220 | * |
||
| 221 | * @since 2.0.18 |
||
| 222 | * @access public |
||
| 223 | */ |
||
| 224 | public function no_items() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Display the search box. |
||
| 230 | * |
||
| 231 | * @since 2.0.18 |
||
| 232 | * @access public |
||
| 233 | * |
||
| 234 | * @param string $text The search button text |
||
| 235 | * @param string $input_id The search input id |
||
| 236 | */ |
||
| 237 | public function search_box( $text, $input_id ) { |
||
| 255 | |||
| 256 | private function hidden_search_inputs( $param_name ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get an associative array ( id => link ) with the list |
||
| 264 | * of views available on this table. |
||
| 265 | * |
||
| 266 | * @since 2.0.18 |
||
| 267 | * @access protected |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | protected function get_views() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Display the list of views available on this table. |
||
| 277 | * |
||
| 278 | * @since 2.0.18 |
||
| 279 | * @access public |
||
| 280 | */ |
||
| 281 | public function views() { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get an associative array ( option_name => option_title ) with the list |
||
| 309 | * of bulk actions available on this table. |
||
| 310 | * |
||
| 311 | * @since 2.0.18 |
||
| 312 | * @access protected |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | protected function get_bulk_actions() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Display the bulk actions dropdown. |
||
| 322 | * |
||
| 323 | * @since 2.0.18 |
||
| 324 | * @access protected |
||
| 325 | * |
||
| 326 | * @param string $which The location of the bulk actions: 'top' or 'bottom'. |
||
| 327 | * This is designated as optional for backwards-compatibility. |
||
| 328 | */ |
||
| 329 | protected function bulk_actions( $which = '' ) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get the current action selected from the bulk actions dropdown. |
||
| 373 | * |
||
| 374 | * @since 2.0.18 |
||
| 375 | * @access public |
||
| 376 | * |
||
| 377 | * @return string|false The action name or False if no action was selected |
||
| 378 | */ |
||
| 379 | public function current_action() { |
||
| 391 | |||
| 392 | private static function get_bulk_action( $action_name ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Generate row actions div |
||
| 402 | * |
||
| 403 | * @since 2.0.18 |
||
| 404 | * @access protected |
||
| 405 | * |
||
| 406 | * @param array $actions The list of actions |
||
| 407 | * @param bool $always_visible Whether the actions should be always visible |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | protected function row_actions( $actions, $always_visible = false ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Display a view switcher |
||
| 433 | * |
||
| 434 | * @since 2.0.18 |
||
| 435 | * @access protected |
||
| 436 | * |
||
| 437 | * @param string $current_mode |
||
| 438 | */ |
||
| 439 | protected function view_switcher( $current_mode ) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get the current page number |
||
| 462 | * |
||
| 463 | * @since 2.0.18 |
||
| 464 | * @access public |
||
| 465 | * |
||
| 466 | * @return int |
||
| 467 | */ |
||
| 468 | public function get_pagenum() { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get number of items to display on a single page |
||
| 480 | * |
||
| 481 | * @since 2.0.18 |
||
| 482 | * @access protected |
||
| 483 | * |
||
| 484 | * @param string $option |
||
| 485 | * @param int $default |
||
| 486 | * @return int |
||
| 487 | */ |
||
| 488 | protected function get_items_per_page( $option, $default = 20 ) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Display the pagination. |
||
| 512 | * |
||
| 513 | * @since 2.0.18 |
||
| 514 | * @access protected |
||
| 515 | * |
||
| 516 | * @param string $which |
||
| 517 | */ |
||
| 518 | protected function pagination( $which ) { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Get a list of sortable columns. The format is: |
||
| 631 | * 'internal-name' => 'orderby' |
||
| 632 | * or |
||
| 633 | * 'internal-name' => array( 'orderby', true ) |
||
| 634 | * |
||
| 635 | * The second format will make the initial sorting order be descending |
||
| 636 | * |
||
| 637 | * @since 2.0.18 |
||
| 638 | * @access protected |
||
| 639 | * |
||
| 640 | * @return array |
||
| 641 | */ |
||
| 642 | protected function get_sortable_columns() { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Gets the name of the default primary column. |
||
| 648 | * |
||
| 649 | * @since 4.3.0 |
||
| 650 | * @access protected |
||
| 651 | * |
||
| 652 | * @return string Name of the default primary column, in this case, an empty string. |
||
| 653 | */ |
||
| 654 | protected function get_default_primary_column_name() { |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Gets the name of the primary column. |
||
| 674 | * |
||
| 675 | * @since 4.3.0 |
||
| 676 | * @access protected |
||
| 677 | * |
||
| 678 | * @return string The name of the primary column. |
||
| 679 | */ |
||
| 680 | protected function get_primary_column_name() { |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Get a list of all, hidden and sortable columns, with filter applied |
||
| 709 | * |
||
| 710 | * @since 2.0.18 |
||
| 711 | * @access protected |
||
| 712 | * |
||
| 713 | * @return array |
||
| 714 | */ |
||
| 715 | protected function get_column_info() { |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Return number of visible columns |
||
| 766 | * |
||
| 767 | * @since 2.0.18 |
||
| 768 | * @access public |
||
| 769 | * |
||
| 770 | * @return int |
||
| 771 | */ |
||
| 772 | public function get_column_count() { |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Print column headers, accounting for hidden and sortable columns. |
||
| 780 | * |
||
| 781 | * @since 2.0.18 |
||
| 782 | * @access public |
||
| 783 | * |
||
| 784 | * @staticvar int $cb_counter |
||
| 785 | * |
||
| 786 | * @param bool $with_id Whether to set the id attribute or not |
||
| 787 | */ |
||
| 788 | public function print_column_headers( $with_id = true ) { |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Display the table |
||
| 860 | * |
||
| 861 | * @since 2.0.18 |
||
| 862 | * @access public |
||
| 863 | */ |
||
| 864 | public function display() { |
||
| 893 | |||
| 894 | /** |
||
| 895 | * Get a list of CSS classes for the list table table tag. |
||
| 896 | * |
||
| 897 | * @since 2.0.18 |
||
| 898 | * @access protected |
||
| 899 | * |
||
| 900 | * @return array List of CSS classes for the table tag. |
||
| 901 | */ |
||
| 902 | protected function get_table_classes() { |
||
| 905 | |||
| 906 | /** |
||
| 907 | * Generate the table navigation above or below the table |
||
| 908 | * |
||
| 909 | * @since 2.0.18 |
||
| 910 | * @access protected |
||
| 911 | * @param string $which |
||
| 912 | */ |
||
| 913 | protected function display_tablenav( $which ) { |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Extra controls to be displayed between bulk actions and pagination |
||
| 935 | * |
||
| 936 | * @since 2.0.18 |
||
| 937 | * @access protected |
||
| 938 | * |
||
| 939 | * @param string $which |
||
| 940 | */ |
||
| 941 | protected function extra_tablenav( $which ) {} |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Generate the tbody element for the list table. |
||
| 945 | * |
||
| 946 | * @since 2.0.18 |
||
| 947 | * @access public |
||
| 948 | */ |
||
| 949 | public function display_rows_or_placeholder() { |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Generates content for a single row of the table |
||
| 961 | * |
||
| 962 | * @since 2.0.18 |
||
| 963 | * @access public |
||
| 964 | * |
||
| 965 | * @param object $item The current item |
||
| 966 | */ |
||
| 967 | public function single_row( $item ) { |
||
| 972 | |||
| 973 | /** |
||
| 974 | * Generates the columns for a single row of the table |
||
| 975 | * |
||
| 976 | * @since 2.0.18 |
||
| 977 | * @access protected |
||
| 978 | * |
||
| 979 | * @param object $item The current item |
||
| 980 | */ |
||
| 981 | protected function single_row_columns( $item ) { |
||
| 1022 | |||
| 1023 | /** |
||
| 1024 | * Generates and display row actions links for the list table. |
||
| 1025 | * |
||
| 1026 | * @since 4.3.0 |
||
| 1027 | * @access protected |
||
| 1028 | * |
||
| 1029 | * @param object $item The item being acted upon. |
||
| 1030 | * @param string $column_name Current column name. |
||
| 1031 | * @param string $primary Primary column name. |
||
| 1032 | * @return string The row actions output. In this case, an empty string. |
||
| 1033 | */ |
||
| 1034 | protected function handle_row_actions( $item, $column_name, $primary ) { |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * Handle an incoming ajax request (called from admin-ajax.php) |
||
| 1040 | * |
||
| 1041 | * @since 2.0.18 |
||
| 1042 | * @access public |
||
| 1043 | */ |
||
| 1044 | public function ajax_response() { |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Send required variables to JavaScript land |
||
| 1074 | * |
||
| 1075 | * @access public |
||
| 1076 | */ |
||
| 1077 | public function _js_vars() { |
||
| 1088 | } |
||
| 1089 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.