Completed
Push — development ( 140d24...d693da )
by
unknown
02:48
created

Set_Field   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 19.72 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 14
loc 71
rs 10
ccs 0
cts 25
cp 0
wmc 5
lcom 2
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set_value_from_input() 0 12 2
A to_json() 14 14 1
A get_formatted_value() 0 5 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
use Carbon_Fields\Helper\Helper;
6
use Carbon_Fields\Value_Set\Value_Set;
7
8
/**
9
 * Set field class.
10
 * Allows to create a set of checkboxes where multiple can be selected.
11
 */
12
class Set_Field extends Predefined_Options_Field {
13
14
	/**
15
	 * The options limit.
16
	 *
17
	 * @var int
18
	 */
19
	protected $limit_options = 0;
20
21
	/**
22
	 * Default field value
23
	 *
24
	 * @var array
25
	 */
26
	protected $default_value = array();
27
28
	/**
29
	 * Create a field from a certain type with the specified label.
30
	 *
31
	 * @param string $type  Field type
32
	 * @param string $name  Field name
33
	 * @param string $label Field label
34
	 */
35
	public function __construct( $type, $name, $label ) {
36
		$this->set_value_set( new Value_Set( Value_Set::TYPE_MULTIPLE_VALUES ) );
37
		parent::__construct( $type, $name, $label );
38
	}
39
40
	/**
41
	 * {@inheritDoc}
42
	 */
43
	public function set_value_from_input( $input ) {
44
		if ( ! isset( $input[ $this->get_name() ] ) ) {
45
			return $this->set_value( array() );
46
		}
47
48
		$options_values = $this->get_options_values();
49
50
		$value = stripslashes_deep( $input[ $this->get_name() ] );
51
		$value = Helper::get_valid_options( $value, $options_values );
52
53
		return $this->set_value( $value );
54
	}
55
56
	/**
57
	 * {@inheritDoc}
58
	 */
59 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...
60
		$field_data = parent::to_json( $load );
61
62
		$options = $this->parse_options( $this->get_options(), true );
63
		$value = array_map( 'strval', $this->get_formatted_value() );
64
65
		$field_data = array_merge( $field_data, array(
66
			'options' => $options,
67
			'value' => $value,
68
			'limit_options' => $this->limit_options,
69
		) );
70
71
		return $field_data;
72
	}
73
74
	/**
75
	 * {@inheritDoc}
76
	 */
77
	public function get_formatted_value() {
78
		$value = $this->get_value();
79
		$value = $this->get_values_from_options( $value );
80
		return $value;
81
	}
82
}
83