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_Display_Posts_Widget__Base 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_Display_Posts_Widget__Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class Jetpack_Display_Posts_Widget__Base extends WP_Widget { |
||
| 21 | /** |
||
| 22 | * @var string Remote service API URL prefix. |
||
| 23 | */ |
||
| 24 | public $service_url = 'https://public-api.wordpress.com/rest/v1.1/'; |
||
| 25 | |||
| 26 | public function __construct() { |
||
| 27 | parent::__construct( |
||
| 28 | // internal id |
||
| 29 | 'jetpack_display_posts_widget', |
||
| 30 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
| 31 | apply_filters( 'jetpack_widget_name', __( 'Display WordPress Posts', 'jetpack' ) ), |
||
| 32 | array( |
||
| 33 | 'description' => __( 'Displays a list of recent posts from another WordPress.com or Jetpack-enabled blog.', 'jetpack' ), |
||
| 34 | 'customize_selective_refresh' => true, |
||
| 35 | ) |
||
| 36 | ); |
||
| 37 | |||
| 38 | if ( is_customize_preview() ) { |
||
| 39 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Enqueue CSS and JavaScript. |
||
| 45 | * |
||
| 46 | * @since 4.0.0 |
||
| 47 | */ |
||
| 48 | public function enqueue_scripts() { |
||
| 49 | wp_enqueue_style( 'jetpack_display_posts_widget', plugins_url( 'style.css', __FILE__ ) ); |
||
| 50 | } |
||
| 51 | |||
| 52 | |||
| 53 | // DATA STORE: Must implement |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Gets blog data from the cache. |
||
| 57 | * |
||
| 58 | * @param string $site |
||
| 59 | * |
||
| 60 | * @return array|WP_Error |
||
| 61 | */ |
||
| 62 | abstract public function get_blog_data( $site ); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Update a widget instance. |
||
| 66 | * |
||
| 67 | * @param string $site The site to fetch the latest data for. |
||
| 68 | * |
||
| 69 | * @return array - the new data |
||
| 70 | */ |
||
| 71 | abstract public function update_instance( $site ); |
||
| 72 | |||
| 73 | |||
| 74 | // WIDGET API |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Set up the widget display on the front end. |
||
| 78 | * |
||
| 79 | * @param array $args |
||
| 80 | * @param array $instance |
||
| 81 | */ |
||
| 82 | public function widget( $args, $instance ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Display the widget administration form. |
||
| 192 | * |
||
| 193 | * @param array $instance Widget instance configuration. |
||
| 194 | * |
||
| 195 | * @return string|void |
||
| 196 | */ |
||
| 197 | public function form( $instance ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Initialize widget configuration variables. |
||
| 201 | */ |
||
| 202 | $title = ( isset( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts', 'jetpack' ); |
||
| 203 | $url = ( isset( $instance['url'] ) ) ? $instance['url'] : ''; |
||
| 204 | $number_of_posts = ( isset( $instance['number_of_posts'] ) ) ? $instance['number_of_posts'] : 5; |
||
| 205 | $open_in_new_window = ( isset( $instance['open_in_new_window'] ) ) ? $instance['open_in_new_window'] : false; |
||
| 206 | $featured_image = ( isset( $instance['featured_image'] ) ) ? $instance['featured_image'] : false; |
||
| 207 | $show_excerpts = ( isset( $instance['show_excerpts'] ) ) ? $instance['show_excerpts'] : false; |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * Check if the widget instance has errors available. |
||
| 212 | * |
||
| 213 | * Only do so if a URL is set. |
||
| 214 | */ |
||
| 215 | $update_errors = array(); |
||
| 216 | |||
| 217 | if ( ! empty( $url ) ) { |
||
| 218 | $data = $this->get_blog_data( $url ); |
||
| 219 | $update_errors = $this->extract_errors_from_blog_data( $data ); |
||
| 220 | } |
||
| 221 | |||
| 222 | ?> |
||
| 223 | <p> |
||
| 224 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'jetpack' ); ?></label> |
||
| 225 | <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
||
| 226 | </p> |
||
| 227 | |||
| 228 | <p> |
||
| 229 | <label for="<?php echo $this->get_field_id( 'url' ); ?>"><?php _e( 'Blog URL:', 'jetpack' ); ?></label> |
||
| 230 | <input class="widefat" id="<?php echo $this->get_field_id( 'url' ); ?>" name="<?php echo $this->get_field_name( 'url' ); ?>" type="text" value="<?php echo esc_attr( $url ); ?>" /> |
||
| 231 | <i> |
||
| 232 | <?php _e( "Enter a WordPress.com or Jetpack WordPress site URL.", 'jetpack' ); ?> |
||
| 233 | </i> |
||
| 234 | <?php |
||
| 235 | /** |
||
| 236 | * Show an error if the URL field was left empty. |
||
| 237 | * |
||
| 238 | * The error is shown only when the widget was already saved. |
||
| 239 | */ |
||
| 240 | if ( empty( $url ) && ! preg_match( '/__i__|%i%/', $this->id ) ) { |
||
| 241 | ?> |
||
| 242 | <br /> |
||
| 243 | <i class="error-message"><?php echo __( 'You must specify a valid blog URL!', 'jetpack' ); ?></i> |
||
| 244 | <?php |
||
| 245 | } |
||
| 246 | ?> |
||
| 247 | </p> |
||
| 248 | <p> |
||
| 249 | <label for="<?php echo $this->get_field_id( 'number_of_posts' ); ?>"><?php _e( 'Number of Posts to Display:', 'jetpack' ); ?></label> |
||
| 250 | <select name="<?php echo $this->get_field_name( 'number_of_posts' ); ?>"> |
||
| 251 | <?php |
||
| 252 | for ( $i = 1; $i <= 10; $i ++ ) { |
||
| 253 | echo '<option value="' . $i . '" ' . selected( $number_of_posts, $i ) . '>' . $i . '</option>'; |
||
| 254 | } |
||
| 255 | ?> |
||
| 256 | </select> |
||
| 257 | </p> |
||
| 258 | <p> |
||
| 259 | <label for="<?php echo $this->get_field_id( 'open_in_new_window' ); ?>"><?php _e( 'Open links in new window/tab:', 'jetpack' ); ?></label> |
||
| 260 | <input type="checkbox" name="<?php echo $this->get_field_name( 'open_in_new_window' ); ?>" <?php checked( $open_in_new_window, 1 ); ?> /> |
||
| 261 | </p> |
||
| 262 | <p> |
||
| 263 | <label for="<?php echo $this->get_field_id( 'featured_image' ); ?>"><?php _e( 'Show Featured Image:', 'jetpack' ); ?></label> |
||
| 264 | <input type="checkbox" name="<?php echo $this->get_field_name( 'featured_image' ); ?>" <?php checked( $featured_image, 1 ); ?> /> |
||
| 265 | </p> |
||
| 266 | <p> |
||
| 267 | <label for="<?php echo $this->get_field_id( 'show_excerpts' ); ?>"><?php _e( 'Show Excerpts:', 'jetpack' ); ?></label> |
||
| 268 | <input type="checkbox" name="<?php echo $this->get_field_name( 'show_excerpts' ); ?>" <?php checked( $show_excerpts, 1 ); ?> /> |
||
| 269 | </p> |
||
| 270 | |||
| 271 | <?php |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Show error messages. |
||
| 275 | */ |
||
| 276 | if ( ! empty( $update_errors['message'] ) ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Prepare the error messages. |
||
| 280 | */ |
||
| 281 | |||
| 282 | $where_message = ''; |
||
| 283 | switch ( $update_errors['where'] ) { |
||
| 284 | case 'posts': |
||
| 285 | $where_message .= __( 'An error occurred while downloading blog posts list', 'jetpack' ); |
||
| 286 | break; |
||
| 287 | |||
| 288 | /** |
||
| 289 | * If something else, beside `posts` and `site_info` broke, |
||
| 290 | * don't handle it and default to blog `information`, |
||
| 291 | * as it is generic enough. |
||
| 292 | */ |
||
| 293 | case 'site_info': |
||
| 294 | default: |
||
| 295 | $where_message .= __( 'An error occurred while downloading blog information', 'jetpack' ); |
||
| 296 | break; |
||
| 297 | } |
||
| 298 | |||
| 299 | ?> |
||
| 300 | <p class="error-message"> |
||
| 301 | <?php echo esc_html( $where_message ); ?>: |
||
| 302 | <br /> |
||
| 303 | <i> |
||
| 304 | <?php echo esc_html( $update_errors['message'] ); ?> |
||
| 305 | <?php |
||
| 306 | /** |
||
| 307 | * If there is any debug - show it here. |
||
| 308 | */ |
||
| 309 | if ( ! empty( $update_errors['debug'] ) ) { |
||
| 310 | ?> |
||
| 311 | <br /> |
||
| 312 | <br /> |
||
| 313 | <?php esc_html_e( 'Detailed information', 'jetpack' ); ?>: |
||
| 314 | <br /> |
||
| 315 | <?php echo esc_html( $update_errors['debug'] ); ?> |
||
| 316 | <?php |
||
| 317 | } |
||
| 318 | ?> |
||
| 319 | </i> |
||
| 320 | </p> |
||
| 321 | |||
| 322 | <?php |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | public function update( $new_instance, $old_instance ) { |
||
| 368 | |||
| 369 | |||
| 370 | // DATA PROCESSING |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Expiring transients have a name length maximum of 45 characters, |
||
| 374 | * so this function returns an abbreviated MD5 hash to use instead of |
||
| 375 | * the full URI. |
||
| 376 | * |
||
| 377 | * @param string $site Site to get the hash for. |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | public function get_site_hash( $site ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Fetch a remote service endpoint and parse it. |
||
| 387 | * |
||
| 388 | * Timeout is set to 15 seconds right now, because sometimes the WordPress API |
||
| 389 | * takes more than 5 seconds to fully respond. |
||
| 390 | * |
||
| 391 | * Caching is used here so we can avoid re-downloading the same endpoint |
||
| 392 | * in a single request. |
||
| 393 | * |
||
| 394 | * @param string $endpoint Parametrized endpoint to call. |
||
| 395 | * |
||
| 396 | * @param int $timeout How much time to wait for the API to respond before failing. |
||
| 397 | * |
||
| 398 | * @return array|WP_Error |
||
| 399 | */ |
||
| 400 | public function fetch_service_endpoint( $endpoint, $timeout = 15 ) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Parse data from service response. |
||
| 417 | * Do basic error handling for general service and data errors |
||
| 418 | * |
||
| 419 | * @param array $service_response Response from the service. |
||
| 420 | * |
||
| 421 | * @return array|WP_Error |
||
| 422 | */ |
||
| 423 | public function parse_service_response( $service_response ) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Fetch site information from the WordPress public API |
||
| 500 | * |
||
| 501 | * @param string $site URL of the site to fetch the information for. |
||
| 502 | * |
||
| 503 | * @return array|WP_Error |
||
| 504 | */ |
||
| 505 | public function fetch_site_info( $site ) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Parse external API response from the site info call and handle errors if they occur. |
||
| 514 | * |
||
| 515 | * @param array|WP_Error $service_response The raw response to be parsed. |
||
| 516 | * |
||
| 517 | * @return array|WP_Error |
||
| 518 | */ |
||
| 519 | View Code Duplication | public function parse_site_info_response( $service_response ) { |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Fetch list of posts from the WordPress public API. |
||
| 544 | * |
||
| 545 | * @param int $site_id The site to fetch the posts for. |
||
| 546 | * |
||
| 547 | * @return array|WP_Error |
||
| 548 | */ |
||
| 549 | public function fetch_posts_for_site( $site_id ) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Parse external API response from the posts list request and handle errors if any occur. |
||
| 575 | * |
||
| 576 | * @param object|WP_Error $service_response The raw response to be parsed. |
||
| 577 | * |
||
| 578 | * @return array|WP_Error |
||
| 579 | */ |
||
| 580 | View Code Duplication | public function parse_posts_response( $service_response ) { |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Format the posts for better storage. Drop all the data that is not used. |
||
| 609 | * |
||
| 610 | * @param object $parsed_data Array of posts returned by the APIs. |
||
| 611 | * |
||
| 612 | * @return array Formatted posts or an empty array if no posts were found. |
||
| 613 | */ |
||
| 614 | public function format_posts_for_storage( $parsed_data ) { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Fetch site information and posts list for a site. |
||
| 647 | * |
||
| 648 | * @param string $site Site to fetch the data for. |
||
| 649 | * @param array $original_data Optional original data to updated. |
||
| 650 | * |
||
| 651 | * @param bool $site_data_only Fetch only site information, skip posts list. |
||
| 652 | * |
||
| 653 | * @return array Updated or new data. |
||
| 654 | */ |
||
| 655 | public function fetch_blog_data( $site, $original_data = array(), $site_data_only = false ) { |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Scan and extract first error from blog data array. |
||
| 753 | * |
||
| 754 | * @param array|WP_Error $blog_data Blog data to scan for errors. |
||
| 755 | * |
||
| 756 | * @return string First error message found |
||
| 757 | */ |
||
| 758 | public function extract_errors_from_blog_data( $blog_data ) { |
||
| 829 | |||
| 830 | /** |
||
| 831 | * This is just to make method mocks in the unit tests easier. |
||
| 832 | * |
||
| 833 | * @param string $url The URL to fetch |
||
| 834 | * @param array $args Optional. Request arguments. |
||
| 835 | * |
||
| 836 | * @return array|WP_Error |
||
| 837 | * |
||
| 838 | * @codeCoverageIgnore |
||
| 839 | */ |
||
| 840 | public function wp_wp_remote_get( $url, $args = array() ) { |
||
| 843 | } |
||
| 844 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.