Completed
Push — branch-4.5 ( 34853b...52372f )
by
unknown
255:58 queued 248:41
created

WordAds_Sidebar_Widget   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 9.8 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 10
loc 102
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
C widget() 0 46 8
B form() 0 27 4
A update() 0 11 2
A __construct() 10 10 1

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
/**
4
 * Widget for inserting an ad into your sidebar
5
 *
6
 * @since 4.5.0
7
 */
8
class WordAds_Sidebar_Widget extends WP_Widget {
9
10
	private static $allowed_tags = array( 'mrec', 'wideskyscraper' );
11
12 View Code Duplication
	function __construct() {
13
		parent::__construct(
14
			'wordads_sidebar_widget',
15
			apply_filters( 'jetpack_widget_name', 'Ads' ),
16
			array(
17
				'description' => __( 'Insert a WordAd wherever you can place a widget.', 'jetpack' ),
18
				'customize_selective_refresh' => true
19
			)
20
		);
21
	}
22
23
	public function widget( $args, $instance ) {
24
		global $wordads;
25
		if ( $wordads->should_bail() ) {
26
			return false;
27
		}
28
29
		$about = __( 'About these ads', 'jetpack' );
30
		$width = WordAds::$ad_tag_ids[$instance['unit']]['width'];
31
		$height = WordAds::$ad_tag_ids[$instance['unit']]['height'];
32
		$snippet = '';
0 ignored issues
show
Unused Code introduced by
$snippet is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
		if ( $wordads->option( 'wordads_house', true ) ) {
34
			$ad_url = 'https://s0.wp.com/wp-content/blog-plugins/wordads/house/';
35
			if ( 'leaderboard' == $instance['unit'] && ! $this->params->mobile_device ) {
36
				$ad_url .= 'leaderboard.png';
37
			} else if ( 'wideskyscraper' == $instance['unit'] ) {
38
				$ad_url .= 'widesky.png';
39
			} else {
40
				$ad_url .= 'mrec.png';
41
			}
42
43
			$snippet = <<<HTML
44
			<a href="https://wordpress.com/create/" target="_blank">
45
				<img src="$ad_url" alt="WordPress.com: Grow Your Business" width="$width" height="$height" />
46
			</a>
47
HTML;
48
		} else {
49
			$section_id = 0 === $wordads->params->blog_id ? WORDADS_API_TEST_ID : $wordads->params->blog_id . '3';
50
			$data_tags = ( $wordads->params->cloudflare ) ? ' data-cfasync="false"' : '';
51
			$snippet = <<<HTML
52
			<script$data_tags type='text/javascript'>
53
				(function(g){g.__ATA.initAd({sectionId:$section_id, width:$width, height:$height});})(window);
54
			</script>
55
HTML;
56
		}
57
58
		echo <<< HTML
59
		<div class="wpcnt">
60
			<div class="wpa">
61
				<a class="wpa-about" href="https://en.wordpress.com/about-these-ads/" rel="nofollow">$about</a>
62
				<div class="u {$instance['unit']}">
63
					$snippet
64
				</div>
65
			</div>
66
		</div>
67
HTML;
68
	}
69
70
	public function form( $instance ) {
71
		// ad unit type
72
		if ( isset( $instance['unit'] ) ) {
73
			$unit = $instance['unit'];
74
		} else {
75
			$unit = 'mrec';
76
		}
77
		?>
78
		<p>
79
			<label for="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>"><?php _e( 'Tag Dimensions:', 'jetpack' ); ?></label>
80
			<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'unit' ) ); ?>">
81
		<?php
82
		foreach ( WordAds::$ad_tag_ids as $ad_unit => $properties ) {
83
				if ( ! in_array( $ad_unit, self::$allowed_tags ) ) {
84
					continue;
85
				}
86
87
				$splits = explode( '_', $properties['tag'] );
88
				$unit_pretty = "{$splits[0]} {$splits[1]}";
89
				$selected = selected( $ad_unit, $unit, false );
90
				echo "<option value='", esc_attr( $ad_unit ) ,"' ", $selected, '>', esc_html( $unit_pretty ) , '</option>';
91
			}
92
		?>
93
			</select>
94
		</p>
95
		<?php
96
	}
97
98
	public function update( $new_instance, $old_instance ) {
99
		$instance = $old_instance;
100
101
		if ( in_array( $new_instance['unit'], self::$allowed_tags ) ) {
102
			$instance['unit'] = $new_instance['unit'];
103
		} else {
104
			$instance['unit'] = 'mrec';
105
		}
106
107
		return $instance;
108
	}
109
}
110
111
add_action(
112
	'widgets_init',
113
	create_function(
0 ignored issues
show
Security Best Practice introduced by
The use of create_function is highly discouraged, better use a closure.

create_function can pose a great security vulnerability as it is similar to eval, and could be used for arbitrary code execution. We highly recommend to use a closure instead.

// Instead of
$function = create_function('$a, $b', 'return $a + $b');

// Better use
$function = function($a, $b) { return $a + $b; }
Loading history...
114
		'',
115
		'return register_widget( "WordAds_Sidebar_Widget" );'
116
	)
117
);
118