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 |
||
| 17 | class Jetpack_Search_Helpers { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The search widget's base ID. |
||
| 21 | * |
||
| 22 | * @since 5.8.0 |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | const FILTER_WIDGET_BASE = 'jetpack-search-filters'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Create a URL for the current search that doesn't include the "paged" parameter. |
||
| 29 | * |
||
| 30 | * @since 5.8.0 |
||
| 31 | * |
||
| 32 | * @return string The search URL. |
||
| 33 | */ |
||
| 34 | static function get_search_url() { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Wraps add_query_arg() with the URL defaulting to the current search URL. |
||
| 53 | * |
||
| 54 | * @see add_query_arg() |
||
| 55 | * |
||
| 56 | * @since 5.8.0 |
||
| 57 | * |
||
| 58 | * @param string|array $key Either a query variable key, or an associative array of query variables. |
||
| 59 | * @param string $value Optional. A query variable value. |
||
|
|
|||
| 60 | * @param bool|string $url Optional. A URL to act upon. Defaults to the current search URL. |
||
| 61 | * |
||
| 62 | * @return string New URL query string (unescaped). |
||
| 63 | */ |
||
| 64 | static function add_query_arg( $key, $value = false, $url = false ) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Wraps remove_query_arg() with the URL defaulting to the current search URL. |
||
| 75 | * |
||
| 76 | * @see remove_query_arg() |
||
| 77 | * |
||
| 78 | * @since 5.8.0 |
||
| 79 | * |
||
| 80 | * @param string|array $key Query key or keys to remove. |
||
| 81 | * @param bool|string $query Optional. A URL to act upon. Defaults to the current search URL. |
||
| 82 | * |
||
| 83 | * @return string New URL query string (unescaped). |
||
| 84 | */ |
||
| 85 | static function remove_query_arg( $key, $url = false ) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns the name of the search widget's option. |
||
| 93 | * |
||
| 94 | * @since 5.8.0 |
||
| 95 | * |
||
| 96 | * @return string The search widget option name. |
||
| 97 | */ |
||
| 98 | static function get_widget_option_name() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Returns the search widget instances from the widget's option. |
||
| 104 | * |
||
| 105 | * @since 5.8.0 |
||
| 106 | * |
||
| 107 | * @return array The widget options. |
||
| 108 | */ |
||
| 109 | static function get_widgets_from_option() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the widget ID (widget base plus the numeric ID). |
||
| 122 | * |
||
| 123 | * @param int $number The widget's numeric ID. |
||
| 124 | * |
||
| 125 | * @return string The widget's numeric ID prefixed with the search widget base. |
||
| 126 | */ |
||
| 127 | static function build_widget_id( $number ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Wrapper for is_active_widget() with the other parameters automatically supplied. |
||
| 133 | * |
||
| 134 | * @see is_active_widget() |
||
| 135 | * |
||
| 136 | * @since 5.8.0 |
||
| 137 | * |
||
| 138 | * @param int $widget_id Widget ID. |
||
| 139 | * |
||
| 140 | * @return bool Whether the widget is active or not. |
||
| 141 | */ |
||
| 142 | static function is_active_widget( $widget_id ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns an array of the filters from all active search widgets. |
||
| 148 | * |
||
| 149 | * @since 5.8.0 |
||
| 150 | * |
||
| 151 | * @return array Active filters. |
||
| 152 | */ |
||
| 153 | static function get_filters_from_widgets() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get the localized default label for a date filter. |
||
| 185 | * |
||
| 186 | * @since 5.8.0 |
||
| 187 | * |
||
| 188 | * @param string $type Date type, either year or month. |
||
| 189 | * @param bool $is_updated Whether the filter was updated or not (adds "Updated" to the end). |
||
| 190 | * |
||
| 191 | * @return string The filter label. |
||
| 192 | */ |
||
| 193 | static function get_date_filter_type_name( $type, $is_updated = false ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Creates a default name for a filter. Used when the filter label is blank. |
||
| 213 | * |
||
| 214 | * @since 5.8.0 |
||
| 215 | * |
||
| 216 | * @param array $widget_filter The filter to generate the title for. |
||
| 217 | * |
||
| 218 | * @return string The suggested filter name. |
||
| 219 | */ |
||
| 220 | static function generate_widget_filter_name( $widget_filter ) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Whether we should rerun a search in the customizer preview or not. |
||
| 269 | * |
||
| 270 | * @since 5.8.0 |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | static function should_rerun_search_in_customizer_preview() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Since PHP's built-in array_diff() works by comparing the values that are in array 1 to the other arrays, |
||
| 288 | * if there are less values in array 1, it's possible to get an empty diff where one might be expected. |
||
| 289 | * |
||
| 290 | * @since 5.8.0 |
||
| 291 | * |
||
| 292 | * @param array $array_1 |
||
| 293 | * @param array $array_2 |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | static function array_diff( $array_1, $array_2 ) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Given the widget instance, will return true when selected post types differ from searchable post types. |
||
| 313 | * |
||
| 314 | * @since 5.8.0 |
||
| 315 | * |
||
| 316 | * @param array $post_types An array of post types. |
||
| 317 | * |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | static function post_types_differ_searchable( $post_types ) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Given the array of post types, will return true when these differ from the current search query. |
||
| 333 | * |
||
| 334 | * @since 5.8.0 |
||
| 335 | * |
||
| 336 | * @param array $post_types An array of post types. |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | static function post_types_differ_query( $post_types ) { |
||
| 341 | if ( empty( $post_types ) ) { |
||
| 342 | return false; |
||
| 343 | } |
||
| 344 | |||
| 345 | if ( empty( $_GET['post_type'] ) ) { |
||
| 346 | $post_types_from_query = array(); |
||
| 347 | } elseif ( is_array( $_GET['post_type'] ) ) { |
||
| 348 | $post_types_from_query = $_GET['post_type']; |
||
| 349 | } else { |
||
| 350 | $post_types_from_query = (array) explode( ',', $_GET['post_type'] ); |
||
| 351 | } |
||
| 352 | |||
| 353 | $post_types_from_query = array_map( 'trim', $post_types_from_query ); |
||
| 354 | |||
| 355 | $diff_query = self::array_diff( (array) $post_types, $post_types_from_query ); |
||
| 356 | |||
| 357 | return ! empty( $diff_query ); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Determine what Tracks value should be used when updating a widget. |
||
| 362 | * |
||
| 363 | * @since 5.8.0 |
||
| 364 | * |
||
| 365 | * @param mixed $old_value The old option value. |
||
| 366 | * @param mixed $new_value The new option value. |
||
| 367 | * |
||
| 368 | * @return array|false False if the widget wasn't updated, otherwise an array of the Tracks action and widget properties. |
||
| 369 | */ |
||
| 370 | static function get_widget_tracks_value( $old_value, $new_value ) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Creates the widget properties for sending to Tracks. |
||
| 452 | * |
||
| 453 | * @since 5.8.0 |
||
| 454 | * |
||
| 455 | * @param array $widget The widget instance. |
||
| 456 | * |
||
| 457 | * @return array The widget properties. |
||
| 458 | */ |
||
| 459 | static function get_widget_properties_for_tracks( $widget ) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Creates the filter properties for sending to Tracks. |
||
| 483 | * |
||
| 484 | * @since 5.8.0 |
||
| 485 | * |
||
| 486 | * @param array $filters An array of filters. |
||
| 487 | * |
||
| 488 | * @return array The filter properties. |
||
| 489 | */ |
||
| 490 | static function get_filter_properties_for_tracks( $filters ) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Gets the active post types given a set of filters. |
||
| 517 | * |
||
| 518 | * @since 5.8.0 |
||
| 519 | * |
||
| 520 | * @param array $filters The active filters for the current query. |
||
| 521 | * |
||
| 522 | * @return array The active post types. |
||
| 523 | */ |
||
| 524 | public static function get_active_post_types( $filters ) { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Sets active to false on all post type buckets. |
||
| 538 | * |
||
| 539 | * @since 5.8.0 |
||
| 540 | * |
||
| 541 | * @param array $filters The available filters for the current query. |
||
| 542 | * |
||
| 543 | * @return array The filters for the current query with modified active field. |
||
| 544 | */ |
||
| 545 | public static function remove_active_from_post_type_buckets( $filters ) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Given a url and an array of post types, will ensure that the post types are properly applied to the URL as args. |
||
| 561 | * |
||
| 562 | * @since 5.8.0 |
||
| 563 | * |
||
| 564 | * @param string $url The URL to add post types to. |
||
| 565 | * @param array $post_types An array of post types that should be added to the URL. |
||
| 566 | * |
||
| 567 | * @return string The URL with added post types. |
||
| 568 | */ |
||
| 569 | public static function add_post_types_to_url( $url, $post_types ) { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Since we provide support for the widget restricting post types by adding the selected post types as |
||
| 586 | * active filters, if removing a post type filter would result in there no longer be post_type args in the URL, |
||
| 587 | * we need to be sure to add them back. |
||
| 588 | * |
||
| 589 | * @since 5.8.0 |
||
| 590 | * |
||
| 591 | * @param array $filters An array of possible filters for the current query. |
||
| 592 | * @param array $post_types The post types to ensure are on the link. |
||
| 593 | * |
||
| 594 | * @return array The updated array of filters with post typed added to the remove URLs. |
||
| 595 | */ |
||
| 596 | public static function ensure_post_types_on_remove_url( $filters, $post_types ) { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Wraps a WordPress filter called "jetpack_search_disable_widget_filters" that allows |
||
| 634 | * developers to disable filters supplied by the search widget. Useful if filters are |
||
| 635 | * being defined at the code level. |
||
| 636 | * |
||
| 637 | * @since 5.8.0 |
||
| 638 | * |
||
| 639 | * @return bool |
||
| 640 | */ |
||
| 641 | public static function are_filters_by_widget_disabled() { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Returns a boolean for whether the current site has a VIP index. |
||
| 657 | * |
||
| 658 | * @since 5.8.0 |
||
| 659 | * |
||
| 660 | * @return bool |
||
| 661 | */ |
||
| 662 | public static function site_has_vip_index() { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Returns the maximum posts per page for a search query. |
||
| 682 | * |
||
| 683 | * @since 5.8.0 |
||
| 684 | * |
||
| 685 | * @return int |
||
| 686 | */ |
||
| 687 | public static function get_max_posts_per_page() { |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Returns the maximum offset for a search query. |
||
| 693 | * |
||
| 694 | * @since 5.8.0 |
||
| 695 | * |
||
| 696 | * @return int |
||
| 697 | */ |
||
| 698 | public static function get_max_offset() { |
||
| 701 | } |
||
| 702 |
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.