Field::__construct()   F
last analyzed

Complexity

Conditions 22
Paths > 20000

Size

Total Lines 67
Code Lines 37

Duplication

Lines 6
Ratio 8.96 %

Importance

Changes 0
Metric Value
cc 22
eloc 37
nc 65536
nop 1
dl 6
loc 67
rs 2.7542
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Input Field
4
 *
5
 * @package SimpleCalendar/Admin
6
 */
7
namespace SimpleCalendar\Abstracts;
8
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
/**
14
 * The Field.
15
 *
16
 * Object for handling an input field in plugin back end.
17
 *
18
 * @since 3.0.0
19
 */
20
abstract class Field {
21
22
	/**
23
	 * Field type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = '';
29
30
	/**
31
	 * Field name.
32
	 *
33
	 * @access public
34
	 * @var string
35
	 */
36
	public $name = '';
37
38
	/**
39
	 * Field Id.
40
	 *
41
	 * @access public
42
	 * @var string
43
	 */
44
	public $id = '';
45
46
	/**
47
	 * Field title (label).
48
	 *
49
	 * @access public
50
	 * @var string
51
	 */
52
	public $title = '';
53
54
	/**
55
	 * Field type class.
56
	 *
57
	 * @access protected
58
	 * @var string
59
	 */
60
	protected $type_class = '';
61
62
	/**
63
	 * CSS classes.
64
	 *
65
	 * @access public
66
	 * @var string
67
	 */
68
	public $class = '';
69
70
	/**
71
	 * CSS styles.
72
	 *
73
	 * @access public
74
	 * @var string
75
	 */
76
	public $style = '';
77
78
	/**
79
	 * Description.
80
	 *
81
	 * @access public
82
	 * @var string
83
	 */
84
	public $description = '';
85
86
	/**
87
	 * Tooltip text.
88
	 *
89
	 * @access public
90
	 * @var string
91
	 */
92
	public $tooltip = '';
93
94
	/**
95
	 * Attributes.
96
	 *
97
	 * @access public
98
	 * @var string
99
	 */
100
	public $attributes = '';
101
102
	/**
103
	 * Placeholder.
104
	 *
105
	 * @access public
106
	 * @var string
107
	 */
108
	public $placeholder = '';
109
110
	/**
111
	 * Options.
112
	 *
113
	 * @access public
114
	 * @var array
115
	 */
116
	public $options = array();
117
118
	/**
119
	 * Value.
120
	 *
121
	 * @access public
122
	 * @var array|string
123
	 */
124
	public $value = '';
125
126
	/**
127
	 * Default value.
128
	 *
129
	 * @access public
130
	 * @var array|string
131
	 */
132
	public $default = '';
133
134
	public $text = '';
135
136
	/**
137
	 * Validation result.
138
	 *
139
	 * @access public
140
	 * @var string|true
141
	 */
142
	public $validation = true;
143
144
	/**
145
	 * Construct the field.
146
	 *
147
	 * Escapes and sets every field property.
148
	 *
149
	 * @since 3.0.0
150
	 *
151
	 * @param array $field Field data.
152
	 */
153
	public function __construct( $field ) {
154
155
		// Field properties.
156
		if ( isset( $field['title'] ) ) {
157
			$this->title = esc_attr( $field['title'] );
158
		}
159
		if ( isset( $field['description'] ) ) {
160
			$this->description = wp_kses_post( $field['description'] );
161
		}
162
		if ( isset( $field['type'] ) ) {
163
			$this->type = esc_attr( $field['type'] );
164
		}
165
		if ( isset( $field['name'] ) ) {
166
			$this->name = esc_attr( $field['name'] );
167
		}
168
		if ( isset( $field['id'] ) ) {
169
			$this->id = esc_attr( $field['id'] );
170
		}
171
		if ( isset( $field['placeholder'] ) ) {
172
			$this->placeholder = esc_attr( $field['placeholder'] );
173
		}
174
		if ( isset( $field['options'] ) && is_array( $field['options'] ) ) {
175
			$this->options = array_map( 'esc_attr', $field['options'] );
176
		}
177
		if ( isset( $field['text'] ) ) {
178
			$this->text = $field['text'];
179
		}
180
181
		// Escaping.
182
		if ( ! empty( $field['escaping'] ) && ( is_string( $field['escaping'] ) || is_array( $field['escaping'] ) ) ) {
183 View Code Duplication
			if ( isset( $field['default'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
184
				$this->default = $this->escape_callback( $field['escaping'], $field['default'] );
185
			}
186 View Code Duplication
			if ( isset( $field['value'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
187
				$this->value = $this->escape_callback( $field['escaping'], $field['value'] );
188
			}
189
		} else {
190
			if ( isset( $field['default'] ) ) {
191
				$this->default = $this->escape( $field['default'] );
192
			}
193
			if ( isset( $field['value'] ) ) {
194
				$this->value = $this->escape( $field['value'] );
195
			}
196
		}
197
198
		// Validation.
199
		if ( ! empty( $field['validation'] ) ) {
200
			$this->validation = $this->validate( $field['validation'], $this->value );
201
		}
202
203
		// CSS classes and styles.
204
		$classes = isset( $field['class'] ) ? $field['class'] : '';
205
		$this->set_class( $classes );
206
		if ( isset( $field['style'] ) ) {
207
			$this->set_style( $field['style'] );
208
		}
209
210
		// Custom attributes.
211
		if ( isset( $field['attributes'] ) ) {
212
			$this->set_attributes( $field['attributes'] );
213
		}
214
215
		// Tooltip markup.
216
		if ( isset( $field['tooltip'] ) ) {
217
			$this->tooltip = ' <i class="simcal-icon-help simcal-help-tip" data-tip="' . esc_attr( $field['tooltip'] ) . '"></i> ' ;
218
		}
219
	}
220
221
	/**
222
	 * Set custom HTML attributes.
223
	 *
224
	 * @since  3.0.0
225
	 *
226
	 * @param  array $attributes
227
	 *
228
	 * @return void
229
	 */
230 View Code Duplication
	public function set_attributes( $attributes ) {
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...
231
232
		$attr = '';
233
234
		if ( ! empty( $attributes ) && is_array( $attributes ) ) {
235
			foreach ( $attributes as $k => $v ) {
236
				$attr .= esc_attr( $k ) . '="' . esc_attr( $v ) . '" ';
237
			}
238
		}
239
240
		$this->attributes = $attr;
241
	}
242
243
	/**
244
	 * Set CSS styles.
245
	 *
246
	 * @since  3.0.0
247
	 *
248
	 * @param  array $css
249
	 *
250
	 * @return void
251
	 */
252 View Code Duplication
	public function set_style( $css ) {
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...
253
254
		$styles = '';
255
256
		if ( ! empty( $css ) && is_array( $css ) ) {
257
			foreach ( $css as $k => $v ) {
258
				$styles .= esc_attr( $k ) . ': ' . esc_attr( $v ) . '; ';
259
			}
260
		}
261
262
		$this->style = $styles;
263
	}
264
265
	/**
266
	 * Set classes.
267
	 *
268
	 * @since  3.0.0
269
	 *
270
	 * @param  array $class
271
	 *
272
	 * @return void
273
	 */
274
	public function set_class( $class ) {
275
276
		$classes = '';
277
		$type_class = '';
278
		$error = '';
279
280
		if ( ! empty( $class ) && is_array( $class ) ) {
281
			$classes = implode( ' ', array_map( 'esc_attr', $class ) );
282
		}
283
		if ( ! empty( $this->type_class ) ) {
284
			$type_class = esc_attr( $this->type_class );
285
		}
286
		if ( true !== $this->validation && ! empty( $this->validation ) ) {
287
			$error = 'simcal-field-error ';
288
		}
289
290
		$this->class = trim( $error . 'simcal-field ' . $type_class . ' ' . $classes );
291
	}
292
293
	/**
294
	 * Escape field value.
295
	 *
296
	 * Default escape function, a wrapper for esc_attr().
297
	 *
298
	 * @since  3.0.0
299
	 * @access protected
300
	 *
301
	 * @param  array|string|int $value
302
	 *
303
	 * @return array|string
304
	 */
305
	protected function escape( $value )  {
306
		return ! empty( $value ) ? ( is_array( $value ) ? array_map( 'esc_attr', $value ) : esc_attr( $value ) ) : '';
307
	}
308
309
	/**
310
	 * Escape callback.
311
	 *
312
	 * Custom escaping function set in field args.
313
	 *
314
	 * @since  3.0.0
315
	 * @access protected
316
	 *
317
	 * @param  array|string $callback
318
	 * @param  mixed        $value
319
	 *
320
	 * @return mixed
321
	 */
322
	protected function escape_callback( $callback, $value ) {
323
		if ( $callback && ( is_string( $callback ) || is_array( $callback ) ) ) {
324
			return call_user_func( $callback, $value );
325
		}
326
		return esc_attr( $value );
327
	}
328
329
	/**
330
	 * Validation callback.
331
	 *
332
	 * Custom field validation callback set in field args.
333
	 *
334
	 * @since  3.0.0
335
	 * @access protected
336
	 *
337
	 * @param  array|string $callback
338
	 * @param  string       $value
339
	 *
340
	 * @return true|string Expected to return bool (true) if passes, message string if not.
341
	 */
342
	protected function validate( $callback, $value ) {
343
		if ( $callback && ( is_string( $callback ) || is_array( $callback ) ) ) {
344
			$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : '';
345
			return call_user_func( $callback, $value, $screen );
346
		}
347
		return true;
348
	}
349
350
	/**
351
	 * Outputs the field markup.
352
	 *
353
	 * @since  3.0.0
354
	 *
355
	 * @return void
356
	 */
357
	abstract public function html();
358
359
}
360