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 |
||
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() { |
||
82 | } |
||
83 |