| Conditions | 10 |
| Paths | 216 |
| Total Lines | 39 |
| Code Lines | 18 |
| 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 |
||
| 65 | public function sanitize( $value = null ) { |
||
| 66 | |||
| 67 | // Make sure value in an array. |
||
| 68 | $value = ( ! is_array( $value ) ) ? array() : $value; |
||
| 69 | |||
| 70 | foreach ( array( 'start', 'end' ) as $context ) { |
||
| 71 | |||
| 72 | // Make sure value is array. |
||
| 73 | if ( ! isset( $value[ $context ] ) ) { |
||
| 74 | $value[ $context ] = array(); |
||
| 75 | } |
||
| 76 | |||
| 77 | // Sanitize colors. |
||
| 78 | if ( ! isset( $value[ $context ]['color'] ) ) { |
||
| 79 | $value[ $context ]['color'] = ''; |
||
| 80 | } |
||
| 81 | $value[ $context ]['color'] = esc_attr( $value[ $context ]['color'] ); |
||
| 82 | |||
| 83 | // Sanitize positions. |
||
| 84 | if ( ! isset( $value[ $context ]['position'] ) ) { |
||
| 85 | $value[ $context ]['position'] = 0; |
||
| 86 | }; |
||
| 87 | $value[ $context ]['position'] = (int) $value[ $context ]['position']; |
||
| 88 | $value[ $context ]['position'] = max( min( $value[ $context ]['position'], 100 ), 0 ); |
||
| 89 | } |
||
| 90 | |||
| 91 | // Sanitize angle. |
||
| 92 | if ( ! isset( $value['angle'] ) ) { |
||
| 93 | $value['angle'] = 0; |
||
| 94 | } |
||
| 95 | $value['angle'] = (int) $value['angle']; |
||
| 96 | $value['angle'] = max( min( $value['angle'], 90 ), -90 ); |
||
| 97 | |||
| 98 | // Sanitize the type. |
||
| 99 | $value['type'] = ( ! isset( $value['type'] ) || 'linear' !== $value['type'] || 'radial' !== $value['type'] ) ? 'linear' : $value['type']; |
||
|
|
|||
| 100 | |||
| 101 | return $value; |
||
| 102 | |||
| 103 | } |
||
| 104 | |||
| 106 |