acf_field_checkbox   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 306
Duplicated Lines 31.05 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 95
loc 306
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 17 17 1
C render_field() 46 118 11
A render_field_settings() 0 54 1
A update_field() 10 10 1
A update_value() 22 22 3

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
*  ACF Checkbox Field Class
5
*
6
*  All the logic for this field type
7
*
8
*  @class 		acf_field_checkbox
9
*  @extends		acf_field
10
*  @package		ACF
11
*  @subpackage	Fields
12
*/
13
14
if( ! class_exists('acf_field_checkbox') ) :
15
16
class acf_field_checkbox extends acf_field {
17
	
18
	
19
	/*
20
	*  __construct
21
	*
22
	*  This function will setup the field type data
23
	*
24
	*  @type	function
25
	*  @date	5/03/2014
26
	*  @since	5.0.0
27
	*
28
	*  @param	n/a
29
	*  @return	n/a
30
	*/
0 ignored issues
show
Documentation introduced by
The doc-type n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
31
	
32 View Code Duplication
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
33
		
34
		// vars
35
		$this->name = 'checkbox';
36
		$this->label = __("Checkbox",'acf');
0 ignored issues
show
Bug introduced by
The property label does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
		$this->category = 'choice';
38
		$this->defaults = array(
39
			'layout'		=> 'vertical',
40
			'choices'		=> array(),
41
			'default_value'	=> '',
42
			'toggle'		=> 0
43
		);
44
		
45
		
46
		// do not delete!
47
    	parent::__construct();
48
	}
49
		
50
	
51
	/*
52
	*  render_field()
53
	*
54
	*  Create the HTML interface for your field
55
	*
56
	*  @param	$field (array) the $field being rendered
57
	*
58
	*  @type	action
59
	*  @since	3.6
60
	*  @date	23/01/13
61
	*
62
	*  @param	$field (array) the $field being edited
63
	*  @return	n/a
64
	*/
0 ignored issues
show
Documentation introduced by
The doc-type n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
65
	
