| Conditions | 11 |
| Paths | 15 |
| Total Lines | 48 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 22 | protected function process_value() { |
||
| 23 | $this->value = trim( $this->value ); |
||
|
|
|||
| 24 | |||
| 25 | // If you use calc() there, I suppose you know what you're doing. |
||
| 26 | // No need to process this any further, just exit. |
||
| 27 | if ( false !== strpos( $this->value, 'calc' ) ) { |
||
| 28 | return; |
||
| 29 | } |
||
| 30 | |||
| 31 | // If the value is initial or inherit, we don't need to do anything. |
||
| 32 | // Just exit. |
||
| 33 | if ( 'initial' === $this->value || 'inherit' === $this->value ) { |
||
| 34 | return; |
||
| 35 | } |
||
| 36 | |||
| 37 | $x_dimensions = array( 'left', 'center', 'right' ); |
||
| 38 | $y_dimensions = array( 'top', 'center', 'bottom' ); |
||
| 39 | |||
| 40 | // If there's a space, we have an X and a Y value. |
||
| 41 | if ( false !== strpos( $this->value, ' ' ) ) { |
||
| 42 | $xy = explode( ' ', $this->value ); |
||
| 43 | |||
| 44 | $x = trim( $xy[0] ); |
||
| 45 | $y = trim( $xy[1] ); |
||
| 46 | |||
| 47 | // If x is not left/center/right, we need to sanitize it. |
||
| 48 | if ( ! in_array( $x, $x_dimensions, true ) ) { |
||
| 49 | $x = sanitize_text_field( $x ); |
||
| 50 | } |
||
| 51 | if ( ! in_array( $y, $y_dimensions, true ) ) { |
||
| 52 | $y = sanitize_text_field( $y ); |
||
| 53 | } |
||
| 54 | $this->value = $x . ' ' . $y; |
||
| 55 | return; |
||
| 56 | } |
||
| 57 | $x = 'center'; |
||
| 58 | foreach ( $x_dimensions as $x_dimension ) { |
||
| 59 | if ( false !== strpos( $this->value, $x_dimension ) ) { |
||
| 60 | $x = $x_dimension; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | $y = 'center'; |
||
| 64 | foreach ( $y_dimensions as $y_dimension ) { |
||
| 65 | if ( false !== strpos( $this->value, $y_dimension ) ) { |
||
| 66 | $y = $y_dimension; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | $this->value = $x . ' ' . $y; |
||
| 70 | } |
||
| 72 |