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( 'set_pagination_args', 'get_views', 'get_bulk_actions', 'bulk_actions', |
||
|
|||
85 | 'row_actions', 'months_dropdown', 'view_switcher', 'comments_bubble', 'get_items_per_page', 'pagination', |
||
86 | 'get_sortable_columns', 'get_column_info', 'get_table_classes', 'display_tablenav', 'extra_tablenav', |
||
87 | 'single_row_columns' ); |
||
88 | |||
89 | /** |
||
90 | * Construct the table object |
||
91 | */ |
||
92 | public function __construct( $args ) { |
||
127 | |||
128 | public function ajax_user_can() { |
||
131 | |||
132 | public function get_columns() { |
||
135 | |||
136 | public function display_rows() { |
||
143 | |||
144 | /** |
||
145 | * Make private properties readable for backwards compatibility. |
||
146 | * |
||
147 | * @since 4.0.0 |
||
148 | * @access public |
||
149 | * |
||
150 | * @param string $name Property to get. |
||
151 | * @return mixed Property. |
||
152 | */ |
||
153 | public function __get( $name ) { |
||
158 | |||
159 | /** |
||
160 | * Make private properties settable for backwards compatibility. |
||
161 | * |
||
162 | * @since 4.0.0 |
||
163 | * @access public |
||
164 | * |
||
165 | * @param string $name Property to check if set. |
||
166 | * @param mixed $value Property value. |
||
167 | * @return mixed Newly-set property. |
||
168 | */ |
||
169 | public function __set( $name, $value ) { |
||
174 | |||
175 | /** |
||
176 | * Make private properties checkable for backwards compatibility. |
||
177 | * |
||
178 | * @since 4.0.0 |
||
179 | * @access public |
||
180 | * |
||
181 | * @param string $name Property to check if set. |
||
182 | * @return bool Whether the property is set. |
||
183 | */ |
||
184 | public function __isset( $name ) { |
||
189 | |||
190 | /** |
||
191 | * Make private properties un-settable for backwards compatibility. |
||
192 | * |
||
193 | * @since 4.0.0 |
||
194 | * @access public |
||
195 | * |
||
196 | * @param string $name Property to unset. |
||
197 | */ |
||
198 | public function __unset( $name ) { |
||
203 | |||
204 | /** |
||
205 | * Make private/protected methods readable for backwards compatibility. |
||
206 | * |
||
207 | * @since 4.0.0 |
||
208 | * @access public |
||
209 | * |
||
210 | * @param callable $name Method to call. |
||
211 | * @param array $arguments Arguments to pass when calling. |
||
212 | * @return mixed|bool Return value of the callback, false otherwise. |
||
213 | */ |
||
214 | public function __call( $name, $arguments ) { |
||
220 | |||
221 | /** |
||
222 | * Prepares the list of items for displaying. |
||
223 | * @uses FrmListHelper::set_pagination_args() |
||
224 | * |
||
225 | * @since 2.0.18 |
||
226 | * @access public |
||
227 | * @abstract |
||
228 | */ |
||
229 | public function prepare_items() { |
||
232 | |||
233 | /** |
||
234 | * An internal method that sets all the necessary pagination arguments |
||
235 | * |
||
236 | * @param array $args An associative array with information about the pagination |
||
237 | * @access protected |
||
238 | * |
||
239 | * @param array|string $args |
||
240 | */ |
||
241 | protected function set_pagination_args( $args ) { |
||
259 | |||
260 | /** |
||
261 | * Access the pagination args. |
||
262 | * |
||
263 | * @since 2.0.18 |
||
264 | * @access public |
||
265 | * |
||
266 | * @param string $key Pagination argument to retrieve. Common values include 'total_items', |
||
267 | * 'total_pages', 'per_page', or 'infinite_scroll'. |
||
268 | * @return int Number of items that correspond to the given pagination argument. |
||
269 | */ |
||
270 | public function get_pagination_arg( $key ) { |
||
277 | |||
278 | /** |
||
279 | * Whether the table has items to display or not |
||
280 | * |
||
281 | * @since 2.0.18 |
||
282 | * @access public |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | public function has_items() { |
||
289 | |||
290 | /** |
||
291 | * Message to be displayed when there are no items |
||
292 | * |
||
293 | * @since 2.0.18 |
||
294 | * @access public |
||
295 | */ |
||
296 | public function no_items() { |
||
299 | |||
300 | /** |
||
301 | * Display the search box. |
||
302 | * |
||
303 | * @since 2.0.18 |
||
304 | * @access public |
||
305 | * |
||
306 | * @param string $text The search button text |
||
307 | * @param string $input_id The search input id |
||
308 | */ |
||
309 | public function search_box( $text, $input_id ) { |
||
331 | |||
332 | /** |
||
333 | * Get an associative array ( id => link ) with the list |
||
334 | * of views available on this table. |
||
335 | * |
||
336 | * @since 2.0.18 |
||
337 | * @access protected |
||
338 | * |
||
339 | * @return array |
||
340 | */ |
||
341 | protected function get_views() { |
||
344 | |||
345 | /** |
||
346 | * Display the list of views available on this table. |
||
347 | * |
||
348 | * @since 2.0.18 |
||
349 | * @access public |
||
350 | */ |
||
351 | public function views() { |
||
375 | |||
376 | /** |
||
377 | * Get an associative array ( option_name => option_title ) with the list |
||
378 | * of bulk actions available on this table. |
||
379 | * |
||
380 | * @since 2.0.18 |
||
381 | * @access protected |
||
382 | * |
||
383 | * @return array |
||
384 | */ |
||
385 | protected function get_bulk_actions() { |
||
388 | |||
389 | /** |
||
390 | * Display the bulk actions dropdown. |
||
391 | * |
||
392 | * @since 2.0.18 |
||
393 | * @access protected |
||
394 | * |
||
395 | * @param string $which The location of the bulk actions: 'top' or 'bottom'. |
||
396 | * This is designated as optional for backwards-compatibility. |
||
397 | */ |
||
398 | protected function bulk_actions( $which = '' ) { |
||
438 | |||
439 | /** |
||
440 | * Get the current action selected from the bulk actions dropdown. |
||
441 | * |
||
442 | * @since 2.0.18 |
||
443 | * @access public |
||
444 | * |
||
445 | * @return string|false The action name or False if no action was selected |
||
446 | */ |
||
447 | public function current_action() { |
||
459 | |||
460 | /** |
||
461 | * Generate row actions div |
||
462 | * |
||
463 | * @since 2.0.18 |
||
464 | * @access protected |
||
465 | * |
||
466 | * @param array $actions The list of actions |
||
467 | * @param bool $always_visible Whether the actions should be always visible |
||
468 | * @return string |
||
469 | */ |
||
470 | protected function row_actions( $actions, $always_visible = false ) { |
||
489 | |||
490 | /** |
||
491 | * Display a monthly dropdown for filtering items |
||
492 | * |
||
493 | * @since 2.0.18 |
||
494 | * @access protected |
||
495 | * |
||
496 | * @global wpdb $wpdb |
||
497 | * @global WP_Locale $wp_locale |
||
498 | * |
||
499 | * @param string $post_type |
||
500 | */ |
||
501 | protected function months_dropdown( $post_type ) { |
||
562 | |||
563 | /** |
||
564 | * Display a view switcher |
||
565 | * |
||
566 | * @since 2.0.18 |
||
567 | * @access protected |
||
568 | * |
||
569 | * @param string $current_mode |
||
570 | */ |
||
571 | protected function view_switcher( $current_mode ) { |
||
591 | |||
592 | /** |
||
593 | * Display a comment count bubble |
||
594 | * |
||
595 | * @since 2.0.18 |
||
596 | * @access protected |
||
597 | * |
||
598 | * @param int $post_id The post ID. |
||
599 | * @param int $pending_comments Number of pending comments. |
||
600 | */ |
||
601 | protected function comments_bubble( $post_id, $pending_comments ) { |
||
638 | |||
639 | /** |
||
640 | * Get the current page number |
||
641 | * |
||
642 | * @since 2.0.18 |
||
643 | * @access public |
||
644 | * |
||
645 | * @return int |
||
646 | */ |
||
647 | public function get_pagenum() { |
||
655 | |||
656 | /** |
||
657 | * Get number of items to display on a single page |
||
658 | * |
||
659 | * @since 2.0.18 |
||
660 | * @access protected |
||
661 | * |
||
662 | * @param string $option |
||
663 | * @param int $default |
||
664 | * @return int |
||
665 | */ |
||
666 | protected function get_items_per_page( $option, $default = 20 ) { |
||
686 | |||
687 | /** |
||
688 | * Display the pagination. |
||
689 | * |
||
690 | * @since 2.0.18 |
||
691 | * @access protected |
||
692 | * |
||
693 | * @param string $which |
||
694 | */ |
||
695 | protected function pagination( $which ) { |
||
805 | |||
806 | /** |
||
807 | * Get a list of sortable columns. The format is: |
||
808 | * 'internal-name' => 'orderby' |
||
809 | * or |
||
810 | * 'internal-name' => array( 'orderby', true ) |
||
811 | * |
||
812 | * The second format will make the initial sorting order be descending |
||
813 | * |
||
814 | * @since 2.0.18 |
||
815 | * @access protected |
||
816 | * |
||
817 | * @return array |
||
818 | */ |
||
819 | protected function get_sortable_columns() { |
||
822 | |||
823 | /** |
||
824 | * Gets the name of the default primary column. |
||
825 | * |
||
826 | * @since 4.3.0 |
||
827 | * @access protected |
||
828 | * |
||
829 | * @return string Name of the default primary column, in this case, an empty string. |
||
830 | */ |
||
831 | protected function get_default_primary_column_name() { |
||
848 | |||
849 | /** |
||
850 | * Gets the name of the primary column. |
||
851 | * |
||
852 | * @since 4.3.0 |
||
853 | * @access protected |
||
854 | * |
||
855 | * @return string The name of the primary column. |
||
856 | */ |
||
857 | protected function get_primary_column_name() { |
||
883 | |||
884 | /** |
||
885 | * Get a list of all, hidden and sortable columns, with filter applied |
||
886 | * |
||
887 | * @since 2.0.18 |
||
888 | * @access protected |
||
889 | * |
||
890 | * @return array |
||
891 | */ |
||
892 | protected function get_column_info() { |
||
938 | |||
939 | /** |
||
940 | * Return number of visible columns |
||
941 | * |
||
942 | * @since 2.0.18 |
||
943 | * @access public |
||
944 | * |
||
945 | * @return int |
||
946 | */ |
||
947 | public function get_column_count() { |
||
952 | |||
953 | /** |
||
954 | * Print column headers, accounting for hidden and sortable columns. |
||
955 | * |
||
956 | * @since 2.0.18 |
||
957 | * @access public |
||
958 | * |
||
959 | * @staticvar int $cb_counter |
||
960 | * |
||
961 | * @param bool $with_id Whether to set the id attribute or not |
||
962 | */ |
||
963 | public function print_column_headers( $with_id = true ) { |
||
1028 | |||
1029 | /** |
||
1030 | * Display the table |
||
1031 | * |
||
1032 | * @since 2.0.18 |
||
1033 | * @access public |
||
1034 | */ |
||
1035 | public function display() { |
||
1064 | |||
1065 | /** |
||
1066 | * Get a list of CSS classes for the list table table tag. |
||
1067 | * |
||
1068 | * @since 2.0.18 |
||
1069 | * @access protected |
||
1070 | * |
||
1071 | * @return array List of CSS classes for the table tag. |
||
1072 | */ |
||
1073 | protected function get_table_classes() { |
||
1076 | |||
1077 | /** |
||
1078 | * Generate the table navigation above or below the table |
||
1079 | * |
||
1080 | * @since 2.0.18 |
||
1081 | * @access protected |
||
1082 | * @param string $which |
||
1083 | */ |
||
1084 | protected function display_tablenav( $which ) { |
||
1102 | |||
1103 | /** |
||
1104 | * Extra controls to be displayed between bulk actions and pagination |
||
1105 | * |
||
1106 | * @since 2.0.18 |
||
1107 | * @access protected |
||
1108 | * |
||
1109 | * @param string $which |
||
1110 | */ |
||
1111 | protected function extra_tablenav( $which ) {} |
||
1112 | |||
1113 | /** |
||
1114 | * Generate the tbody element for the list table. |
||
1115 | * |
||
1116 | * @since 2.0.18 |
||
1117 | * @access public |
||
1118 | */ |
||
1119 | public function display_rows_or_placeholder() { |
||
1128 | |||
1129 | /** |
||
1130 | * Generates content for a single row of the table |
||
1131 | * |
||
1132 | * @since 2.0.18 |
||
1133 | * @access public |
||
1134 | * |
||
1135 | * @param object $item The current item |
||
1136 | */ |
||
1137 | public function single_row( $item ) { |
||
1142 | |||
1143 | /** |
||
1144 | * |
||
1145 | * @param object $item |
||
1146 | * @param string $column_name |
||
1147 | */ |
||
1148 | protected function column_default( $item, $column_name ) {} |
||
1149 | |||
1150 | /** |
||
1151 | * |
||
1152 | * @param object $item |
||
1153 | */ |
||
1154 | protected function column_cb( $item ) {} |
||
1155 | |||
1156 | /** |
||
1157 | * Generates the columns for a single row of the table |
||
1158 | * |
||
1159 | * @since 2.0.18 |
||
1160 | * @access protected |
||
1161 | * |
||
1162 | * @param object $item The current item |
||
1163 | */ |
||
1164 | protected function single_row_columns( $item ) { |
||
1208 | |||
1209 | /** |
||
1210 | * Generates and display row actions links for the list table. |
||
1211 | * |
||
1212 | * @since 4.3.0 |
||
1213 | * @access protected |
||
1214 | * |
||
1215 | * @param object $item The item being acted upon. |
||
1216 | * @param string $column_name Current column name. |
||
1217 | * @param string $primary Primary column name. |
||
1218 | * @return string The row actions output. In this case, an empty string. |
||
1219 | */ |
||
1220 | protected function handle_row_actions( $item, $column_name, $primary ) { |
||
1223 | |||
1224 | /** |
||
1225 | * Handle an incoming ajax request (called from admin-ajax.php) |
||
1226 | * |
||
1227 | * @since 2.0.18 |
||
1228 | * @access public |
||
1229 | */ |
||
1230 | public function ajax_response() { |
||
1257 | |||
1258 | /** |
||
1259 | * Send required variables to JavaScript land |
||
1260 | * |
||
1261 | * @access public |
||
1262 | */ |
||
1263 | public function _js_vars() { |
||
1274 | } |
||
1275 |