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_PostImages 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_PostImages, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Jetpack_PostImages { |
||
| 14 | /** |
||
| 15 | * If a slideshow is embedded within a post, then parse out the images involved and return them |
||
| 16 | */ |
||
| 17 | static function from_slideshow( $post_id, $width = 200, $height = 200 ) { |
||
| 18 | $images = array(); |
||
| 19 | |||
| 20 | $post = get_post( $post_id ); |
||
| 21 | |||
| 22 | if ( ! $post ) { |
||
| 23 | return $images; |
||
| 24 | } |
||
| 25 | |||
| 26 | if ( ! empty( $post->post_password ) ) { |
||
| 27 | return $images; |
||
| 28 | } |
||
| 29 | |||
| 30 | if ( false === has_shortcode( $post->post_content, 'slideshow' ) ) { |
||
| 31 | return $images; // no slideshow - bail |
||
| 32 | } |
||
| 33 | |||
| 34 | $permalink = get_permalink( $post->ID ); |
||
| 35 | |||
| 36 | // Mechanic: Somebody set us up the bomb |
||
| 37 | $old_post = $GLOBALS['post']; |
||
| 38 | $GLOBALS['post'] = $post; |
||
| 39 | $old_shortcodes = $GLOBALS['shortcode_tags']; |
||
| 40 | $GLOBALS['shortcode_tags'] = array( 'slideshow' => $old_shortcodes['slideshow'] ); |
||
| 41 | |||
| 42 | // Find all the slideshows |
||
| 43 | preg_match_all( '/' . get_shortcode_regex() . '/sx', $post->post_content, $slideshow_matches, PREG_SET_ORDER ); |
||
| 44 | |||
| 45 | ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that |
||
| 46 | |||
| 47 | foreach ( $slideshow_matches as $slideshow_match ) { |
||
|
|
|||
| 48 | $slideshow = do_shortcode_tag( $slideshow_match ); |
||
| 49 | if ( false === $pos = stripos( $slideshow, 'jetpack-slideshow' ) ) // must be something wrong - or we changed the output format in which case none of the following will work |
||
| 50 | continue; |
||
| 51 | $start = strpos( $slideshow, '[', $pos ); |
||
| 52 | $end = strpos( $slideshow, ']', $start ); |
||
| 53 | $post_images = json_decode( wp_specialchars_decode( str_replace( "'", '"', substr( $slideshow, $start, $end - $start + 1 ) ), ENT_QUOTES ) ); // parse via JSON |
||
| 54 | foreach ( $post_images as $post_image ) { |
||
| 55 | if ( !$post_image_id = absint( $post_image->id ) ) |
||
| 56 | continue; |
||
| 57 | |||
| 58 | $meta = wp_get_attachment_metadata( $post_image_id ); |
||
| 59 | |||
| 60 | // Must be larger than 200x200 (or user-specified) |
||
| 61 | if ( !isset( $meta['width'] ) || $meta['width'] < $width ) |
||
| 62 | continue; |
||
| 63 | if ( !isset( $meta['height'] ) || $meta['height'] < $height ) |
||
| 64 | continue; |
||
| 65 | |||
| 66 | $url = wp_get_attachment_url( $post_image_id ); |
||
| 67 | |||
| 68 | $images[] = array( |
||
| 69 | 'type' => 'image', |
||
| 70 | 'from' => 'slideshow', |
||
| 71 | 'src' => $url, |
||
| 72 | 'src_width' => $meta['width'], |
||
| 73 | 'src_height' => $meta['height'], |
||
| 74 | 'href' => $permalink, |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | ob_end_clean(); |
||
| 79 | |||
| 80 | // Operator: Main screen turn on |
||
| 81 | $GLOBALS['shortcode_tags'] = $old_shortcodes; |
||
| 82 | $GLOBALS['post'] = $old_post; |
||
| 83 | |||
| 84 | return $images; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * If a gallery is detected, then get all the images from it. |
||
| 89 | */ |
||
| 90 | static function from_gallery( $post_id ) { |
||
| 91 | $images = array(); |
||
| 92 | |||
| 93 | $post = get_post( $post_id ); |
||
| 94 | |||
| 95 | if ( ! $post ) { |
||
| 96 | return $images; |
||
| 97 | } |
||
| 98 | |||
| 99 | if ( ! empty( $post->post_password ) ) { |
||
| 100 | return $images; |
||
| 101 | } |
||
| 102 | |||
| 103 | $permalink = get_permalink( $post->ID ); |
||
| 104 | |||
| 105 | $galleries = get_post_galleries( $post->ID, false ); |
||
| 106 | |||
| 107 | foreach ( $galleries as $gallery ) { |
||
| 108 | if ( isset( $gallery['type'] ) && 'slideshow' === $gallery['type'] && ! empty( $gallery['ids'] ) ) { |
||
| 109 | $image_ids = explode( ',', $gallery['ids'] ); |
||
| 110 | $image_size = isset( $gallery['size'] ) ? $gallery['size'] : 'thumbnail'; |
||
| 111 | foreach ( $image_ids as $image_id ) { |
||
| 112 | $image = wp_get_attachment_image_src( $image_id, $image_size ); |
||
| 113 | View Code Duplication | if ( ! empty( $image[0] ) ) { |
|
| 114 | list( $raw_src ) = explode( '?', $image[0] ); // pull off any Query string (?w=250) |
||
| 115 | $raw_src = wp_specialchars_decode( $raw_src ); // rawify it |
||
| 116 | $raw_src = esc_url_raw( $raw_src ); // clean it |
||
| 117 | $images[] = array( |
||
| 118 | 'type' => 'image', |
||
| 119 | 'from' => 'gallery', |
||
| 120 | 'src' => $raw_src, |
||
| 121 | 'href' => $permalink, |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | View Code Duplication | } elseif ( ! empty( $gallery['src'] ) ) { |
|
| 126 | foreach ( $gallery['src'] as $src ) { |
||
| 127 | list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250) |
||
| 128 | $raw_src = wp_specialchars_decode( $raw_src ); // rawify it |
||
| 129 | $raw_src = esc_url_raw( $raw_src ); // clean it |
||
| 130 | $images[] = array( |
||
| 131 | 'type' => 'image', |
||
| 132 | 'from' => 'gallery', |
||
| 133 | 'src' => $raw_src, |
||
| 134 | 'href' => $permalink, |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | return $images; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get attachment images for a specified post and return them. Also make sure |
||
| 145 | * their dimensions are at or above a required minimum. |
||
| 146 | */ |
||
| 147 | static function from_attachment( $post_id, $width = 200, $height = 200 ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Check if a Featured Image is set for this post, and return it in a similar |
||
| 218 | * format to the other images?_from_*() methods. |
||
| 219 | * @param int $post_id The post ID to check |
||
| 220 | * @return Array containing details of the Featured Image, or empty array if none. |
||
| 221 | */ |
||
| 222 | static function from_thumbnail( $post_id, $width = 200, $height = 200 ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Very raw -- just parse the HTML and pull out any/all img tags and return their src |
||
| 299 | * @param mixed $html_or_id The HTML string to parse for images, or a post id |
||
| 300 | * @return Array containing images |
||
| 301 | */ |
||
| 302 | static function from_html( $html_or_id ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param int $post_id The post ID to check |
||
| 341 | * @param int $size |
||
| 342 | * @return Array containing details of the image, or empty array if none. |
||
| 343 | */ |
||
| 344 | static function from_blavatar( $post_id, $size = 96 ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param int $post_id The post ID to check |
||
| 374 | * @param int $size |
||
| 375 | * @param string $default The default image to use. |
||
| 376 | * @return Array containing details of the image, or empty array if none. |
||
| 377 | */ |
||
| 378 | static function from_gravatar( $post_id, $size = 96, $default = false ) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Run through the different methods that we have available to try to find a single good |
||
| 420 | * display image for this post. |
||
| 421 | * @param int $post_id |
||
| 422 | * @param array $args Other arguments (currently width and height required for images where possible to determine) |
||
| 423 | * @return Array containing details of the best image to be used |
||
| 424 | */ |
||
| 425 | static function get_image( $post_id, $args = array() ) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get an array containing a collection of possible images for this post, stopping once we hit a method |
||
| 462 | * that returns something useful. |
||
| 463 | * @param int $post_id |
||
| 464 | * @param array $args Optional args, see defaults list for details |
||
| 465 | * @return Array containing images that would be good for representing this post |
||
| 466 | */ |
||
| 467 | static function get_images( $post_id, $args = array() ) { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Takes an image URL and pixel dimensions then returns a URL for the |
||
| 540 | * resized and croped image. |
||
| 541 | * |
||
| 542 | * @param string $src |
||
| 543 | * @param int $dimension |
||
| 544 | * @return string Transformed image URL |
||
| 545 | */ |
||
| 546 | static function fit_image_url( $src, $width, $height ) { |
||
| 583 | } |
||
| 584 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.