| Conditions | 11 |
| Paths | 14 |
| Total Lines | 42 |
| Code Lines | 33 |
| 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 |
||
| 43 | protected function set_props( &$object ) { |
||
| 44 | $props = [ |
||
| 45 | 'username', |
||
| 46 | 'password', |
||
| 47 | 'email', |
||
| 48 | 'first_name', |
||
| 49 | 'last_name', |
||
| 50 | 'billing', |
||
| 51 | 'shipping', |
||
| 52 | 'billing', |
||
| 53 | 'shipping', |
||
| 54 | ]; |
||
| 55 | |||
| 56 | $request_props = array_intersect_key( $this->request, array_flip( $props ) ); |
||
| 57 | $prop_values = []; |
||
| 58 | |||
| 59 | foreach ( $request_props as $prop => $value ) { |
||
| 60 | switch ( $prop ) { |
||
| 61 | case 'email': |
||
| 62 | if ( email_exists( $value ) && $value !== $object->get_email() ) { |
||
| 63 | throw new \WC_REST_Exception( 'woocommerce_rest_customer_invalid_email', __( 'Email address is invalid.', 'woocommerce' ), 400 ); |
||
| 64 | } |
||
| 65 | $prop_values[ $prop ] = $value; |
||
| 66 | break; |
||
| 67 | case 'username': |
||
| 68 | if ( $object->get_id() && $value !== $object->get_username() ) { |
||
| 69 | throw new \WC_REST_Exception( 'woocommerce_rest_customer_invalid_argument', __( "Username isn't editable.", 'woocommerce' ), 400 ); |
||
| 70 | } |
||
| 71 | $prop_values[ $prop ] = $value; |
||
| 72 | break; |
||
| 73 | case 'billing': |
||
| 74 | case 'shipping': |
||
| 75 | $address = $this->parse_address_field( $value, $object, $prop ); |
||
| 76 | $prop_values = array_merge( $prop_values, $address ); |
||
| 77 | break; |
||
| 78 | default: |
||
| 79 | $prop_values[ $prop ] = $value; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | foreach ( $prop_values as $prop => $value ) { |
||
| 84 | $object->{"set_$prop"}( $value ); |
||
| 85 | } |
||
| 106 |