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_Search_Helpers 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_Search_Helpers, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Jetpack_Search_Helpers { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The search widget's base ID. |
||
| 19 | * |
||
| 20 | * @since 5.8.0 |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | const FILTER_WIDGET_BASE = 'jetpack-search-filters'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Create a URL for the current search that doesn't include the "paged" parameter. |
||
| 27 | * |
||
| 28 | * @since 5.8.0 |
||
| 29 | * |
||
| 30 | * @return string The search URL. |
||
| 31 | */ |
||
| 32 | static function get_search_url() { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Wraps add_query_arg() with the URL defaulting to the current search URL. |
||
| 51 | * |
||
| 52 | * @see add_query_arg() |
||
| 53 | * |
||
| 54 | * @since 5.8.0 |
||
| 55 | * |
||
| 56 | * @param string|array $key Either a query variable key, or an associative array of query variables. |
||
| 57 | * @param string $value Optional. A query variable value. |
||
|
|
|||
| 58 | * @param bool|string $url Optional. A URL to act upon. Defaults to the current search URL. |
||
| 59 | * |
||
| 60 | * @return string New URL query string (unescaped). |
||
| 61 | */ |
||
| 62 | static function add_query_arg( $key, $value = false, $url = false ) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Wraps remove_query_arg() with the URL defaulting to the current search URL. |
||
| 73 | * |
||
| 74 | * @see remove_query_arg() |
||
| 75 | * |
||
| 76 | * @since 5.8.0 |
||
| 77 | * |
||
| 78 | * @param string|array $key Query key or keys to remove. |
||
| 79 | * @param bool|string $query Optional. A URL to act upon. Defaults to the current search URL. |
||
| 80 | * |
||
| 81 | * @return string New URL query string (unescaped). |
||
| 82 | */ |
||
| 83 | static function remove_query_arg( $key, $url = false ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns the name of the search widget's option. |
||
| 91 | * |
||
| 92 | * @since 5.8.0 |
||
| 93 | * |
||
| 94 | * @return string The search widget option name. |
||
| 95 | */ |
||
| 96 | static function get_widget_option_name() { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns the search widget instances from the widget's option. |
||
| 102 | * |
||
| 103 | * @since 5.8.0 |
||
| 104 | * |
||
| 105 | * @return array The widget options. |
||
| 106 | */ |
||
| 107 | static function get_widgets_from_option() { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns the widget ID (widget base plus the numeric ID). |
||
| 120 | * |
||
| 121 | * @param int $number The widget's numeric ID. |
||
| 122 | * |
||
| 123 | * @return string The widget's numeric ID prefixed with the search widget base. |
||
| 124 | */ |
||
| 125 | static function build_widget_id( $number ) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Wrapper for is_active_widget() with the other parameters automatically supplied. |
||
| 131 | * |
||
| 132 | * @see is_active_widget() |
||
| 133 | * |
||
| 134 | * @since 5.8.0 |
||
| 135 | * |
||
| 136 | * @param int $widget_id Widget ID. |
||
| 137 | * |
||
| 138 | * @return bool Whether the widget is active or not. |
||
| 139 | */ |
||
| 140 | static function is_active_widget( $widget_id ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns an array of the filters from all active search widgets. |
||
| 146 | * |
||
| 147 | * @since 5.8.0 |
||
| 148 | * |
||
| 149 | * @return array Active filters. |
||
| 150 | */ |
||
| 151 | static function get_filters_from_widgets() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the localized default label for a date filter. |
||
| 183 | * |
||
| 184 | * @since 5.8.0 |
||
| 185 | * |
||
| 186 | * @param string $type Date type, either year or month. |
||
| 187 | * @param bool $is_updated Whether the filter was updated or not (adds "Updated" to the end). |
||
| 188 | * |
||
| 189 | * @return string The filter label. |
||
| 190 | */ |
||
| 191 | static function get_date_filter_type_name( $type, $is_updated = false ) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Creates a default name for a filter. Used when the filter label is blank. |
||
| 211 | * |
||
| 212 | * @since 5.8.0 |
||
| 213 | * |
||
| 214 | * @param array $widget_filter The filter to generate the title for. |
||
| 215 | * |
||
| 216 | * @return string The suggested filter name. |
||
| 217 | */ |
||
| 218 | static function generate_widget_filter_name( $widget_filter ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Whether we should rerun a search in the customizer preview or not. |
||
| 267 | * |
||
| 268 | * @since 5.8.0 |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | static function should_rerun_search_in_customizer_preview() { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Since PHP's built-in array_diff() works by comparing the values that are in array 1 to the other arrays, |
||
| 286 | * if there are less values in array 1, it's possible to get an empty diff where one might be expected. |
||
| 287 | * |
||
| 288 | * @since 5.8.0 |
||
| 289 | * |
||
| 290 | * @param array $array_1 |
||
| 291 | * @param array $array_2 |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | static function array_diff( $array_1, $array_2 ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Given the widget instance, will return true when selected post types differ from searchable post types. |
||
| 311 | * |
||
| 312 | * @since 5.8.0 |
||
| 313 | * |
||
| 314 | * @param array $post_types An array of post types. |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | static function post_types_differ_searchable( $post_types ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Given the array of post types, will return true when these differ from the current search query. |
||
| 331 | * |
||
| 332 | * @since 5.8.0 |
||
| 333 | * |
||
| 334 | * @param array $post_types An array of post types. |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | static function post_types_differ_query( $post_types ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Determine what Tracks value should be used when updating a widget. |
||
| 360 | * |
||
| 361 | * @since 5.8.0 |
||
| 362 | * |
||
| 363 | * @param mixed $old_value The old option value. |
||
| 364 | * @param mixed $new_value The new option value. |
||
| 365 | * |
||
| 366 | * @return array|false False if the widget wasn't updated, otherwise an array of the Tracks action and widget properties. |
||
| 367 | */ |
||
| 368 | static function get_widget_tracks_value( $old_value, $new_value ) { |
||
| 369 | $old_value = (array) $old_value; |
||
| 370 | if ( isset( $old_value['_multiwidget'] ) ) { |
||
| 371 | unset( $old_value['_multiwidget'] ); |
||
| 372 | } |
||
| 373 | |||
| 374 | $new_value = (array) $new_value; |
||
| 375 | if ( isset( $new_value['_multiwidget'] ) ) { |
||
| 376 | unset( $new_value['_multiwidget'] ); |
||
| 377 | } |
||
| 378 | |||
| 379 | $old_keys = array_keys( $old_value ); |
||
| 380 | $new_keys = array_keys( $new_value ); |
||
| 381 | |||
| 382 | if ( count( $new_keys ) > count( $old_keys ) ) { // This is the case for a widget being added |
||
| 383 | $diff = self::array_diff( $new_keys, $old_keys ); |
||
| 384 | $action = 'widget_added'; |
||
| 385 | $widget = empty( $diff ) || ! isset( $new_value[ $diff[0] ] ) |
||
| 386 | ? false |
||
| 387 | : $new_value[ $diff[0] ]; |
||
| 388 | } elseif ( count( $old_keys ) > count( $new_keys ) ) { // This is the case for a widget being deleted |
||
| 389 | $diff = self::array_diff( $old_keys, $new_keys ); |
||
| 390 | $action = 'widget_deleted'; |
||
| 391 | $widget = empty( $diff ) || ! isset( $old_value[ $diff[0] ] ) |
||
| 392 | ? false |
||
| 393 | : $old_value[ $diff[0] ]; |
||
| 394 | } else { |
||
| 395 | $action = 'widget_updated'; |
||
| 396 | $widget = false; |
||
| 397 | |||
| 398 | // This is a bit crazy. Since there can be multiple widgets stored in a single option, |
||
| 399 | // we need to diff the old and new values to figure out which widget was updated. |
||
| 400 | foreach ( $new_value as $key => $new_instance ) { |
||
| 401 | if ( ! isset( $old_value[ $key ] ) ) { |
||
| 402 | continue; |
||
| 403 | } |
||
| 404 | $old_instance = $old_value[ $key ]; |
||
| 405 | |||
| 406 | // First, let's test the keys of each instance |
||
| 407 | $diff = self::array_diff( array_keys( $new_instance ), array_keys( $old_instance ) ); |
||
| 408 | if ( ! empty( $diff ) ) { |
||
| 409 | $widget = $new_instance; |
||
| 410 | break; |
||
| 411 | } |
||
| 412 | |||
| 413 | // Next, lets's loop over each value and compare it |
||
| 414 | foreach ( $new_instance as $k => $v ) { |
||
| 415 | if ( is_scalar( $v ) && (string) $v !== (string) $old_instance[ $k ] ) { |
||
| 416 | $widget = $new_instance; |
||
| 417 | break; |
||
| 418 | } |
||
| 419 | |||
| 420 | if ( 'filters' == $k ) { |
||
| 421 | if ( count( $new_instance['filters'] ) != count( $old_instance['filters'] ) ) { |
||
| 422 | $widget = $new_instance; |
||
| 423 | break; |
||
| 424 | } |
||
| 425 | |||
| 426 | foreach ( $v as $filter_key => $new_filter_value ) { |
||
| 427 | $diff = self::array_diff( $new_filter_value, $old_instance['filters'][ $filter_key ] ); |
||
| 428 | if ( ! empty( $diff ) ) { |
||
| 429 | $widget = $new_instance; |
||
| 430 | break; |
||
| 431 | } |
||
| 432 | } |
||
| 433 | } |
||
| 434 | } |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | if ( empty( $action ) || empty( $widget ) ) { |
||
| 439 | return false; |
||
| 440 | } |
||
| 441 | |||
| 442 | return array( |
||
| 443 | 'action' => $action, |
||
| 444 | 'widget' => self::get_widget_properties_for_tracks( $widget ), |
||
| 445 | ); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Creates the widget properties for sending to Tracks. |
||
| 450 | * |
||
| 451 | * @since 5.8.0 |
||
| 452 | * |
||
| 453 | * @param array $widget The widget instance. |
||
| 454 | * |
||
| 455 | * @return array The widget properties. |
||
| 456 | */ |
||
| 457 | static function get_widget_properties_for_tracks( $widget ) { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Creates the filter properties for sending to Tracks. |
||
| 481 | * |
||
| 482 | * @since 5.8.0 |
||
| 483 | * |
||
| 484 | * @param array $filters An array of filters. |
||
| 485 | * |
||
| 486 | * @return array The filter properties. |
||
| 487 | */ |
||
| 488 | static function get_filter_properties_for_tracks( $filters ) { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Gets the active post types given a set of filters. |
||
| 515 | * |
||
| 516 | * @since 5.8.0 |
||
| 517 | * |
||
| 518 | * @param array $filters The active filters for the current query. |
||
| 519 | * |
||
| 520 | * @return array The active post types. |
||
| 521 | */ |
||
| 522 | public static function get_active_post_types( $filters ) { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Sets active to false on all post type buckets. |
||
| 536 | * |
||
| 537 | * @since 5.8.0 |
||
| 538 | * |
||
| 539 | * @param array $filters The available filters for the current query. |
||
| 540 | * |
||
| 541 | * @return array The filters for the current query with modified active field. |
||
| 542 | */ |
||
| 543 | public static function remove_active_from_post_type_buckets( $filters ) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Given a url and an array of post types, will ensure that the post types are properly applied to the URL as args. |
||
| 559 | * |
||
| 560 | * @since 5.8.0 |
||
| 561 | * |
||
| 562 | * @param string $url The URL to add post types to. |
||
| 563 | * @param array $post_types An array of post types that should be added to the URL. |
||
| 564 | * |
||
| 565 | * @return string The URL with added post types. |
||
| 566 | */ |
||
| 567 | public static function add_post_types_to_url( $url, $post_types ) { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Since we provide support for the widget restricting post types by adding the selected post types as |
||
| 584 | * active filters, if removing a post type filter would result in there no longer be post_type args in the URL, |
||
| 585 | * we need to be sure to add them back. |
||
| 586 | * |
||
| 587 | * @since 5.8.0 |
||
| 588 | * |
||
| 589 | * @param array $filters An array of possible filters for the current query. |
||
| 590 | * @param array $post_types The post types to ensure are on the link. |
||
| 591 | * |
||
| 592 | * @return array The updated array of filters with post typed added to the remove URLs. |
||
| 593 | */ |
||
| 594 | public static function ensure_post_types_on_remove_url( $filters, $post_types ) { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Returns a boolean for whether the current site has a VIP index. |
||
| 632 | * |
||
| 633 | * @since 5.8.0 |
||
| 634 | * |
||
| 635 | * @return bool |
||
| 636 | */ |
||
| 637 | public static function site_has_vip_index() { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Returns the maximum posts per page for a search query. |
||
| 657 | * |
||
| 658 | * @since 5.8.0 |
||
| 659 | * |
||
| 660 | * @return int |
||
| 661 | */ |
||
| 662 | public static function get_max_posts_per_page() { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Returns the maximum offset for a search query. |
||
| 668 | * |
||
| 669 | * @since 5.8.0 |
||
| 670 | * |
||
| 671 | * @return int |
||
| 672 | */ |
||
| 673 | public static function get_max_offset() { |
||
| 676 | } |
||
| 677 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.