| Conditions | 12 |
| Paths | 18 |
| Total Lines | 46 |
| Code Lines | 24 |
| 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 |
||
| 96 | public function verify( $response, $remote_ip ) { |
||
| 97 | // No need make a request if response is empty. |
||
| 98 | if ( empty( $response ) ) { |
||
| 99 | return new WP_Error( 'missing-input-response', $this->error_codes['missing-input-response'], 400 ); |
||
| 100 | } |
||
| 101 | |||
| 102 | $resp = wp_remote_post( self::VERIFY_URL, $this->get_verify_request_params( $response, $remote_ip ) ); |
||
| 103 | if ( is_wp_error( $resp ) ) { |
||
| 104 | return $resp; |
||
| 105 | } |
||
| 106 | |||
| 107 | $resp_decoded = json_decode( wp_remote_retrieve_body( $resp ), true ); |
||
| 108 | if ( ! $resp_decoded ) { |
||
| 109 | return new WP_Error( 'invalid-json', $this->error_codes['invalid-json'], 400 ); |
||
| 110 | } |
||
| 111 | |||
| 112 | // Default error code and message. |
||
| 113 | $error_code = 'unexpected-response'; |
||
| 114 | $error_message = $this->error_codes['unexpected-response']; |
||
| 115 | |||
| 116 | // Use the first error code if exists. |
||
| 117 | if ( isset( $resp_decoded['error-codes'] ) && is_array( $resp_decoded['error-codes'] ) ) { |
||
| 118 | if ( isset( $resp_decoded['error-codes'][0] ) && isset( $this->error_codes[ $resp_decoded['error-codes'][0] ] ) ) { |
||
| 119 | $error_message = $this->error_codes[ $resp_decoded['error-codes'][0] ]; |
||
| 120 | $error_code = $resp_decoded['error-codes'][0]; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | if ( ! isset( $resp_decoded['success'] ) ) { |
||
| 125 | return new WP_Error( $error_code, $error_message ); |
||
| 126 | } |
||
| 127 | |||
| 128 | if ( true !== $resp_decoded['success'] ) { |
||
| 129 | return new WP_Error( $error_code, $error_message ); |
||
| 130 | } |
||
| 131 | |||
| 132 | // Validate the hostname matches expected source |
||
| 133 | if ( isset( $resp_decoded['hostname'] ) ) { |
||
| 134 | $url = wp_parse_url( get_home_url() ); |
||
| 135 | if ( $url['host'] !== $resp_decoded['hostname'] ) { |
||
| 136 | return new WP_Error( 'unexpected-host', $this->error_codes['unexpected-hostname'] ); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | return true; |
||
| 141 | } |
||
| 142 | |||
| 189 |