Completed
Push — develop ( 9f3e22...fba03d )
by Aristeides
02:33
created

Kirki_Field_Number::sanitize()   F

Complexity

Conditions 14
Paths 450

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 20
nc 450
nop 1
dl 0
loc 36
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 checkbox values.
41
	 *
42
	 * @static
43
	 * @access public
44
	 * @param boolean|integer|string|null $value The checkbox value.
45
	 * @return bool
46
	 */
47
	public static function sanitize( $value = null ) {
48
49
		$value = ( is_numeric( $value ) ) ? $value : intval( $value );
50
51
		// Minimum value limit.
52
		if ( isset( $this->choices['min'] ) ) {
53
			$min = ( is_numeric( $this->choices['min'] ) ) ? $this->choices['min'] : intval( $this->choices['min'] );
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
54
			if ( $value < $min ) {
55
				$value = $min;
56
			}
57
		}
58
59
		// Maximum value limit.
60
		if ( isset( $this->choices['max'] ) ) {
61
			$max = ( is_numeric( $this->choices['max'] ) ) ? $this->choices['max'] : intval( $this->choices['max'] );
62
			if ( $value > $max ) {
63
				$value = $max;
64
			}
65
		}
66
67
		// Step divider.
68
		if ( isset( $this->choices['min'] ) && isset( $this->choices['step'] ) ) {
69
			$min   = ( is_numeric( $this->choices['min'] ) ) ? $this->choices['min'] : intval( $this->choices['min'] );
70
			$max   = ( is_numeric( $this->choices['max'] ) ) ? $this->choices['max'] : intval( $this->choices['max'] );
71
			$step  = ( is_numeric( $this->choices['step'] ) ) ? $this->choices['step'] : intval( $this->choices['step'] );
72
			$valid = range( $min, $max, $step );
73
			foreach ( $valid as $possible_value ) {
74
				$smallest[ $possible_value ] = abs( $possible_value - $value );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$smallest was never initialized. Although not strictly required by PHP, it is generally a good practice to add $smallest = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
75
			}
76
			asort( $smallest );
77
			$value = key( $smallest );
78
		}
79
80
		return $value;
81
82
	}
83
84
}
85