Completed
Push — whitelist/sync ( 830219...fbc430 )
by
unknown
13:02 queued 02:23
created

WordAds_Sidebar_Widget   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 97
Duplicated Lines 11.34 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

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

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
			/** This filter is documented in modules/widgets/facebook-likebox.php */
16
			apply_filters( 'jetpack_widget_name', 'Ads' ),
17
			array(
18
				'description' => __( 'Insert an ad unit wherever you can place a widget.', 'jetpack' ),
19
				'customize_selective_refresh' => true
20
			)
21
		);
22
	}
23
24
	public function widget( $args, $instance ) {
25
		global $wordads;
26
		if ( $wordads->should_bail() ) {
27
			return false;
28
		}
29
30
		if ( ! isset( $instance['unit'] ) ) {
31
			$instance['unit'] = 'mrec';
32
		}
33
34
		$about = __( 'Advertisements', 'jetpack' );
35
		$width = WordAds::$ad_tag_ids[$instance['unit']]['width'];
36
		$height = WordAds::$ad_tag_ids[$instance['unit']]['height'];
37
38
		$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...
39
		if ( $wordads->option( 'wordads_house', true ) ) {
40
			$unit = 'mrec';
41
			if ( 'leaderboard' == $instance['unit'] && ! $this->params->mobile_device ) {
42
				$unit = 'leaderboard';
43
			} else if ( 'wideskyscraper' == $instance['unit'] ) {
44
				$unit = 'widesky';
45
			}
46
47
			$snippet = $wordads->get_house_ad( $unit );
48
		} else {
49
			$section_id = 0 === $wordads->params->blog_id ? WORDADS_API_TEST_ID : $wordads->params->blog_id . '3';
50
			$snippet = $wordads->get_ad_snippet( $section_id, $height, $width );
51
		}
52
53
		echo <<< HTML
54
		<div class="wpcnt">
55
			<div class="wpa">
56
				<span class="wpa-about">$about</span>
57
				<div class="u {$instance['unit']}">
58
					$snippet
59
				</div>
60
			</div>
61
		</div>
62
HTML;
63
	}
64
65
	public function form( $instance ) {
66
		// ad unit type
67
		if ( isset( $instance['unit'] ) ) {
68
			$unit = $instance['unit'];
69
		} else {
70
			$unit = 'mrec';
71
		}
72
		?>
73
		<p>
74
			<label for="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>"><?php _e( 'Tag Dimensions:', 'jetpack' ); ?></label>
75
			<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'unit' ) ); ?>">
76
		<?php
77
		foreach ( WordAds::$ad_tag_ids as $ad_unit => $properties ) {
78
				if ( ! in_array( $ad_unit, self::$allowed_tags ) ) {
79
					continue;
80
				}
81
82
				$splits = explode( '_', $properties['tag'] );
83
				$unit_pretty = "{$splits[0]} {$splits[1]}";
84
				$selected = selected( $ad_unit, $unit, false );
85
				echo "<option value='", esc_attr( $ad_unit ) ,"' ", $selected, '>', esc_html( $unit_pretty ) , '</option>';
86
			}
87
		?>
88
			</select>
89
		</p>
90
		<?php
91
	}
92
93
	public function update( $new_instance, $old_instance ) {
94
		$instance = $old_instance;
95
96
		if ( in_array( $new_instance['unit'], self::$allowed_tags ) ) {
97
			$instance['unit'] = $new_instance['unit'];
98
		} else {
99
			$instance['unit'] = 'mrec';
100
		}
101
102
		return $instance;
103
	}
104
}
105
106
add_action(
107
	'widgets_init',
108
	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...
109
		'',
110
		'return register_widget( "WordAds_Sidebar_Widget" );'
111
	)
112
);
113