Completed
Pull Request — master (#34)
by
unknown
03:04
created

Multiselect_Field   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 37
loc 37
rs 10
wmc 2
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A to_json() 9 9 1
A template() 15 15 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
 * Select dropdown field class wtih multiple selection enabled.
7
 */
8 View Code Duplication
class Multiselect_Field extends Predefined_Options_Field {
0 ignored issues
show
Duplication introduced by
This class 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...
9
	/**
10
	 * Returns an array that holds the field data, suitable for JSON representation.
11
	 * This data will be available in the Underscore template and the Backbone Model.
12
	 *
13
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
14
	 * @return array
15
	 */
16
	public function to_json( $load ) {
17
		$field_data = parent::to_json( $load );
18
		$this->load_options();
19
20
		$field_data = array_merge( $field_data, array(
21
			'options' => $this->parse_options( $this->options ),
22
		) );
23
		return $field_data;
24
	}
25
26
	/**
27
	 * The main Underscore template of this field.
28
	 */
29
	public function template() {
30
		?>
31
		<# if (_.isEmpty(options)) { #>
32
			<em><?php _e( 'no options', 'carbon_fields' ); ?></em>
33
		<# } else { #>
34
			<select id="{{{ id }}}" name="{{{ name }}}[]" multiple {{{ special_attrs }}}>
35
				<# _.each(options, function(option) { #>
36
                    <option value="{{ option.value }}" {{{ value && ( value.indexOf(option.value) != -1 ) ? 'selected="selected"' : '' }}}>
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
37
						{{{ option.name }}}
38
					</option>
39
				<# }) #>
40
			</select>
41
		<# } #>
42
		<?php
43
	}
44
}
45