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 Jetpack_Modules_List_Table 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 Jetpack_Modules_List_Table, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Jetpack_Modules_List_Table extends WP_List_Table { |
||
9 | |||
10 | function __construct() { |
||
11 | parent::__construct(); |
||
12 | |||
13 | Jetpack::init(); |
||
14 | |||
15 | if ( $this->compat_fields && is_array( $this->compat_fields ) ) { |
||
16 | array_push( $this->compat_fields, 'all_items' ); |
||
17 | } |
||
18 | |||
19 | $this->items = $this->all_items = Jetpack_Admin::init()->get_modules(); |
||
20 | $this->items = $this->filter_displayed_table_items( $this->items ); |
||
21 | /** |
||
22 | * Filters the list of modules available to be displayed in the Jetpack Settings screen. |
||
23 | * |
||
24 | * @since 3.0.0 |
||
25 | * |
||
26 | * @param array $this->items Array of Jetpack modules. |
||
27 | */ |
||
28 | $this->items = apply_filters( 'jetpack_modules_list_table_items', $this->items ); |
||
29 | $this->_column_headers = array( $this->get_columns(), array(), array(), 'name' ); |
||
30 | $modal_info = isset( $_GET['info'] ) ? $_GET['info'] : false; |
||
31 | |||
32 | wp_register_script( |
||
33 | 'models.jetpack-modules', |
||
34 | Assets::get_file_url_for_environment( |
||
35 | '_inc/build/jetpack-modules.models.min.js', |
||
36 | '_inc/jetpack-modules.models.js' |
||
37 | ), |
||
38 | array( 'backbone', 'underscore' ), |
||
39 | JETPACK__VERSION |
||
40 | ); |
||
41 | wp_register_script( |
||
42 | 'views.jetpack-modules', |
||
43 | Assets::get_file_url_for_environment( |
||
44 | '_inc/build/jetpack-modules.views.min.js', |
||
45 | '_inc/jetpack-modules.views.js' |
||
46 | ), |
||
47 | array( 'backbone', 'underscore', 'wp-util' ), |
||
48 | JETPACK__VERSION |
||
49 | ); |
||
50 | wp_register_script( |
||
51 | 'jetpack-modules-list-table', |
||
52 | Assets::get_file_url_for_environment( |
||
53 | '_inc/build/jetpack-modules.min.js', |
||
54 | '_inc/jetpack-modules.js' |
||
55 | ), |
||
56 | array( |
||
57 | 'views.jetpack-modules', |
||
58 | 'models.jetpack-modules', |
||
59 | 'jquery', |
||
60 | ), |
||
61 | JETPACK__VERSION, |
||
62 | true |
||
63 | ); |
||
64 | |||
65 | wp_localize_script( 'jetpack-modules-list-table', 'jetpackModulesData', array( |
||
66 | 'modules' => Jetpack::get_translated_modules( $this->all_items ), |
||
67 | 'i18n' => array( |
||
68 | 'search_placeholder' => __( 'Search Modules…', 'jetpack' ), |
||
69 | ), |
||
70 | 'modalinfo' => $this->module_info_check( $modal_info, $this->all_items ), |
||
71 | 'nonces' => array( |
||
72 | 'bulk' => wp_create_nonce( 'bulk-jetpack_page_jetpack_modules' ), |
||
73 | ), |
||
74 | ) ); |
||
75 | |||
76 | wp_enqueue_script( 'jetpack-modules-list-table' ); |
||
77 | |||
78 | /** |
||
79 | * Filters the js_templates callback value. |
||
80 | * |
||
81 | * @since 3.6.0 |
||
82 | * |
||
83 | * @param array array( $this, 'js_templates' ) js_templates callback. |
||
84 | */ |
||
85 | add_action( 'admin_footer', apply_filters( 'jetpack_modules_list_table_js_template_callback', array( $this, 'js_templates' ) ), 9 ); |
||
86 | } |
||
87 | |||
88 | function js_templates() { |
||
129 | |||
130 | function get_views() { |
||
156 | |||
157 | function views() { |
||
167 | |||
168 | function filter_displayed_table_items( $modules ) { |
||
171 | |||
172 | static function is_module_displayed( $module ) { |
||
183 | |||
184 | View Code Duplication | static function sort_requires_connection_last( $module1, $module2 ) { |
|
194 | |||
195 | function get_columns() { |
||
202 | |||
203 | function get_bulk_actions() { |
||
210 | |||
211 | function single_row( $item ) { |
||
226 | |||
227 | function get_table_classes() { |
||
230 | |||
231 | function column_cb( $item ) { |
||
237 | |||
238 | function column_icon( $item ) { |
||
251 | |||
252 | function column_name( $item ) { |
||
285 | |||
286 | function column_description( $item ) { |
||
298 | |||
299 | function column_module_tags( $item ) { |
||
306 | |||
307 | function column_default( $item, $column_name ) { |
||
317 | |||
318 | //Check if the info parameter provided in the URL corresponds to an actual module |
||
319 | function module_info_check( $info = false, $modules ) { |
||
326 | |||
327 | /** |
||
328 | * Core switched their `display_tablenav()` method to protected, so we can't access it directly. |
||
329 | * Instead, let's include an access function to make it doable without errors! |
||
330 | * |
||
331 | * @see https://github.com/WordPress/WordPress/commit/d28f6344de97616de8ece543ed290c4ba2383622 |
||
332 | * |
||
333 | * @param string $which |
||
334 | * |
||
335 | * @return mixed |
||
336 | */ |
||
337 | function unprotected_display_tablenav( $which = 'top' ) { |
||
340 | |||
341 | } |
||
342 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.