WPMC_MailChimp_Groups   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 145
Duplicated Lines 30.34 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
c 1
b 0
f 1
lcom 1
cbo 0
dl 44
loc 145
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A get_value() 0 17 3
A show_group_type_checkboxes() 0 22 3
A show_group_type_radio() 23 23 3
A show_group_type_dropdown() 21 21 3

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
class WPMC_MailChimp_Groups {
3
4
	/**
5
	 * @var string ID of the current list group
6
	 * @since 1.1
7
	 */
8
	protected $group_id;
9
10
	/**
11
	 * @var array Group values
12
	 * @since 1.1
13
	 */
14
	protected $options;
15
16
	/**
17
	 * @var int Current popup (post) ID
18
	 * @since 1.1
19
	 */
20
	protected $post_id;
21
22
	public function __construct( $group_id = '', $options = array(), $post_id = 0 ) {
23
24
		if ( ! empty( $group_id ) && ! empty( $options ) ) {
25
		
26
			$this->group_name = $group_id;
0 ignored issues
show
Bug introduced by
The property group_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
			$this->options    = $options;
28
			$this->post_id    = $post_id;
29
30
		}
31
32
	}
33
34
	/**
35
	 * Get the value.
36
	 *
37
	 * Get the value for a specific group.
38
	 *
39
	 * @since  1.1.0
40
	 * @param  integer  $group  The group ID
41
	 * @param  boolean  $post_id Post ID
42
	 * @return array             Array of values for this group (empty array if no values)
43
	 */
44
	public function get_value( $group, $post_id = false ) {
45
46
		/* Make sure we have a post ID to check */
47
		if ( false === $post_id ) {
48
			$post_id = $this->post_id;
49
		}
50
51
		$value = maybe_unserialize( get_post_meta( $post_id, 'wpbo_mc_list_groups', true ) );
52
53
		if ( isset( $value[$group] ) ) {
54
			return apply_filters( "wpmc_mailchimp_groups_value_$group", $value[$group] );
55
		} else {
56
			return array();
57
		}
58
		
59
60
	}
61
62
	/**
63
	 * Display group options as checkboxes.
64
	 *
65
	 * @since  1.1.0
66
	 */
67
	public function show_group_type_checkboxes() {
68
69
		do_action( 'wpmc_mailchimp_groups_type_checkboxes_before', $this->group_name ); ?>
70
71
		<fieldset>
72
			<?php
73
			foreach( $this->options as $option ):
74
75
				$value   = $this->get_value( $this->group_name );
76
				$checked = in_array( $option['name'], $value ) ? 'checked="checked"' : '';
77
				?>
78
				<legend class="screen-reader-text"><span><?php echo $option['name']; ?></span></legend>
79
				<label for="wpbo_mc_group_<?php echo $option['id']; ?>" class="ta-label-block">
80
						<input name="wpbo_mc_list_groups[<?php echo $this->group_name; ?>][]" type="checkbox" id="wpbo_mc_group_<?php echo $option['id']; ?>" value="<?php echo $option['name']; ?>" <?php echo $checked; ?>> <?php echo $option['name']; ?>
81
				</label>
82
			<?php endforeach; ?>
83
		</fieldset>
84
85
		<?php
86
		do_action( 'wpmc_mailchimp_groups_type_checkboxes_after', $this->group_name );
87
88
	}
89
90
	/**
91
	 * Display group options as radio.
92
	 *
93
	 * @since  1.1.0
94
	 */
95 View Code Duplication
	public function show_group_type_radio() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
96
97
		do_action( 'wpmc_mailchimp_groups_type_radio_before', $this->group_name ); ?>
98
99
		<fieldset>
100
			<?php
101
			foreach( $this->options as $option ):
102
103
				$value   = $this->get_value( $this->group_name );
104
				$checked = $option['name'] == $value ? 'checked="checked"' : '';
105
				?>
106
				<label>
107
					<input type="radio" name="wpbo_mc_list_groups[<?php echo $this->group_name; ?>]" value="<?php echo $option['name']; ?>" <?php echo $checked; ?>> 
108
					<span><?php echo $option['name']; ?></span>
109
				</label>
110
				<br>
111
			<?php endforeach; ?>
112
		</fieldset>
113
114
		<?php
115
		do_action( 'wpmc_mailchimp_groups_type_radio_after', $this->group_name );
116
117
	}
118
119
	/**
120
	 * Display group options as dropdown.
121
	 *
122
	 * @since  1.1.0
123
	 */
124 View Code Duplication
	public function show_group_type_dropdown() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
125
126
		do_action( 'wpmc_mailchimp_groups_type_dropdown_before', $this->group_name ); ?>
127
128
		<select name="wpbo_mc_list_groups[<?php echo $this->group_name; ?>]" id="wpbo_mc_list_groups_<?php echo $this->group_name; ?>" style="width:100%">
129
			<?php
130
			foreach( $this->options as $option ):
131
132
				$value   = $this->get_value( $this->group_name );
133
				$checked = ( $option['name'] == $value ) ? 'selected="selected"' : '';
134
				?>
135
136
				<option value="<?php echo $option['name']; ?>" <?php echo $checked; ?>><?php echo $option['name']; ?></option>
137
138
			<?php endforeach; ?>
139
		</select>
140
141
		<?php
142
		do_action( 'wpmc_mailchimp_groups_type_dropdown_after', $this->group_name );
143
144
	}
145
146
}