Completed
Push — develop ( 6f5051...4cbe75 )
by Aristeides
02:32
created

Kirki_Field_Number::sanitize()   F

Complexity

Conditions 14
Paths 450

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 21
nc 450
nop 1
dl 0
loc 38
rs 3.4758
c 0
b 0
f 0

How to fix   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
 * Override field methods
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
8
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
9
 * @since       2.2.7
10
 */
11
12
/**
13
 * Field overrides.
14
 */
15
class Kirki_Field_Number extends Kirki_Field {
16
17
	/**
18
	 * Sets the control type.
19
	 *
20
	 * @access protected
21
	 */
22
	protected function set_type() {
23
24
		$this->type = 'kirki-number';
25
26
	}
27
28
	/**
29
	 * Sets the $sanitize_callback
30
	 *
31
	 * @access protected
32
	 */
33
	protected function set_sanitize_callback() {
34
35
		$this->sanitize_callback = array( $this, 'sanitize' );
36
37
	}
38
39
	/**
40
	 * Sanitizes numeric values.
41
	 *
42
	 * @access public
43
	 * @param boolean|integer|string|null $value The checkbox value.
44
	 * @return bool
45
	 */
46
	public function sanitize( $value = null ) {
47
48
		// Make sure min, max & step are all numeric.
49
		$min  = ( isset( $this->choices['min'] ) && ! is_numeric( $this->choices['min'] ) ) ? filter_var( $this->choices['min'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : -999999999
50
		$max  = ( isset( $this->choices['max'] ) && ! is_numeric( $this->choices['max'] ) ) ? filter_var( $this->choices['max'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : 999999999;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE
Loading history...
51
		$step = ( isset( $this->choices['step'] ) && ! is_numeric( $this->choices['step'] ) ) ? filter_var( $this->choices['step'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) : 1;
52
53
		if ( ! is_numeric( $value ) ) {
54
			$value = filter_var( $value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
55
		}
56
57
		// Minimum value limit.
58
		if ( $value < $min ) {
59
			return $min;
60
		}
61
62
		// Maximum value limit.
63
		if ( $value > $max ) {
64
			return $max;
65
		}
66
67
		// Step divider.
68
		if ( isset( $this->choices['min'] ) && isset( $this->choices['step'] ) ) {
69
			$valid = range( $min, $max, $step );
70
71
			$smallest = array();
72
			foreach ( $valid as $possible_value ) {
73
				$smallest[ $possible_value ] = abs( $possible_value - $value );
74
			}
75
			asort( $smallest );
76
			$value = key( $smallest );
77
		}
78
79
		return $value;
80
81
	}
82
83
}
84