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() { | 
            ||
| 33 | |||
| 34 | /**  | 
            ||
| 35 | * Setup the various hooks needed for the plugin to take over search duties.  | 
            ||
| 36 | *  | 
            ||
| 37 | * @since 5.0.0  | 
            ||
| 38 | */  | 
            ||
| 39 | View Code Duplication | 	public function init_hooks() { | 
            |
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * Loads assets for Jetpack Instant Search Prototype featuring Search As You Type experience.  | 
            ||
| 58 | */  | 
            ||
| 59 | 	public function load_assets() { | 
            ||
| 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 = array_key_exists( 'jetpack-instant-search-sidebar', get_option( 'sidebars_widgets', array() ) ) ?  | 
            ||
| 87 | get_option( 'sidebars_widgets', array() )['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(), '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 | $options = array(  | 
            ||
| 121 | 'overlayOptions' => array(  | 
            ||
| 122 | 'colorTheme' => get_option( $prefix . 'color_theme', 'light' ),  | 
            ||
| 123 | 'enableInfScroll' => (bool) get_option( $prefix . 'inf_scroll', false ),  | 
            ||
| 124 | 'highlightColor' => get_option( $prefix . 'highlight_color', '#FFC' ),  | 
            ||
| 125 | 'opacity' => (int) get_option( $prefix . 'opacity', 97 ),  | 
            ||
| 126 | 'showPoweredBy' => (bool) get_option( $prefix . 'show_powered_by', true ),  | 
            ||
| 127 | ),  | 
            ||
| 128 | |||
| 129 | // core config.  | 
            ||
| 130 | 'homeUrl' => home_url(),  | 
            ||
| 131 | 'locale' => str_replace( '_', '-', Jetpack_Search_Helpers::is_valid_locale( get_locale() ) ? get_locale() : 'en_US' ),  | 
            ||
| 132 | 'postsPerPage' => get_option( 'posts_per_page' ),  | 
            ||
| 133 | 'siteId' => Jetpack::get_option( 'id' ),  | 
            ||
| 134 | |||
| 135 | // filtering.  | 
            ||
| 136 | 'postTypeFilters' => isset( $widget_options['post_types'] ) ? $widget_options['post_types'] : array(),  | 
            ||
| 137 | 'postTypes' => $post_type_labels,  | 
            ||
| 138 | 'sort' => isset( $widget_options['sort'] ) ? $widget_options['sort'] : null,  | 
            ||
| 139 | 'widgets' => array_values( $widgets ),  | 
            ||
| 140 | 'widgetsOutsideOverlay' => array_values( $widgets_outside_overlay ),  | 
            ||
| 141 | );  | 
            ||
| 142 | |||
| 143 | /**  | 
            ||
| 144 | * Customize Instant Search Options.  | 
            ||
| 145 | *  | 
            ||
| 146 | * @module search  | 
            ||
| 147 | *  | 
            ||
| 148 | * @since 7.7.0  | 
            ||
| 149 | *  | 
            ||
| 150 | * @param array $options Array of parameters used in Instant Search queries.  | 
            ||
| 151 | */  | 
            ||
| 152 | $options = apply_filters( 'jetpack_instant_search_options', $options );  | 
            ||
| 153 | |||
| 154 | // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280.  | 
            ||
| 155 | 		wp_add_inline_script( 'jetpack-instant-search', 'var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $options ) ) . '"));' ); | 
            ||
| 156 | }  | 
            ||
| 157 | |||
| 158 | /**  | 
            ||
| 159 | * Registers a widget sidebar for Instant Search.  | 
            ||
| 160 | */  | 
            ||
| 161 | 	public function register_jetpack_instant_sidebar() { | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * Prints Instant Search sidebar.  | 
            ||
| 177 | */  | 
            ||
| 178 | 	public function print_instant_search_sidebar() { | 
            ||
| 187 | |||
| 188 | /**  | 
            ||
| 189 | * Loads scripts for Tracks analytics library  | 
            ||
| 190 | */  | 
            ||
| 191 | 	public function load_and_initialize_tracks() { | 
            ||
| 194 | |||
| 195 | /**  | 
            ||
| 196 | * Get the version number to use when loading the file. Allows us to bypass cache when developing.  | 
            ||
| 197 | *  | 
            ||
| 198 | * @param string $file Path of the file we are looking for.  | 
            ||
| 199 | * @return string $script_version Version number.  | 
            ||
| 200 | */  | 
            ||
| 201 | 	public static function get_asset_version( $file ) { | 
            ||
| 206 | |||
| 207 | /**  | 
            ||
| 208 | * Bypass the normal Search query since we will run it with instant search.  | 
            ||
| 209 | *  | 
            ||
| 210 | * @since 8.3.0  | 
            ||
| 211 | *  | 
            ||
| 212 | * @param array $posts Current array of posts (still pre-query).  | 
            ||
| 213 | * @param WP_Query $query The WP_Query being filtered.  | 
            ||
| 214 | *  | 
            ||
| 215 | * @return array Array of matching posts.  | 
            ||
| 216 | */  | 
            ||
| 217 | 	public function filter__posts_pre_query( $posts, $query ) { | 
            ||
| 233 | |||
| 234 | /**  | 
            ||
| 235 | * Run the aggregations API query for any filtering  | 
            ||
| 236 | *  | 
            ||
| 237 | * @since 8.3.0  | 
            ||
| 238 | *  | 
            ||
| 239 | * @param WP_Query $query The WP_Query being filtered.  | 
            ||
| 240 | */  | 
            ||
| 241 | 	public function action__parse_query( $query ) { | 
            ||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * Run an instant search on the WordPress.com public API.  | 
            ||
| 269 | *  | 
            ||
| 270 | * @since 8.3.0  | 
            ||
| 271 | *  | 
            ||
| 272 | * @param array $args Args conforming to the WP.com v1.3/sites/<blog_id>/search endpoint.  | 
            ||
| 273 | *  | 
            ||
| 274 | * @return object|WP_Error The response from the public API, or a WP_Error.  | 
            ||
| 275 | */  | 
            ||
| 276 | 	public function instant_api( array $args ) { | 
            ||
| 370 | |||
| 371 | /**  | 
            ||
| 372 | * Get the raw Aggregation results from the Elasticsearch response.  | 
            ||
| 373 | *  | 
            ||
| 374 | * @since 8.3.0  | 
            ||
| 375 | *  | 
            ||
| 376 | * @return array Array of Aggregations performed on the search.  | 
            ||
| 377 | */  | 
            ||
| 378 | 	public function get_search_aggregations_results() { | 
            ||
| 385 | |||
| 386 | |||
| 387 | }  | 
            ||
| 388 | 
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..