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_Instant_Search 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_Instant_Search, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Jetpack_Instant_Search extends Jetpack_Search { |
||
18 | |||
19 | /** |
||
20 | * Loads the php for this version of search |
||
21 | * |
||
22 | * @since 8.3.0 |
||
23 | */ |
||
24 | public function load_php() { |
||
32 | |||
33 | /** |
||
34 | * Setup the various hooks needed for the plugin to take over search duties. |
||
35 | * |
||
36 | * @since 5.0.0 |
||
37 | */ |
||
38 | View Code Duplication | public function init_hooks() { |
|
54 | |||
55 | /** |
||
56 | * Loads assets for Jetpack Instant Search Prototype featuring Search As You Type experience. |
||
57 | */ |
||
58 | public function load_assets() { |
||
59 | $script_relative_path = '_inc/build/instant-search/jp-search.bundle.js'; |
||
60 | $style_relative_path = '_inc/build/instant-search/instant-search.min.css'; |
||
61 | if ( ! file_exists( JETPACK__PLUGIN_DIR . $script_relative_path ) || ! file_exists( JETPACK__PLUGIN_DIR . $style_relative_path ) ) { |
||
62 | return; |
||
63 | } |
||
64 | |||
65 | $script_version = Jetpack_Search_Helpers::get_asset_version( $script_relative_path ); |
||
66 | $script_path = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE ); |
||
67 | wp_enqueue_script( 'jetpack-instant-search', $script_path, array(), $script_version, true ); |
||
68 | wp_set_script_translations( 'jetpack-instant-search', 'jetpack' ); |
||
69 | $this->load_and_initialize_tracks(); |
||
70 | $this->inject_javascript_options(); |
||
71 | |||
72 | $style_version = Jetpack_Search_Helpers::get_asset_version( $style_relative_path ); |
||
73 | $style_path = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE ); |
||
74 | wp_enqueue_style( 'jetpack-instant-search', $style_path, array(), $style_version ); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Passes all options to the JS app. |
||
79 | */ |
||
80 | protected function inject_javascript_options() { |
||
81 | $widget_options = Jetpack_Search_Helpers::get_widgets_from_option(); |
||
82 | if ( is_array( $widget_options ) ) { |
||
83 | $widget_options = end( $widget_options ); |
||
|
|||
84 | } |
||
85 | |||
86 | $overlay_widget_ids = is_active_sidebar( 'jetpack-instant-search-sidebar' ) ? |
||
87 | wp_get_sidebars_widgets()['jetpack-instant-search-sidebar'] : array(); |
||
88 | $filters = Jetpack_Search_Helpers::get_filters_from_widgets(); |
||
89 | $widgets = array(); |
||
90 | $widgets_outside_overlay = array(); |
||
91 | foreach ( $filters as $key => &$filter ) { |
||
92 | $filter['filter_id'] = $key; |
||
93 | |||
94 | if ( in_array( $filter['widget_id'], $overlay_widget_ids, true ) ) { |
||
95 | View Code Duplication | if ( ! isset( $widgets[ $filter['widget_id'] ] ) ) { |
|
96 | $widgets[ $filter['widget_id'] ]['filters'] = array(); |
||
97 | $widgets[ $filter['widget_id'] ]['widget_id'] = $filter['widget_id']; |
||
98 | } |
||
99 | $widgets[ $filter['widget_id'] ]['filters'][] = $filter; |
||
100 | } else { |
||
101 | View Code Duplication | if ( ! isset( $widgets_outside_overlay[ $filter['widget_id'] ] ) ) { |
|
102 | $widgets_outside_overlay[ $filter['widget_id'] ]['filters'] = array(); |
||
103 | $widgets_outside_overlay[ $filter['widget_id'] ]['widget_id'] = $filter['widget_id']; |
||
104 | } |
||
105 | $widgets_outside_overlay[ $filter['widget_id'] ]['filters'][] = $filter; |
||
106 | } |
||
107 | } |
||
108 | unset( $filter ); |
||
109 | |||
110 | $post_type_objs = get_post_types( array( 'exclude_from_search' => false ), 'objects' ); |
||
111 | $post_type_labels = array(); |
||
112 | foreach ( $post_type_objs as $key => $obj ) { |
||
113 | $post_type_labels[ $key ] = array( |
||
114 | 'singular_name' => $obj->labels->singular_name, |
||
115 | 'name' => $obj->labels->name, |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | $prefix = Jetpack_Search_Options::OPTION_PREFIX; |
||
120 | $posts_per_page = (int) get_option( 'posts_per_page' ); |
||
121 | if ( ( $posts_per_page > 20 ) || ( $posts_per_page <= 0 ) ) { |
||
122 | $posts_per_page = 20; |
||
123 | } |
||
124 | |||
125 | $options = array( |
||
126 | 'overlayOptions' => array( |
||
127 | 'colorTheme' => get_option( $prefix . 'color_theme', 'light' ), |
||
128 | 'enableInfScroll' => get_option( $prefix . 'inf_scroll', '1' ) === '1', |
||
129 | 'enableSort' => get_option( $prefix . 'enable_sort', '1' ) === '1', |
||
130 | 'highlightColor' => get_option( $prefix . 'highlight_color', '#FFC' ), |
||
131 | 'opacity' => (int) get_option( $prefix . 'opacity', 97 ), |
||
132 | 'overlayTrigger' => get_option( $prefix . 'overlay_trigger', 'immediate' ), |
||
133 | 'resultFormat' => get_option( $prefix . 'result_format', 'minimal' ), |
||
134 | 'showPoweredBy' => get_option( $prefix . 'show_powered_by', '1' ) === '1', |
||
135 | ), |
||
136 | |||
137 | // core config. |
||
138 | 'homeUrl' => home_url(), |
||
139 | 'locale' => str_replace( '_', '-', Jetpack_Search_Helpers::is_valid_locale( get_locale() ) ? get_locale() : 'en_US' ), |
||
140 | 'postsPerPage' => $posts_per_page, |
||
141 | 'siteId' => $this->jetpack_blog_id, |
||
142 | 'postTypes' => $post_type_labels, |
||
143 | |||
144 | // search options. |
||
145 | 'defaultSort' => get_option( $prefix . 'default_sort', 'relevance' ), |
||
146 | 'excludedPostTypes' => explode( ',', get_option( $prefix . 'excluded_post_types', '' ) ), |
||
147 | |||
148 | // widget info. |
||
149 | 'hasOverlayWidgets' => count( $overlay_widget_ids ) > 0, |
||
150 | 'widgets' => array_values( $widgets ), |
||
151 | 'widgetsOutsideOverlay' => array_values( $widgets_outside_overlay ), |
||
152 | ); |
||
153 | |||
154 | /** |
||
155 | * Customize Instant Search Options. |
||
156 | * |
||
157 | * @module search |
||
158 | * |
||
159 | * @since 7.7.0 |
||
160 | * |
||
161 | * @param array $options Array of parameters used in Instant Search queries. |
||
162 | */ |
||
163 | $options = apply_filters( 'jetpack_instant_search_options', $options ); |
||
164 | |||
165 | // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280. |
||
166 | wp_add_inline_script( 'jetpack-instant-search', 'var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $options ) ) . '"));' ); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Registers a widget sidebar for Instant Search. |
||
171 | */ |
||
172 | public function register_jetpack_instant_sidebar() { |
||
185 | |||
186 | /** |
||
187 | * Prints Instant Search sidebar. |
||
188 | */ |
||
189 | public function print_instant_search_sidebar() { |
||
198 | |||
199 | /** |
||
200 | * Loads scripts for Tracks analytics library |
||
201 | */ |
||
202 | public function load_and_initialize_tracks() { |
||
205 | |||
206 | /** |
||
207 | * Bypass the normal Search query since we will run it with instant search. |
||
208 | * |
||
209 | * @since 8.3.0 |
||
210 | * |
||
211 | * @param array $posts Current array of posts (still pre-query). |
||
212 | * @param WP_Query $query The WP_Query being filtered. |
||
213 | * |
||
214 | * @return array Array of matching posts. |
||
215 | */ |
||
216 | public function filter__posts_pre_query( $posts, $query ) { |
||
232 | |||
233 | /** |
||
234 | * Run the aggregations API query for any filtering |
||
235 | * |
||
236 | * @since 8.3.0 |
||
237 | * |
||
238 | * @param WP_Query $query The WP_Query being filtered. |
||
239 | */ |
||
240 | public function action__parse_query( $query ) { |
||
265 | |||
266 | /** |
||
267 | * Run an instant search on the WordPress.com public API. |
||
268 | * |
||
269 | * @since 8.3.0 |
||
270 | * |
||
271 | * @param array $args Args conforming to the WP.com v1.3/sites/<blog_id>/search endpoint. |
||
272 | * |
||
273 | * @return object|WP_Error The response from the public API, or a WP_Error. |
||
274 | */ |
||
275 | public function instant_api( array $args ) { |
||
369 | |||
370 | /** |
||
371 | * Get the raw Aggregation results from the Elasticsearch response. |
||
372 | * |
||
373 | * @since 8.4.0 |
||
374 | * |
||
375 | * @return array Array of Aggregations performed on the search. |
||
376 | */ |
||
377 | public function get_search_aggregations_results() { |
||
384 | |||
385 | /** |
||
386 | * Automatically configure necessary settings for instant search |
||
387 | * |
||
388 | * @since 8.3.0 |
||
389 | */ |
||
390 | public function auto_config_search() { |
||
401 | |||
402 | /** |
||
403 | * Automatically copy configured search widgets into the overlay sidebar |
||
404 | * |
||
405 | * @since 8.8.0 |
||
406 | */ |
||
407 | public function auto_config_overlay_sidebar_widgets() { |
||
485 | |||
486 | /** |
||
487 | * Autoconfig search by adding filter widgets |
||
488 | * |
||
489 | * @since 8.4.0 |
||
490 | * |
||
491 | * @return array Array of config settings for search widget. |
||
492 | */ |
||
493 | protected function get_preconfig_widget_options() { |
||
553 | /** |
||
554 | * Automatically configure post types to exclude from one of the search widgets |
||
555 | * |
||
556 | * @since 8.8.0 |
||
557 | */ |
||
558 | public function auto_config_excluded_post_types() { |
||
581 | } |
||
582 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.