Completed
Pull Request — develop (#1260)
by Naveen
03:02
created

Srcset_Util   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 11.54 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 6
loc 52
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_srcset() 6 29 5
A get_image_width() 0 12 4

How to fix   Duplicated Code   

Duplicated Code

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:

1
<?php
2
3
namespace Wordlift\Widgets;
4
/**
5
 * @since 3.28.0
6
 * @author Naveen Muthusamy <[email protected]>
7
 */
8
class Srcset_Util {
9
10
	const NAVIGATOR_WIDGET = 'navigator';
11
12
	const FACETED_SEARCH_WIDGET = 'faceted_search';
13
14
	public static function get_srcset( $post_id, $widget_name ) {
15
16
		$srcset = array();
17
		$medium = get_the_post_thumbnail_url( $post_id, 'medium' );
18
		$large  = get_the_post_thumbnail_url( $post_id, 'large' );
19
20 View Code Duplication
		if ( $medium && $width = self::get_image_width( $post_id, 'medium' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
			$srcset[] = $medium . ' ' . $width . 'w';
22
		}
23
24 View Code Duplication
		if ( $large && $width = self::get_image_width( $post_id, 'large' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
			$srcset[] = $large . ' ' . $width . 'w';
26
		}
27
28
		$srcset_string = join( ",", $srcset );
29
30
		/**
31
		 * Filter name: wordlift_${widget_name}_thumbnail_srcset
32
		 * Filters the srcset string supplied to widgets for each post.
33
		 *
34
		 * @param $srcset_string string The srcset string
35
		 *
36
		 * @since 3.28.0
37
		 */
38
		$srcset_string = apply_filters( "wordlift_${widget_name}_thumbnail_srcset", $srcset_string );
39
40
		return $srcset_string;
41
42
	}
43
44
45
	private static function get_image_width( $post_id, $size ) {
46
		$thumbnail_id = get_post_thumbnail_id( $post_id );
47
		if ( ! $thumbnail_id ) {
48
			return false;
49
		}
50
		$data = wp_get_attachment_image_src( $thumbnail_id, $size );
51
		if ( ! $data ) {
52
			return false;
53
		}
54
55
		return array_key_exists( 2, $data ) ? $data[2] : false;
56
	}
57
58
59
}