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 |
||
19 | class Jetpack_Search_Helpers { |
||
20 | |||
21 | /** |
||
22 | * The search widget's base ID. |
||
23 | * |
||
24 | * @since 5.8.0 |
||
25 | * @var string |
||
26 | */ |
||
27 | const FILTER_WIDGET_BASE = 'jetpack-search-filters'; |
||
28 | |||
29 | /** |
||
30 | * Create a URL for the current search that doesn't include the "paged" parameter. |
||
31 | * |
||
32 | * @since 5.8.0 |
||
33 | * |
||
34 | * @return string The search URL. |
||
35 | */ |
||
36 | static function get_search_url() { |
||
52 | |||
53 | /** |
||
54 | * Wraps add_query_arg() with the URL defaulting to the current search URL. |
||
55 | * |
||
56 | * @see add_query_arg() |
||
57 | * |
||
58 | * @since 5.8.0 |
||
59 | * |
||
60 | * @param string|array $key Either a query variable key, or an associative array of query variables. |
||
61 | * @param string $value Optional. A query variable value. |
||
|
|||
62 | * @param bool|string $url Optional. A URL to act upon. Defaults to the current search URL. |
||
63 | * |
||
64 | * @return string New URL query string (unescaped). |
||
65 | */ |
||
66 | static function add_query_arg( $key, $value = false, $url = false ) { |
||
74 | |||
75 | /** |
||
76 | * Wraps remove_query_arg() with the URL defaulting to the current search URL. |
||
77 | * |
||
78 | * @see remove_query_arg() |
||
79 | * |
||
80 | * @since 5.8.0 |
||
81 | * |
||
82 | * @param string|array $key Query key or keys to remove. |
||
83 | * @param bool|string $query Optional. A URL to act upon. Defaults to the current search URL. |
||
84 | * |
||
85 | * @return string New URL query string (unescaped). |
||
86 | */ |
||
87 | static function remove_query_arg( $key, $url = false ) { |
||
92 | |||
93 | /** |
||
94 | * Returns the name of the search widget's option. |
||
95 | * |
||
96 | * @since 5.8.0 |
||
97 | * |
||
98 | * @return string The search widget option name. |
||
99 | */ |
||
100 | static function get_widget_option_name() { |
||
103 | |||
104 | /** |
||
105 | * Returns the search widget instances from the widget's option. |
||
106 | * |
||
107 | * @since 5.8.0 |
||
108 | * |
||
109 | * @return array The widget options. |
||
110 | */ |
||
111 | static function get_widgets_from_option() { |
||
121 | |||
122 | /** |
||
123 | * Returns the widget ID (widget base plus the numeric ID). |
||
124 | * |
||
125 | * @param int $number The widget's numeric ID. |
||
126 | * |
||
127 | * @return string The widget's numeric ID prefixed with the search widget base. |
||
128 | */ |
||
129 | static function build_widget_id( $number ) { |
||
132 | |||
133 | /** |
||
134 | * Wrapper for is_active_widget() with the other parameters automatically supplied. |
||
135 | * |
||
136 | * @see is_active_widget() |
||
137 | * |
||
138 | * @since 5.8.0 |
||
139 | * |
||
140 | * @param int $widget_id Widget ID. |
||
141 | * |
||
142 | * @return bool Whether the widget is active or not. |
||
143 | */ |
||
144 | static function is_active_widget( $widget_id ) { |
||
147 | |||
148 | /** |
||
149 | * Returns an array of the filters from all active search widgets. |
||
150 | * |
||
151 | * @since 5.8.0 |
||
152 | * |
||
153 | * @param array|null $whitelisted_widget_ids array of whitelisted widget IDs. |
||
154 | * |
||
155 | * @return array Active filters. |
||
156 | */ |
||
157 | public static function get_filters_from_widgets( $whitelisted_widget_ids = null ) { |
||
189 | |||
190 | /** |
||
191 | * Get the localized default label for a date filter. |
||
192 | * |
||
193 | * @since 5.8.0 |
||
194 | * |
||
195 | * @param string $type Date type, either year or month. |
||
196 | * @param bool $is_updated Whether the filter was updated or not (adds "Updated" to the end). |
||
197 | * |
||
198 | * @return string The filter label. |
||
199 | */ |
||
200 | static function get_date_filter_type_name( $type, $is_updated = false ) { |
||
217 | |||
218 | /** |
||
219 | * Creates a default name for a filter. Used when the filter label is blank. |
||
220 | * |
||
221 | * @since 5.8.0 |
||
222 | * |
||
223 | * @param array $widget_filter The filter to generate the title for. |
||
224 | * |
||
225 | * @return string The suggested filter name. |
||
226 | */ |
||
227 | static function generate_widget_filter_name( $widget_filter ) { |
||
273 | |||
274 | /** |
||
275 | * Whether we should rerun a search in the customizer preview or not. |
||
276 | * |
||
277 | * @since 5.8.0 |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | static function should_rerun_search_in_customizer_preview() { |
||
292 | |||
293 | /** |
||
294 | * Since PHP's built-in array_diff() works by comparing the values that are in array 1 to the other arrays, |
||
295 | * if there are less values in array 1, it's possible to get an empty diff where one might be expected. |
||
296 | * |
||
297 | * @since 5.8.0 |
||
298 | * |
||
299 | * @param array $array_1 |
||
300 | * @param array $array_2 |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | static function array_diff( $array_1, $array_2 ) { |
||
317 | |||
318 | /** |
||
319 | * Given the widget instance, will return true when selected post types differ from searchable post types. |
||
320 | * |
||
321 | * @since 5.8.0 |
||
322 | * |
||
323 | * @param array $post_types An array of post types. |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | static function post_types_differ_searchable( $post_types ) { |
||
337 | |||
338 | /** |
||
339 | * Given the array of post types, will return true when these differ from the current search query. |
||
340 | * |
||
341 | * @since 5.8.0 |
||
342 | * |
||
343 | * @param array $post_types An array of post types. |
||
344 | * |
||
345 | * @return bool |
||
346 | */ |
||
347 | static function post_types_differ_query( $post_types ) { |
||
366 | |||
367 | /** |
||
368 | * Determine what Tracks value should be used when updating a widget. |
||
369 | * |
||
370 | * @since 5.8.0 |
||
371 | * |
||
372 | * @param mixed $old_value The old option value. |
||
373 | * @param mixed $new_value The new option value. |
||
374 | * |
||
375 | * @return array|false False if the widget wasn't updated, otherwise an array of the Tracks action and widget properties. |
||
376 | */ |
||
377 | static function get_widget_tracks_value( $old_value, $new_value ) { |
||
456 | |||
457 | /** |
||
458 | * Creates the widget properties for sending to Tracks. |
||
459 | * |
||
460 | * @since 5.8.0 |
||
461 | * |
||
462 | * @param array $widget The widget instance. |
||
463 | * |
||
464 | * @return array The widget properties. |
||
465 | */ |
||
466 | static function get_widget_properties_for_tracks( $widget ) { |
||
487 | |||
488 | /** |
||
489 | * Creates the filter properties for sending to Tracks. |
||
490 | * |
||
491 | * @since 5.8.0 |
||
492 | * |
||
493 | * @param array $filters An array of filters. |
||
494 | * |
||
495 | * @return array The filter properties. |
||
496 | */ |
||
497 | static function get_filter_properties_for_tracks( $filters ) { |
||
521 | |||
522 | /** |
||
523 | * Gets the active post types given a set of filters. |
||
524 | * |
||
525 | * @since 5.8.0 |
||
526 | * |
||
527 | * @param array $filters The active filters for the current query. |
||
528 | * |
||
529 | * @return array The active post types. |
||
530 | */ |
||
531 | public static function get_active_post_types( $filters ) { |
||
542 | |||
543 | /** |
||
544 | * Sets active to false on all post type buckets. |
||
545 | * |
||
546 | * @since 5.8.0 |
||
547 | * |
||
548 | * @param array $filters The available filters for the current query. |
||
549 | * |
||
550 | * @return array The filters for the current query with modified active field. |
||
551 | */ |
||
552 | public static function remove_active_from_post_type_buckets( $filters ) { |
||
565 | |||
566 | /** |
||
567 | * Given a url and an array of post types, will ensure that the post types are properly applied to the URL as args. |
||
568 | * |
||
569 | * @since 5.8.0 |
||
570 | * |
||
571 | * @param string $url The URL to add post types to. |
||
572 | * @param array $post_types An array of post types that should be added to the URL. |
||
573 | * |
||
574 | * @return string The URL with added post types. |
||
575 | */ |
||
576 | public static function add_post_types_to_url( $url, $post_types ) { |
||
590 | |||
591 | /** |
||
592 | * Since we provide support for the widget restricting post types by adding the selected post types as |
||
593 | * active filters, if removing a post type filter would result in there no longer be post_type args in the URL, |
||
594 | * we need to be sure to add them back. |
||
595 | * |
||
596 | * @since 5.8.0 |
||
597 | * |
||
598 | * @param array $filters An array of possible filters for the current query. |
||
599 | * @param array $post_types The post types to ensure are on the link. |
||
600 | * |
||
601 | * @return array The updated array of filters with post typed added to the remove URLs. |
||
602 | */ |
||
603 | public static function ensure_post_types_on_remove_url( $filters, $post_types ) { |
||
638 | |||
639 | /** |
||
640 | * Wraps a WordPress filter called "jetpack_search_disable_widget_filters" that allows |
||
641 | * developers to disable filters supplied by the search widget. Useful if filters are |
||
642 | * being defined at the code level. |
||
643 | * |
||
644 | * @since 5.8.0 |
||
645 | * |
||
646 | * @return bool |
||
647 | */ |
||
648 | public static function are_filters_by_widget_disabled() { |
||
661 | |||
662 | /** |
||
663 | * Returns the maximum posts per page for a search query. |
||
664 | * |
||
665 | * @since 5.8.0 |
||
666 | * |
||
667 | * @return int |
||
668 | */ |
||
669 | public static function get_max_posts_per_page() { |
||
672 | |||
673 | /** |
||
674 | * Returns the maximum offset for a search query. |
||
675 | * |
||
676 | * @since 5.8.0 |
||
677 | * |
||
678 | * @return int |
||
679 | */ |
||
680 | public static function get_max_offset() { |
||
683 | |||
684 | /** |
||
685 | * Returns the maximum offset for a search query. |
||
686 | * |
||
687 | * @since 8.4.0 |
||
688 | * @param string $locale A potentially valid locale string. |
||
689 | * |
||
690 | * @return bool |
||
691 | */ |
||
692 | public static function is_valid_locale( $locale ) { |
||
703 | } |
||
704 |
This check looks for
@param
annotations 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.