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_Widget 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_Widget, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Jetpack_Search_Widget extends WP_Widget { |
||
37 | |||
38 | /** |
||
39 | * The Jetpack_Search instance. |
||
40 | * |
||
41 | * @since 5.7.0 |
||
42 | * @var Jetpack_Search |
||
43 | */ |
||
44 | protected $jetpack_search; |
||
45 | |||
46 | /** |
||
47 | * Number of aggregations (filters) to show by default. |
||
48 | * |
||
49 | * @since 5.8.0 |
||
50 | * @var int |
||
51 | */ |
||
52 | const DEFAULT_FILTER_COUNT = 5; |
||
53 | |||
54 | /** |
||
55 | * Default sort order for search results. |
||
56 | * |
||
57 | * @since 5.8.0 |
||
58 | * @var string |
||
59 | */ |
||
60 | const DEFAULT_SORT = 'relevance_desc'; |
||
61 | |||
62 | /** |
||
63 | * Jetpack_Search_Widget constructor. |
||
64 | * |
||
65 | * @since 5.0.0 |
||
66 | */ |
||
67 | public function __construct( $name = null ) { |
||
101 | |||
102 | /** |
||
103 | * Check whether search is currently active |
||
104 | * |
||
105 | * @since 6.3 |
||
106 | */ |
||
107 | public function is_search_active() { |
||
110 | |||
111 | /** |
||
112 | * Activate search |
||
113 | * |
||
114 | * @since 6.3 |
||
115 | */ |
||
116 | public function activate_search() { |
||
119 | |||
120 | |||
121 | /** |
||
122 | * Enqueues the scripts and styles needed for the customizer. |
||
123 | * |
||
124 | * @since 5.7.0 |
||
125 | */ |
||
126 | public function widget_admin_setup() { |
||
171 | |||
172 | /** |
||
173 | * Enqueue scripts and styles for the frontend. |
||
174 | * |
||
175 | * @since 5.8.0 |
||
176 | */ |
||
177 | public function enqueue_frontend_scripts() { |
||
192 | |||
193 | /** |
||
194 | * Get the list of valid sort types/orders. |
||
195 | * |
||
196 | * @since 5.8.0 |
||
197 | * |
||
198 | * @return array The sort orders. |
||
199 | */ |
||
200 | private function get_sort_types() { |
||
207 | |||
208 | /** |
||
209 | * Callback for an array_filter() call in order to only get filters for the current widget. |
||
210 | * |
||
211 | * @see Jetpack_Search_Widget::widget() |
||
212 | * |
||
213 | * @since 5.7.0 |
||
214 | * |
||
215 | * @param array $item Filter item. |
||
216 | * |
||
217 | * @return bool Whether the current filter item is for the current widget. |
||
218 | */ |
||
219 | function is_for_current_widget( $item ) { |
||
222 | |||
223 | /** |
||
224 | * This method returns a boolean for whether the widget should show site-wide filters for the site. |
||
225 | * |
||
226 | * This is meant to provide backwards-compatibility for VIP, and other professional plan users, that manually |
||
227 | * configured filters via `Jetpack_Search::set_filters()`. |
||
228 | * |
||
229 | * @since 5.7.0 |
||
230 | * |
||
231 | * @return bool Whether the widget should display site-wide filters or not. |
||
232 | */ |
||
233 | public function should_display_sitewide_filters() { |
||
251 | |||
252 | public function jetpack_search_populate_defaults( $instance ) { |
||
266 | |||
267 | /** |
||
268 | * Responsible for rendering the widget on the frontend. |
||
269 | * |
||
270 | * @since 5.0.0 |
||
271 | * |
||
272 | * @param array $args Widgets args supplied by the theme. |
||
273 | * @param array $instance The current widget instance. |
||
274 | */ |
||
275 | public function widget( $args, $instance ) { |
||
276 | $instance = $this->jetpack_search_populate_defaults( $instance ); |
||
277 | |||
278 | if ( ( new Status() )->is_development_mode() ) { |
||
279 | echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
||
280 | ?><div id="<?php echo esc_attr( $this->id ); ?>-wrapper"> |
||
281 | <div class="jetpack-search-sort-wrapper"> |
||
282 | <label> |
||
283 | <?php esc_html_e( 'Jetpack Search not supported in Development Mode', 'jetpack' ); ?> |
||
284 | </label> |
||
285 | </div> |
||
286 | </div><?php |
||
287 | echo $args['after_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
||
288 | return; |
||
289 | } |
||
290 | |||
291 | if ( Jetpack_Search_Options::is_instant_enabled() ) { |
||
292 | if ( 'jetpack-instant-search-sidebar' === $args['id'] ) { |
||
293 | $this->widget_empty_instant( $args, $instance ); |
||
294 | } else { |
||
295 | $this->widget_instant( $args, $instance ); |
||
296 | } |
||
297 | } else { |
||
298 | $this->widget_non_instant( $args, $instance ); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Render the non-instant frontend widget. |
||
304 | * |
||
305 | * @since 8.3.0 |
||
306 | * |
||
307 | * @param array $args Widgets args supplied by the theme. |
||
308 | * @param array $instance The current widget instance. |
||
309 | */ |
||
310 | public function widget_non_instant( $args, $instance ) { |
||
311 | $display_filters = false; |
||
312 | |||
313 | if ( is_search() ) { |
||
314 | if ( Jetpack_Search_Helpers::should_rerun_search_in_customizer_preview() ) { |
||
315 | Jetpack_Search::instance()->update_search_results_aggregations(); |
||
316 | } |
||
317 | |||
318 | $filters = Jetpack_Search::instance()->get_filters(); |
||
319 | |||
320 | View Code Duplication | if ( ! Jetpack_Search_Helpers::are_filters_by_widget_disabled() && ! $this->should_display_sitewide_filters() ) { |
|
321 | $filters = array_filter( $filters, array( $this, 'is_for_current_widget' ) ); |
||
322 | } |
||
323 | |||
324 | if ( ! empty( $filters ) ) { |
||
325 | $display_filters = true; |
||
326 | } |
||
327 | } |
||
328 | |||
329 | if ( ! $display_filters && empty( $instance['search_box_enabled'] ) && empty( $instance['user_sort_enabled'] ) ) { |
||
330 | return; |
||
331 | } |
||
332 | |||
333 | $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; |
||
334 | |||
335 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
336 | $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
||
337 | |||
338 | echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
||
339 | ?> |
||
340 | <div id="<?php echo esc_attr( $this->id ); ?>-wrapper" > |
||
341 | <?php |
||
342 | |||
343 | if ( ! empty( $title ) ) { |
||
344 | /** |
||
345 | * Responsible for displaying the title of the Jetpack Search filters widget. |
||
346 | * |
||
347 | * @module search |
||
348 | * |
||
349 | * @since 5.7.0 |
||
350 | * |
||
351 | * @param string $title The widget's title |
||
352 | * @param string $args['before_title'] The HTML tag to display before the title |
||
353 | * @param string $args['after_title'] The HTML tag to display after the title |
||
354 | */ |
||
355 | do_action( 'jetpack_search_render_filters_widget_title', $title, $args['before_title'], $args['after_title'] ); |
||
356 | } |
||
357 | |||
358 | $default_sort = isset( $instance['sort'] ) ? $instance['sort'] : self::DEFAULT_SORT; |
||
359 | list( $orderby, $order ) = $this->sorting_to_wp_query_param( $default_sort ); |
||
360 | $current_sort = "{$orderby}|{$order}"; |
||
361 | |||
362 | // we need to dynamically inject the sort field into the search box when the search box is enabled, and display |
||
363 | // it separately when it's not. |
||
364 | if ( ! empty( $instance['search_box_enabled'] ) ) { |
||
365 | Jetpack_Search_Template_Tags::render_widget_search_form( $instance['post_types'], $orderby, $order ); |
||
366 | } |
||
367 | |||
368 | if ( ! empty( $instance['search_box_enabled'] ) && ! empty( $instance['user_sort_enabled'] ) ) : |
||
369 | ?> |
||
370 | <div class="jetpack-search-sort-wrapper"> |
||
371 | <label> |
||
372 | <?php esc_html_e( 'Sort by', 'jetpack' ); ?> |
||
373 | <select class="jetpack-search-sort"> |
||
374 | View Code Duplication | <?php foreach ( $this->get_sort_types() as $sort => $label ) { ?> |
|
375 | <option value="<?php echo esc_attr( $sort ); ?>" <?php selected( $current_sort, $sort ); ?>> |
||
376 | <?php echo esc_html( $label ); ?> |
||
377 | </option> |
||
378 | <?php } ?> |
||
379 | </select> |
||
380 | </label> |
||
381 | </div> |
||
382 | <?php |
||
383 | endif; |
||
384 | |||
385 | if ( $display_filters ) { |
||
386 | /** |
||
387 | * Responsible for rendering filters to narrow down search results. |
||
388 | * |
||
389 | * @module search |
||
390 | * |
||
391 | * @since 5.8.0 |
||
392 | * |
||
393 | * @param array $filters The possible filters for the current query. |
||
394 | * @param array $post_types An array of post types to limit filtering to. |
||
395 | */ |
||
396 | do_action( |
||
397 | 'jetpack_search_render_filters', |
||
398 | $filters, |
||
|
|||
399 | isset( $instance['post_types'] ) ? $instance['post_types'] : null |
||
400 | ); |
||
401 | } |
||
402 | |||
403 | $this->maybe_render_sort_javascript( $instance, $order, $orderby ); |
||
404 | |||
405 | echo '</div>'; |
||
406 | echo $args['after_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Render the instant frontend widget. |
||
411 | * |
||
412 | * @since 8.3.0 |
||
413 | * |
||
414 | * @param array $args Widgets args supplied by the theme. |
||
415 | * @param array $instance The current widget instance. |
||
416 | */ |
||
417 | public function widget_instant( $args, $instance ) { |
||
489 | |||
490 | /** |
||
491 | * Render the instant widget for the overlay. |
||
492 | * |
||
493 | * @since 8.3.0 |
||
494 | * |
||
495 | * @param array $args Widgets args supplied by the theme. |
||
496 | * @param array $instance The current widget instance. |
||
497 | */ |
||
498 | public function widget_empty_instant( $args, $instance ) { |
||
531 | |||
532 | |||
533 | /** |
||
534 | * Renders JavaScript for the sorting controls on the frontend. |
||
535 | * |
||
536 | * This JS is a bit complicated, but here's what it's trying to do: |
||
537 | * - find the search form |
||
538 | * - find the orderby/order fields and set default values |
||
539 | * - detect changes to the sort field, if it exists, and use it to set the order field values |
||
540 | * |
||
541 | * @since 5.8.0 |
||
542 | * |
||
543 | * @param array $instance The current widget instance. |
||
544 | * @param string $order The order to initialize the select with. |
||
545 | * @param string $orderby The orderby to initialize the select with. |
||
546 | */ |
||
547 | private function maybe_render_sort_javascript( $instance, $order, $orderby ) { |
||
592 | |||
593 | /** |
||
594 | * Convert a sort string into the separate order by and order parts. |
||
595 | * |
||
596 | * @since 5.8.0 |
||
597 | * |
||
598 | * @param string $sort A sort string. |
||
599 | * |
||
600 | * @return array Order by and order. |
||
601 | */ |
||
602 | private function sorting_to_wp_query_param( $sort ) { |
||
614 | |||
615 | /** |
||
616 | * Updates a particular instance of the widget. Validates and sanitizes the options. |
||
617 | * |
||
618 | * @since 5.0.0 |
||
619 | * |
||
620 | * @param array $new_instance New settings for this instance as input by the user via Jetpack_Search_Widget::form(). |
||
621 | * @param array $old_instance Old settings for this instance. |
||
622 | * |
||
623 | * @return array Settings to save. |
||
624 | */ |
||
625 | public function update( $new_instance, $old_instance ) { |
||
678 | |||
679 | /** |
||
680 | * Outputs the settings update form. |
||
681 | * |
||
682 | * @since 5.0.0 |
||
683 | * |
||
684 | * @param array $instance Current settings. |
||
685 | */ |
||
686 | public function form( $instance ) { |
||
798 | |||
799 | /** |
||
800 | * We need to render HTML in two formats: an Underscore template (client-side) |
||
801 | * and native PHP (server-side). This helper function allows for easy rendering |
||
802 | * of attributes in both formats. |
||
803 | * |
||
804 | * @since 5.8.0 |
||
805 | * |
||
806 | * @param string $name Attribute name. |
||
807 | * @param string $value Attribute value. |
||
808 | * @param bool $is_template Whether this is for an Underscore template or not. |
||
809 | */ |
||
810 | private function render_widget_attr( $name, $value, $is_template ) { |
||
813 | |||
814 | /** |
||
815 | * We need to render HTML in two formats: an Underscore template (client-size) |
||
816 | * and native PHP (server-side). This helper function allows for easy rendering |
||
817 | * of the "selected" attribute in both formats. |
||
818 | * |
||
819 | * @since 5.8.0 |
||
820 | * |
||
821 | * @param string $name Attribute name. |
||
822 | * @param string $value Attribute value. |
||
823 | * @param string $compare Value to compare to the attribute value to decide if it should be selected. |
||
824 | * @param bool $is_template Whether this is for an Underscore template or not. |
||
825 | */ |
||
826 | private function render_widget_option_selected( $name, $value, $compare, $is_template ) { |
||
830 | |||
831 | /** |
||
832 | * Responsible for rendering a single filter in the customizer or the widget administration screen in wp-admin. |
||
833 | * |
||
834 | * We use this method for two purposes - rendering the fields server-side, and also rendering a script template for Underscore. |
||
835 | * |
||
836 | * @since 5.7.0 |
||
837 | * |
||
838 | * @param array $filter The filter to render. |
||
839 | * @param bool $is_template Whether this is for an Underscore template or not. |
||
840 | */ |
||
841 | public function render_widget_edit_filter( $filter, $is_template = false ) { |
||
971 | } |
||
972 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: