| Conditions | 12 |
| Paths | 18 |
| Total Lines | 60 |
| 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 |
||
| 98 | public function verify( $response, $remote_ip ) { |
||
| 99 | // No need make a request if response is empty. |
||
| 100 | if ( empty( $response ) ) { |
||
| 101 | return new WP_Error( 'missing-input-response', $this->error_codes['missing-input-response'], 400 ); |
||
| 102 | } |
||
| 103 | |||
| 104 | $resp = wp_remote_post( self::VERIFY_URL, $this->get_verify_request_params( $response, $remote_ip ) ); |
||
| 105 | if ( is_wp_error( $resp ) ) { |
||
| 106 | return $resp; |
||
| 107 | } |
||
| 108 | |||
| 109 | $resp_decoded = json_decode( wp_remote_retrieve_body( $resp ), true ); |
||
| 110 | if ( ! $resp_decoded ) { |
||
| 111 | return new WP_Error( 'invalid-json', $this->error_codes['invalid-json'], 400 ); |
||
| 112 | } |
||
| 113 | |||
| 114 | // Default error code and message. |
||
| 115 | $error_code = 'unexpected-response'; |
||
| 116 | $error_message = $this->error_codes['unexpected-response']; |
||
| 117 | |||
| 118 | // Use the first error code if exists. |
||
| 119 | if ( isset( $resp_decoded['error-codes'] ) && is_array( $resp_decoded['error-codes'] ) ) { |
||
| 120 | if ( isset( $resp_decoded['error-codes'][0] ) && isset( $this->error_codes[ $resp_decoded['error-codes'][0] ] ) ) { |
||
| 121 | $error_message = $this->error_codes[ $resp_decoded['error-codes'][0] ]; |
||
| 122 | $error_code = $resp_decoded['error-codes'][0]; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | if ( ! isset( $resp_decoded['success'] ) ) { |
||
| 127 | return new WP_Error( $error_code, $error_message ); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ( true !== $resp_decoded['success'] ) { |
||
| 131 | return new WP_Error( $error_code, $error_message ); |
||
| 132 | } |
||
| 133 | // Validate the hostname matches expected source |
||
| 134 | if ( isset( $resp_decoded['hostname'] ) ) { |
||
| 135 | $url = wp_parse_url( get_home_url() ); |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Allow other valid hostnames. |
||
| 139 | * |
||
| 140 | * This can be useful in cases where the token hostname is expected to be |
||
| 141 | * different from the get_home_url (ex. AMP recaptcha token contains a different hostname) |
||
| 142 | * |
||
| 143 | * @module sharedaddy |
||
| 144 | * |
||
| 145 | * @since 9.1.0 |
||
| 146 | * |
||
| 147 | * @param array [ $url['host'] ] List of the valid hostnames to check against. |
||
| 148 | */ |
||
| 149 | $valid_hostnames = apply_filters( 'jetpack_recaptcha_valid_hostnames', array( $url['host'] ) ); |
||
| 150 | |||
| 151 | if ( ! in_array( $resp_decoded['hostname'], $valid_hostnames, true ) ) { |
||
| 152 | return new WP_Error( 'unexpected-host', $this->error_codes['unexpected-hostname'] ); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | return true; |
||
| 157 | } |
||
| 158 | |||
| 224 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: