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 ) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * If a gallery is detected, then get all the images from it. |
||
| 92 | */ |
||
| 93 | static function from_gallery( $post_id ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get attachment images for a specified post and return them. Also make sure |
||
| 164 | * their dimensions are at or above a required minimum. |
||
| 165 | */ |
||
| 166 | static function from_attachment( $post_id, $width = 200, $height = 200 ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Check if a Featured Image is set for this post, and return it in a similar |
||
| 225 | * format to the other images?_from_*() methods. |
||
| 226 | * @param int $post_id The post ID to check |
||
| 227 | * @return Array containing details of the Featured Image, or empty array if none. |
||
| 228 | */ |
||
| 229 | static function from_thumbnail( $post_id, $width = 200, $height = 200 ) { |
||
| 230 | $images = array(); |
||
| 231 | |||
| 232 | $post = get_post( $post_id ); |
||
| 233 | |||
| 234 | if ( ! empty( $post->post_password ) ) { |
||
| 235 | return $images; |
||
| 236 | } |
||
| 237 | |||
| 238 | if ( 'attachment' === get_post_type( $post ) && wp_attachment_is_image( $post ) ) { |
||
| 239 | $thumb = $post_id; |
||
| 240 | } else { |
||
| 241 | $thumb = get_post_thumbnail_id( $post ); |
||
| 242 | } |
||
| 243 | |||
| 244 | if ( $thumb ) { |
||
| 245 | $meta = wp_get_attachment_metadata( $thumb ); |
||
| 246 | // Must be larger than requested minimums |
||
| 247 | if ( !isset( $meta['width'] ) || $meta['width'] < $width ) |
||
| 248 | return $images; |
||
| 249 | if ( !isset( $meta['height'] ) || $meta['height'] < $height ) |
||
| 250 | return $images; |
||
| 251 | |||
| 252 | $too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > 1200 ) || ( ! empty( $meta['height'] ) && $meta['height'] > 1200 ) ); |
||
| 253 | |||
| 254 | if ( |
||
| 255 | $too_big && |
||
| 256 | ( |
||
| 257 | ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) || |
||
| 258 | ( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
||
| 259 | ) |
||
| 260 | ) { |
||
| 261 | $img_src = wp_get_attachment_image_src( $thumb, array( 1200, 1200 ) ); |
||
| 262 | } else { |
||
| 263 | $img_src = wp_get_attachment_image_src( $thumb, 'full' ); |
||
| 264 | } |
||
| 265 | if ( ! is_array( $img_src ) ) { |
||
| 266 | // If wp_get_attachment_image_src returns false but we know that there should be an image that could be used. |
||
| 267 | // we try a bit harder and user the data that we have. |
||
| 268 | $thumb_post_data = get_post( $thumb ); |
||
| 269 | $img_src = array( $thumb_post_data->guid, $meta['width'], $meta['height'] ); |
||
| 270 | } |
||
| 271 | |||
| 272 | $url = $img_src[0]; |
||
| 273 | $images = array( array( // Other methods below all return an array of arrays |
||
| 274 | 'type' => 'image', |
||
| 275 | 'from' => 'thumbnail', |
||
| 276 | 'src' => $url, |
||
| 277 | 'src_width' => $img_src[1], |
||
| 278 | 'src_height' => $img_src[2], |
||
| 279 | 'href' => get_permalink( $thumb ), |
||
| 280 | 'alt_text' => self::get_alt_text( $thumb ), |
||
| 281 | ) ); |
||
| 282 | |||
| 283 | } |
||
| 284 | |||
| 285 | if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
||
| 286 | $meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true ); |
||
| 287 | if ( ! empty( $meta_thumbnail ) ) { |
||
| 288 | if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) { |
||
| 289 | return $images; |
||
| 290 | } |
||
| 291 | |||
| 292 | if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) { |
||
| 293 | return $images; |
||
| 294 | } |
||
| 295 | |||
| 296 | $images = array( array( // Other methods below all return an array of arrays |
||
| 297 | 'type' => 'image', |
||
| 298 | 'from' => 'thumbnail', |
||
| 299 | 'src' => $meta_thumbnail['URL'], |
||
| 300 | 'src_width' => $meta_thumbnail['width'], |
||
| 301 | 'src_height' => $meta_thumbnail['height'], |
||
| 302 | 'href' => $meta_thumbnail['URL'], |
||
| 303 | 'alt_text' => self::get_alt_text( $thumb ), |
||
| 304 | ) ); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | return $images; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get images from Gutenberg Image blocks. |
||
| 313 | * |
||
| 314 | * @since 6.9.0 |
||
| 315 | * |
||
| 316 | * @param mixed $html_or_id The HTML string to parse for images, or a post id. |
||
| 317 | * @param int $width Minimum Image width. |
||
| 318 | * @param int $height Minimum Image height. |
||
| 319 | */ |
||
| 320 | public static function from_blocks( $html_or_id, $width = 200, $height = 200 ) { |
||
| 321 | $images = array(); |
||
| 322 | |||
| 323 | $html_info = self::get_post_html( $html_or_id ); |
||
| 324 | |||
| 325 | if ( empty( $html_info['html'] ) ) { |
||
| 326 | return $images; |
||
| 327 | } |
||
| 328 | |||
| 329 | // Look for block information in the HTML. |
||
| 330 | /* |
||
| 331 | if ( ! function_exists( 'parse_blocks' ) ) { |
||
| 332 | return $images; |
||
| 333 | } |
||
| 334 | */ |
||
| 335 | $blocks = parse_blocks( $html_info['html'] ); |
||
| 336 | if ( empty( $blocks ) ) { |
||
| 337 | return $images; |
||
| 338 | } |
||
| 339 | |||
| 340 | foreach ( $blocks as $block ) { |
||
| 341 | /** |
||
| 342 | * Parse content from Core Image blocks. |
||
| 343 | * If it is an image block for an image hosted on our site, it will have an ID. |
||
| 344 | * If it does not have an ID, let `from_html` parse that content later, |
||
| 345 | * and extract an image if it has size parameters. |
||
| 346 | */ |
||
| 347 | if ( |
||
| 348 | 'core/image' === $block['blockName'] |
||
| 349 | && ! empty( $block['attrs']['id'] ) |
||
| 350 | ) { |
||
| 351 | $images[] = self::get_attachment_data( $block['attrs']['id'], $html_info['post_url'], $width, $height ); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Parse content from Core Gallery blocks and Jetpack's Tiled Gallery blocks. |
||
| 356 | * Gallery blocks include the ID of each one of the images in the gallery. |
||
| 357 | */ |
||
| 358 | if ( |
||
| 359 | ( 'core/gallery' === $block['blockName'] || 'jetpack/tiled-gallery' === $block['blockName'] ) |
||
| 360 | && ! empty( $block['attrs']['ids'] ) |
||
| 361 | ) { |
||
| 362 | foreach ( $block['attrs']['ids'] as $img_id ) { |
||
| 363 | $images[] = self::get_attachment_data( $img_id, $html_info['post_url'], $width, $height ); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Returning a filtered array because get_attachment_data returns false |
||
| 370 | * for unsuccessful attempts. |
||
| 371 | */ |
||
| 372 | return array_filter( $images ); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Very raw -- just parse the HTML and pull out any/all img tags and return their src |
||
| 377 | * |
||
| 378 | * @param mixed $html_or_id The HTML string to parse for images, or a post id. |
||
| 379 | * @param int $width Minimum Image width. |
||
| 380 | * @param int $height Minimum Image height. |
||
| 381 | * |
||
| 382 | * @uses DOMDocument |
||
| 383 | * |
||
| 384 | * @return Array containing images |
||
| 385 | */ |
||
| 386 | static function from_html( $html_or_id, $width = 200, $height = 200 ) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param int $post_id The post ID to check |
||
| 470 | * @param int $size |
||
| 471 | * @return Array containing details of the image, or empty array if none. |
||
| 472 | */ |
||
| 473 | static function from_blavatar( $post_id, $size = 96 ) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Gets a post image from the author avatar. |
||
| 505 | * |
||
| 506 | * @param int $post_id The post ID to check. |
||
| 507 | * @param int $size The size of the avatar to get. |
||
| 508 | * @param string $default The default image to use. |
||
| 509 | * @return Array containing details of the image, or empty array if none. |
||
| 510 | */ |
||
| 511 | static function from_gravatar( $post_id, $size = 96, $default = false ) { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Run through the different methods that we have available to try to find a single good |
||
| 542 | * display image for this post. |
||
| 543 | * @param int $post_id |
||
| 544 | * @param array $args Other arguments (currently width and height required for images where possible to determine) |
||
| 545 | * @return Array containing details of the best image to be used |
||
| 546 | */ |
||
| 547 | static function get_image( $post_id, $args = array() ) { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Get an array containing a collection of possible images for this post, stopping once we hit a method |
||
| 584 | * that returns something useful. |
||
| 585 | * @param int $post_id |
||
| 586 | * @param array $args Optional args, see defaults list for details |
||
| 587 | * @return Array containing images that would be good for representing this post |
||
| 588 | */ |
||
| 589 | static function get_images( $post_id, $args = array() ) { |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Takes an image URL and pixel dimensions then returns a URL for the |
||
| 669 | * resized and cropped image. |
||
| 670 | * |
||
| 671 | * @param string $src |
||
| 672 | * @param int $dimension |
||
| 673 | * @return string Transformed image URL |
||
| 674 | */ |
||
| 675 | static function fit_image_url( $src, $width, $height ) { |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Get HTML from given post content. |
||
| 714 | * |
||
| 715 | * @since 6.9.0 |
||
| 716 | * |
||
| 717 | * @param mixed $html_or_id The HTML string to parse for images, or a post id. |
||
| 718 | * |
||
| 719 | * @return array $html_info { |
||
| 720 | * @type string $html Post content. |
||
| 721 | * @type string $post_url Post URL. |
||
| 722 | * } |
||
| 723 | */ |
||
| 724 | static function get_post_html( $html_or_id ) { |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Get info about a WordPress attachment. |
||
| 747 | * |
||
| 748 | * @since 6.9.0 |
||
| 749 | * |
||
| 750 | * @param int $attachment_id Attachment ID. |
||
| 751 | * @param string $post_url URL of the post, if we have one. |
||
| 752 | * @param int $width Minimum Image width. |
||
| 753 | * @param int $height Minimum Image height. |
||
| 754 | * @return array|bool Image data or false if unavailable. |
||
| 755 | */ |
||
| 756 | public static function get_attachment_data( $attachment_id, $post_url = '', $width, $height ) { |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Get the alt text for an image or other media from the Media Library. |
||
| 786 | * |
||
| 787 | * @since 7.1 |
||
| 788 | * |
||
| 789 | * @param int $attachment_id The Post ID of the media. |
||
| 790 | * @return string The alt text value or an emptry string. |
||
| 791 | */ |
||
| 792 | public static function get_alt_text( $attachment_id ) { |
||
| 795 | } |
||
| 796 |
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.