Completed
Pull Request — master (#1)
by
unknown
10:02
created

Set_Field   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 13.19 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 8
c 6
b 1
f 0
lcom 1
cbo 1
dl 12
loc 91
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A limit_options() 0 4 1
B get_value() 0 19 5
A to_json() 12 12 1
B template() 0 29 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
namespace Carbon_Fields\Field;
4
5
/**
6
 * Set field class.
7
 * Allows to create a set of checkboxes where multiple can be selected.
8
 */
9
class Set_Field extends Predefined_Options_Field {
10
	protected $limit_options = 0;
11
12
	/**
13
	 * Set the number of the options to be displayed at the initial field display.
14
	 * 
15
	 * @param  int $limit
16
	 */
17
	public function limit_options( $limit ) {
18
		$this->limit_options = $limit;
19
		return $this;
20
	}
21
22
	/**
23
	 * Retrieve the field value(s).
24
	 * 
25
	 * @return array
26
	 */
27
	public function get_value() {
28
		if ( $this->value === false ) {
29
			return array();
30
		}
31
32
		$this->load_options();
33
34
		if ( ! is_array( $this->value ) ) {
35
			$this->value = maybe_unserialize( $this->value );
36
			if ( ! is_array( $this->value ) ) {
37
				if ( is_null( $this->value ) ) {
38
					return array();
39
				}
40
				return array( $this->value );
41
			}
42
		}
43
44
		return (array) $this->value;
45
	}
46
47
	/**
48
	 * Returns an array that holds the field data, suitable for JSON representation.
49
	 * This data will be available in the Underscore template and the Backbone Model.
50
	 * 
51
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
52
	 * @return array
53
	 */
54 View Code Duplication
	public function to_json( $load ) {
1 ignored issue
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...
55
		$field_data = parent::to_json( $load );
56
57
		$this->load_options();
58
59
		$field_data = array_merge( $field_data, array(
60
			'limit_options' => $this->limit_options,
61
			'options' => $this->parse_options( $this->options ),
62
		) );
63
64
		return $field_data;
65
	}
66
67
	/**
68
	 * The Underscore template of this field.
69
	 */
70
	public function template() {
71
		?>
72
		<# if (_.isEmpty(options)) { #>
73
			<em><?php _e( 'no options', 'carbon_fields' ); ?></em>
74
		<# } else { #>
75
			<div class="carbon-set-list">
76
				<# _.each(options, function(option, i) { #>
77
					<# 
78
						var selected = jQuery.inArray(String(option.value), value) > -1;
79
						var counter = i + 1;
80
						var exceed = limit_options > 0 && counter > limit_options;
81
						var last = options.length === counter;
82
					#>
83
84
					<p {{{ exceed ? 'style="display:none"' : '' }}}>
85
						<label>
86
							<input type="checkbox" name="{{{ name }}}[]" value="{{ option.value }}" {{{ selected ? 'checked="checked"' : '' }}} />
87
							{{{ option.name }}}
88
						</label>
89
					</p>
90
91
					<# if (!exceed && !last && counter == limit_options) { #>
92
						<p>... <a href="#" class="carbon-set-showall"><?php _e( 'Show All Options', 'carbon_fields' ); ?></a></p>
93
					<# } #>
94
				<# }) #>
95
			</div>
96
		<# } #>
97
		<?php
98
	}
99
}
100