Completed
Push — master ( ba1cd7...836330 )
by Jamie
02:56
created

FrmFieldValue   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 187
Duplicated Lines 3.74 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 7
loc 187
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 4
A init_source() 7 7 4
A init_saved_value() 0 9 2
A init_displayed_value() 0 5 1
A get_saved_value() 0 3 1
A get_field_label() 0 3 1
A get_field_key() 0 3 1
A get_field_type() 0 3 1
A get_displayed_value() 0 3 1
A filter_displayed_value() 0 23 3
A clean_saved_value() 0 10 4

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
/**
4
 * @since 2.03.11
5
 */
6
class FrmFieldValue {
7
8
	/**
9
	 * @since 2.03.11
10
	 *
11
	 * @var stdClass
12
	 */
13
	protected $field = null;
14
15
	/**
16
	 * @since 2.03.11
17
	 *
18
	 * @var stdClass
19
	 */
20
	protected $entry = null;
21
22
	/**
23
	 * @since 2.03.11
24
	 *
25
	 * @var string
26
	 */
27
	protected $source = '';
28
29
	/**
30
	 * @since 2.03.11
31
	 *
32
	 * @var mixed
33
	 */
34
	protected $saved_value = '';
35
36
	/**
37
	 * @since 2.03.11
38
	 *
39
	 * @var mixed
40
	 */
41
	protected $displayed_value = '';
42
43
	/**
44
	 * FrmFieldValue constructor.
45
	 *
46
	 * @param stdClass $field
47
	 * @param stdClass $entry
48
	 * @param array $atts
49
	 */
50
	public function __construct( $field, $entry, $atts = array() ) {
51
		if ( ! is_object( $field ) || ! is_object( $entry ) || ! isset( $entry->metas ) ) {
52
			return;
53
		}
54
55
		$this->field = $field;
56
		$this->entry = $entry;
57
		$this->init_source( $atts );
58
		$this->init_saved_value();
59
		$this->init_displayed_value();
60
	}
61
62
	/**
63
	 * Initialize the source property
64
	 *
65
	 * @since 2.03.11
66
	 *
67
	 * @param array $atts
68
	 */
69 View Code Duplication
	protected function init_source( $atts ) {
0 ignored issues
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...
70
		if ( isset( $atts['source'] ) && is_string( $atts['source'] ) && $atts['source'] !== '' ) {
71
			$this->source = (string) $atts['source'];
72
		} else {
73
			$this->source = 'general';
74
		}
75
	}
76
77
	/**
78
	 * Initialize the saved_value property
79
	 *
80
	 * @since 2.03.11
81
	 */
82
	protected function init_saved_value() {
83
		if ( isset( $this->entry->metas[ $this->field->id ] ) ) {
84
			$this->saved_value = $this->entry->metas[ $this->field->id ];
85
		} else {
86
			$this->saved_value = '';
87
		}
88
89
		$this->clean_saved_value();
90
	}
91
92
	/**
93
	 * Initialize a field's displayed value
94
	 *
95
	 * @since 2.03.11
96
	 */
97
	protected function init_displayed_value() {
98
		$this->displayed_value = $this->saved_value;
99
100
		$this->filter_displayed_value();
101
	}
102
103
	/**
104
	 * Get the saved_value property
105
	 *
106
	 * @since 2.03.11
107
	 */
108
	public function get_saved_value() {
109
		return $this->saved_value;
110
	}
111
112
	/**
113
	 * Get the field property's label
114
	 *
115
	 * @since 2.03.11
116
	 */
117
	public function get_field_label() {
118
		return $this->field->name;
119
	}
120
121
	/**
122
	 * Get the field property's key
123
	 *
124
	 * @since 2.03.11
125
	 */
126
	public function get_field_key() {
127
		return $this->field->field_key;
128
	}
129
130
	/**
131
	 * Get the field property's type
132
	 *
133
	 * @since 2.03.11
134
	 */
135
	public function get_field_type() {
136
		return $this->field->type;
137
	}
138
139
	/**
140
	 * Get the displayed_value property
141
	 *
142
	 * @since 2.03.11
143
	 */
144
	public function get_displayed_value() {
145
		return $this->displayed_value;
146
	}
147
148
	/**
149
	 * Filter the displayed_value property
150
	 *
151
	 * @since 2.03.11
152
	 */
153
	protected function filter_displayed_value() {
154
155
		if ( $this->source === 'entry_formatter' ) {
156
			// Deprecated frm_email_value hook
157
			$meta                  = array(
158
				'item_id'    => $this->entry->id,
159
				'field_id'   => $this->field->id,
160
				'meta_value' => $this->saved_value,
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
161
				'field_type' => $this->field->type
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
162
			);
163
			$this->displayed_value = apply_filters( 'frm_email_value', $this->displayed_value, (object) $meta, $this->entry, array(
164
				'field' => $this->field,
165
			) );
166
			if ( has_filter( 'frm_email_value' ) ) {
167
				_deprecated_function( 'The frm_email_value filter', '2.03.11', 'the frm_display_{fieldtype}_value_custom filter' );
168
			}
169
		}
170
171
		// frm_display_{fieldtype}_value_custom hook
172
		$this->displayed_value = apply_filters( 'frm_display_' . $this->field->type . '_value_custom', $this->displayed_value, array(
173
			'field' => $this->field,
174
		) );
175
	}
176
177
	/**
178
	 * Clean a field's saved value
179
	 *
180
	 * @since 2.03.11
181
	 */
182
	protected function clean_saved_value() {
183
		if ( $this->saved_value !== '' ) {
184
185
			$this->saved_value = maybe_unserialize( $this->saved_value );
186
187
			if ( is_array( $this->saved_value ) && empty( $this->saved_value ) ) {
188
				$this->saved_value = '';
189
			}
190
		}
191
	}
192
}