| Conditions | 14 |
| Paths | 450 |
| Total Lines | 36 |
| Code Lines | 20 |
| 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 |
||
| 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'] ); |
||
|
|
|||
| 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 ); |
||
| 75 | } |
||
| 76 | asort( $smallest ); |
||
| 77 | $value = key( $smallest ); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $value; |
||
| 81 | |||
| 82 | } |
||
| 83 | |||
| 85 |
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.