| Conditions | 13 | 
| Paths | 48 | 
| Total Lines | 105 | 
| 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  | 
            ||
| 84 | 		protected function request( $method, $path, $body = array() ) { | 
            ||
| 85 | |||
| 86 | 			if ( ! is_array( $body ) ) { | 
            ||
| 87 | return new WP_Error(  | 
            ||
| 88 | 'request_body_should_be_array',  | 
            ||
| 89 | __( 'Unable to send request to WooCommerce Connect server. Body must be an array.', 'woocommerce-gateway-stripe' )  | 
            ||
| 90 | );  | 
            ||
| 91 | }  | 
            ||
| 92 | |||
| 93 | $url = trailingslashit( WOOCOMMERCE_CONNECT_SERVER_URL );  | 
            ||
| 94 | $url = apply_filters( 'wc_connect_server_url', $url );  | 
            ||
| 95 | $url = trailingslashit( $url ) . ltrim( $path, '/' );  | 
            ||
| 96 | |||
| 97 | // Add useful system information to requests that contain bodies.  | 
            ||
| 98 | 			if ( in_array( $method, array( 'POST', 'PUT' ), true ) ) { | 
            ||
| 99 | $body = $this->request_body( $body );  | 
            ||
| 100 | $body = wp_json_encode( apply_filters( 'wc_connect_api_client_body', $body ) );  | 
            ||
| 101 | |||
| 102 | 				if ( ! $body ) { | 
            ||
| 103 | return new WP_Error(  | 
            ||
| 104 | 'unable_to_json_encode_body',  | 
            ||
| 105 | __( 'Unable to encode body for request to WooCommerce Connect server.', 'woocommerce-gateway-stripe' )  | 
            ||
| 106 | );  | 
            ||
| 107 | }  | 
            ||
| 108 | }  | 
            ||
| 109 | |||
| 110 | $headers = $this->request_headers();  | 
            ||
| 111 | 			if ( is_wp_error( $headers ) ) { | 
            ||
| 112 | return $headers;  | 
            ||
| 113 | }  | 
            ||
| 114 | |||
| 115 | $http_timeout = 60; // 1 minute  | 
            ||
| 116 | wc_set_time_limit( $http_timeout + 10 );  | 
            ||
| 117 | $args = array(  | 
            ||
| 118 | 'headers' => $headers,  | 
            ||
| 119 | 'method' => $method,  | 
            ||
| 120 | 'body' => $body,  | 
            ||
| 121 | 'redirection' => 0,  | 
            ||
| 122 | 'compress' => true,  | 
            ||
| 123 | 'timeout' => $http_timeout,  | 
            ||
| 124 | );  | 
            ||
| 125 | |||
| 126 | $args = apply_filters( 'wc_connect_request_args', $args );  | 
            ||
| 127 | $response = wp_remote_request( $url, $args );  | 
            ||
| 128 | $response_code = wp_remote_retrieve_response_code( $response );  | 
            ||
| 129 | $content_type = wp_remote_retrieve_header( $response, 'content-type' );  | 
            ||
| 130 | |||
| 131 | 			if ( false === strpos( $content_type, 'application/json' ) ) { | 
            ||
| 132 | 				if ( 200 !== $response_code ) { | 
            ||
| 133 | return new WP_Error(  | 
            ||
| 134 | 'wcc_server_error',  | 
            ||
| 135 | sprintf(  | 
            ||
| 136 | // Translators: HTTP error code.  | 
            ||
| 137 | __( 'Error: The WooCommerce Connect server returned HTTP code: %d', 'woocommerce-gateway-stripe' ),  | 
            ||
| 138 | $response_code  | 
            ||
| 139 | )  | 
            ||
| 140 | );  | 
            ||
| 141 | 				} else { | 
            ||
| 142 | return new WP_Error(  | 
            ||
| 143 | 'wcc_server_error_content_type',  | 
            ||
| 144 | sprintf(  | 
            ||
| 145 | // Translators: content-type error code.  | 
            ||
| 146 | __( 'Error: The WooCommerce Connect server returned an invalid content-type: %s.', 'woocommerce-gateway-stripe' ),  | 
            ||
| 147 | $content_type  | 
            ||
| 148 | )  | 
            ||
| 149 | );  | 
            ||
| 150 | }  | 
            ||
| 151 | }  | 
            ||
| 152 | |||
| 153 | $response_body = wp_remote_retrieve_body( $response );  | 
            ||
| 154 | 			if ( ! empty( $response_body ) ) { | 
            ||
| 155 | $response_body = json_decode( $response_body );  | 
            ||
| 156 | }  | 
            ||
| 157 | |||
| 158 | 			if ( 200 !== $response_code ) { | 
            ||
| 159 | 				if ( empty( $response_body ) ) { | 
            ||
| 160 | return new WP_Error(  | 
            ||
| 161 | 'wcc_server_empty_response',  | 
            ||
| 162 | sprintf(  | 
            ||
| 163 | // Translators: HTTP error code.  | 
            ||
| 164 | __( 'Error: The WooCommerce Connect server returned ( %d ) and an empty response body.', 'woocommerce-gateway-stripe' ),  | 
            ||
| 165 | $response_code  | 
            ||
| 166 | )  | 
            ||
| 167 | );  | 
            ||
| 168 | }  | 
            ||
| 169 | |||
| 170 | $error = property_exists( $response_body, 'error' ) ? $response_body->error : '';  | 
            ||
| 171 | $message = property_exists( $response_body, 'message' ) ? $response_body->message : '';  | 
            ||
| 172 | $data = property_exists( $response_body, 'data' ) ? $response_body->data : '';  | 
            ||
| 173 | |||
| 174 | return new WP_Error(  | 
            ||
| 175 | 'wcc_server_error_response',  | 
            ||
| 176 | sprintf(  | 
            ||
| 177 | /* translators: %1$s: error code, %2$s: error message, %3$d: HTTP response code */  | 
            ||
| 178 | __( 'Error: The WooCommerce Connect server returned: %1$s %2$s ( %3$d )', 'woocommerce-gateway-stripe' ),  | 
            ||
| 179 | $error,  | 
            ||
| 180 | $message,  | 
            ||
| 181 | $response_code  | 
            ||
| 182 | ),  | 
            ||
| 183 | $data  | 
            ||
| 184 | );  | 
            ||
| 185 | }  | 
            ||
| 186 | |||
| 187 | return $response_body;  | 
            ||
| 188 | }  | 
            ||
| 189 | |||
| 242 |