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

Select_Field   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 24.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 13
loc 53
rs 10
ccs 0
cts 29
cp 0
wmc 7
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set_value_from_input() 0 18 4
A to_json() 13 13 1
A get_formatted_value() 0 9 2

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
7
/**
8
 * Select dropdown field class.
9
 */
10
class Select_Field extends Predefined_Options_Field {
11
	/**
12
	 * {@inheritDoc}
13
	 */
14
	public function set_value_from_input( $input ) {
15
		$options_values = $this->get_options_values();
16
17
		$value = null;
18
		if ( isset( $input[ $this->get_name() ] ) ) {
19
			$raw_value = stripslashes_deep( $input[ $this->get_name() ] );
20
			$raw_value = Helper::get_valid_options( array( $raw_value ), $options_values );
21
			if ( ! empty( $raw_value ) ) {
22
				$value = $raw_value[0];
23
			}
24
		}
25
26
		if ( $value === null ) {
27
			$value = $options_values[0];
28
		}
29
30
		return $this->set_value( $value );
31
	}
32
33
	/**
34
	 * {@inheritDoc}
35
	 */
36 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...
37
		$field_data = parent::to_json( $load );
38
39
		$options = $this->parse_options( $this->get_options(), true );
40
		$value = strval( $this->get_formatted_value() );
41
42
		$field_data = array_merge( $field_data, array(
43
			'value' => strval( $value ),
44
			'options' => $options,
45
		) );
46
47
		return $field_data;
48
	}
49
50
	/**
51
	 * {@inheritDoc}
52
	 */
53
	public function get_formatted_value() {
54
		$options_values = $this->get_options_values();
55
56
		$value = $this->get_value();
57
		$value = $this->get_values_from_options( array( $value ) );
58
		$value = ! empty( $value ) ? $value[0] : $options_values[0];
59
60
		return $value;
61
	}
62
}
63