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 | * Bypass the normal Search query since we will run it with instant search. |
||
| 197 | * |
||
| 198 | * @since 8.3.0 |
||
| 199 | * |
||
| 200 | * @param array $posts Current array of posts (still pre-query). |
||
| 201 | * @param WP_Query $query The WP_Query being filtered. |
||
| 202 | * |
||
| 203 | * @return array Array of matching posts. |
||
| 204 | */ |
||
| 205 | public function filter__posts_pre_query( $posts, $query ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Run the aggregations API query for any filtering |
||
| 224 | * |
||
| 225 | * @since 8.3.0 |
||
| 226 | * |
||
| 227 | * @param WP_Query $query The WP_Query being filtered. |
||
| 228 | */ |
||
| 229 | public function action__parse_query( $query ) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Run an instant search on the WordPress.com public API. |
||
| 257 | * |
||
| 258 | * @since 8.3.0 |
||
| 259 | * |
||
| 260 | * @param array $args Args conforming to the WP.com v1.3/sites/<blog_id>/search endpoint. |
||
| 261 | * |
||
| 262 | * @return object|WP_Error The response from the public API, or a WP_Error. |
||
| 263 | */ |
||
| 264 | public function instant_api( array $args ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get the raw Aggregation results from the Elasticsearch response. |
||
| 361 | * |
||
| 362 | * @since 8.4.0 |
||
| 363 | * |
||
| 364 | * @return array Array of Aggregations performed on the search. |
||
| 365 | */ |
||
| 366 | public function get_search_aggregations_results() { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Autoconfig search by adding filter widgets |
||
| 376 | * |
||
| 377 | * @since 8.3.0 |
||
| 378 | */ |
||
| 379 | public function auto_config_search() { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Autoconfig search by adding filter widgets |
||
| 460 | * |
||
| 461 | * @since 8.4.0 |
||
| 462 | * |
||
| 463 | * @return array Array of config settings for search widget. |
||
| 464 | */ |
||
| 465 | protected function get_preconfig_widget_options() { |
||
| 528 | |||
| 529 | } |
||
| 530 |
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..