Conditions | 14 |
Paths | 450 |
Total Lines | 38 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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; |
||
|
|||
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 |