66
	function render_field( $field ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
		
68
		// decode value (convert to array)
69
		$field['value'] = acf_get_array($field['value'], false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
		
71
		
72
		// hiden input
73
		acf_hidden_input(array(
74
			'type'	=> 'hidden',
75
			'name'	=> $field['name'],
76
		));
77
		
78
		
79
		// vars
80
		$i = 0;
81
		$li = '';
82
		$all_checked = true;
83
		
84
		
85
		// checkbox saves an array
86
		$field['name'] .= '[]';
87
		
88
		
89
		// foreach choices
90
		if( !empty($field['choices']) ) {
91
			
92 View Code Duplication
			foreach( $field['choices'] as $value => $label ) {
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...
93
				
94
				// increase counter
95
				$i++;
96
				
97
				
98
				// vars
99
				$atts = array(
100
					'type'	=> 'checkbox',
101
					'id'	=> $field['id'], 
102
					'name'	=> $field['name'],
103
					'value'	=> $value,
104
				);
105
				
106
				
107
				// is choice selected?
108
				if( in_array($value, $field['value']) ) {
109
					
110
					$atts['checked'] = 'checked';
111
					
112
				} else {
113
					
114
					$all_checked = false;
115
					
116
				}
117
				
118
				
119
				if( isset($field['disabled']) && acf_in_array($value, $field['disabled']) ) {
120
				
121
					$atts['disabled'] = 'disabled';
122
					
123
				}
124
				
125
				
126
				// each input ID is generated with the $key, however, the first input must not use $key so that it matches the field's label for attribute
127
				if( $i > 1 ) {
128
				
129
					$atts['id'] .= '-' . $value;
130
					
131
				}
132
				
133
				
134
				// append HTML
135
				$li .= '<li><label><input ' . acf_esc_attr( $atts ) . '/>' . $label . '</label></li>';
136
				
137
			}
138
			
139
			
140
			// toggle all
141
			if( $field['toggle'] ) {
142
				
143
				// vars
144
				$label = __("Toggle All", 'acf');
145
				$atts = array(
146
					'type'	=> 'checkbox',
147
					'class'	=> 'acf-checkbox-toggle'
148
				);
149
				
150
				
151
				// custom label
152
				if( is_string($field['toggle']) ) {
153
					
154
					$label = $field['toggle'];
155
					
156
				}
157
				
158
				
159
				// checked
160
				if( $all_checked ) {
161
					
162
					$atts['checked'] = 'checked';
163
					
164
				}
165
				
166
				
167
				// append HTML
168
				$li = '<li><label><input ' . acf_esc_attr( $atts ) . '/>' . $label . '</label></li>' . $li;
169
					
170
			}
171
		
172
		}
173
		
174
		
175
		// class
176
		$field['class'] .= ' acf-checkbox-list';
177
		$field['class'] .= ($field['layout'] == 'horizontal') ? ' acf-hl' : ' acf-bl';
178
179
		
180
		// return
181
		echo '<ul ' . acf_esc_attr(array( 'class' => $field['class'] )) . '>' . $li . '</ul>';
182
		
183
	}
184
	
185
	
186
	/*
187
	*  render_field_settings()
188
	*
189
	*  Create extra options for your field. This is rendered when editing a field.
190
	*  The value of $field['name'] can be used (like bellow) to save extra data to the $field
191
	*
192
	*  @type	action
193
	*  @since	3.6
194
	*  @date	23/01/13
195
	*
196
	*  @param	$field	- an array holding all the field's data
197
	*/
198
	
199
	function render_field_settings( $field ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
200
		
201
		// encode choices (convert from array)
202
		$field['choices'] = acf_encode_choices($field['choices']);
203
		$field['default_value'] = acf_encode_choices($field['default_value']);
204
				
205
		
206
		// choices
207
		acf_render_field_setting( $field, array(
208
			'label'			=> __('Choices','acf'),
209
			'instructions'	=> __('Enter each choice on a new line.','acf') . '<br /><br />' . __('For more control, you may specify both a value and label like this:','acf'). '<br /><br />' . __('red : Red','acf'),
210
			'type'			=> 'textarea',
211
			'name'			=> 'choices',
212
		));	
213
		
214
		
215
		// default_value
216
		acf_render_field_setting( $field, array(
217
			'label'			=> __('Default Value','acf'),
218
			'instructions'	=> __('Enter each default value on a new line','acf'),
219
			'type'			=> 'textarea',
220
			'name'			=> 'default_value',
221
		));
222
		
223
		
224
		// layout
225
		acf_render_field_setting( $field, array(
226
			'label'			=> __('Layout','acf'),
227
			'instructions'	=> '',
228
			'type'			=> 'radio',
229
			'name'			=> 'layout',
230
			'layout'		=> 'horizontal', 
231
			'choices'		=> array(
232
				'vertical'		=> __("Vertical",'acf'), 
233
				'horizontal'	=> __("Horizontal",'acf')
234
			)
235
		));
236
		
237
		
238
		// layout
239
		acf_render_field_setting( $field, array(
240
			'label'			=> __('Toggle','acf'),
241
			'instructions'	=> __('Prepend an extra checkbox to toggle all choices','acf'),
242
			'type'			=> 'radio',
243
			'name'			=> 'toggle',
244
			'layout'		=> 'horizontal', 
245
			'choices'		=> array(
246
				1				=> __("Yes",'acf'),
247
				0				=> __("No",'acf'),
248
			)
249
		));
250
		
251
		
252
	}
253
	
254
	
255
	/*
256
	*  update_field()
257
	*
258
	*  This filter is appied to the $field before it is saved to the database
259
	*
260
	*  @type	filter
261
	*  @since	3.6
262
	*  @date	23/01/13
263
	*
264
	*  @param	$field - the field array holding all the field options
265
	*  @param	$post_id - the field group ID (post_type = acf)
266
	*
267
	*  @return	$field - the modified field
268
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $field could not be parsed: Unknown type name "$field" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
269
270 View Code Duplication
	function update_field( $field ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
271
		
272
		// decode choices (convert to array)
273
		$field['choices'] = acf_decode_choices($field['choices']);
274
		$field['default_value'] = acf_decode_choices($field['default_value']);
275
		
276
		
277
		// return
278
		return $field;
279
	}
280
	
281
	
282
	/*
283
	*  update_value()
284
	*
285
	*  This filter is appied to the $value before it is updated in the db
286
	*
287
	*  @type	filter
288
	*  @since	3.6
289
	*  @date	23/01/13
290
	*
291
	*  @param	$value - the value which will be saved in the database
292
	*  @param	$post_id - the $post_id of which the value will be saved
293
	*  @param	$field - the field array holding all the field options
294
	*
295
	*  @return	$value - the modified value
296
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $value could not be parsed: Unknown type name "$value" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
297
	
298 View Code Duplication
	function update_value( $value, $post_id, $field ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
299
		
300
		// validate
301
		if( empty($value) ) {
302
		
303
			return $value;
304
			
305
		}
306
		
307
		
308
		// array
309
		if( is_array($value) ) {
310
			
311
			// save value as strings, so we can clearly search for them in SQL LIKE statements
312
			$value = array_map('strval', $value);
313
			
314
		}
315
		
316
		
317
		// return
318
		return $value;
319
	}
320
	
321
}
322
323
new acf_field_checkbox();
324
325
endif;
326
327
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
328