Completed
Push — master ( 361a80...57c765 )
by Atanas
07:10
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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 71
ccs 0
cts 24
cp 0
rs 10
wmc 5
lcom 2
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_formatted_value() 0 5 1
A __construct() 0 4 1
A set_value_from_input() 0 12 2
A to_json() 14 14 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 ) {
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