Conditions | 29 |
Paths | 522 |
Total Lines | 117 |
Lines | 12 |
Ratio | 10.26 % |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
170 | public function sign_request( $token = '', $timestamp = 0, $nonce = '', $body_hash = '', $method = '', $url = '', $body = null, $verify_body_hash = true ) { |
||
171 | if ( ! $this->secret ) { |
||
172 | return new WP_Error( 'invalid_secret', 'Invalid secret' ); |
||
173 | } |
||
174 | |||
175 | if ( ! $this->token ) { |
||
176 | return new WP_Error( 'invalid_token', 'Invalid token' ); |
||
177 | } |
||
178 | |||
179 | list( $token ) = explode( '.', $token ); |
||
180 | |||
181 | $signature_details = compact( 'token', 'timestamp', 'nonce', 'body_hash', 'method', 'url' ); |
||
182 | |||
183 | if ( 0 !== strpos( $token, "$this->token:" ) ) { |
||
184 | return new WP_Error( 'token_mismatch', 'Incorrect token', compact( 'signature_details' ) ); |
||
185 | } |
||
186 | |||
187 | // If we got an array at this point, let's encode it, so we can see what it looks like as a string. |
||
188 | if ( is_array( $body ) ) { |
||
189 | if ( count( $body ) > 0 ) { |
||
190 | // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
||
191 | $body = json_encode( $body ); |
||
192 | |||
193 | } else { |
||
194 | $body = ''; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | $required_parameters = array( 'token', 'timestamp', 'nonce', 'method', 'url' ); |
||
199 | if ( ! is_null( $body ) ) { |
||
200 | $required_parameters[] = 'body_hash'; |
||
201 | if ( ! is_string( $body ) ) { |
||
202 | return new WP_Error( 'invalid_body', 'Body is malformed.', compact( 'signature_details' ) ); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | foreach ( $required_parameters as $required ) { |
||
207 | if ( ! is_scalar( $$required ) ) { |
||
208 | return new WP_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', str_replace( '_', '-', $required ) ), compact( 'signature_details' ) ); |
||
209 | } |
||
210 | |||
211 | View Code Duplication | if ( ! strlen( $$required ) ) { |
|
212 | return new WP_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is missing.', str_replace( '_', '-', $required ) ), compact( 'signature_details' ) ); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | if ( empty( $body ) ) { |
||
217 | if ( $body_hash ) { |
||
218 | return new WP_Error( 'invalid_body_hash', 'Invalid body hash for empty body.', compact( 'signature_details' ) ); |
||
219 | } |
||
220 | } else { |
||
221 | $connection = new Connection_Manager(); |
||
222 | if ( $verify_body_hash && $connection->sha1_base64( $body ) !== $body_hash ) { |
||
223 | return new WP_Error( 'invalid_body_hash', 'The body hash does not match.', compact( 'signature_details' ) ); |
||
224 | } |
||
225 | } |
||
226 | |||
227 | $parsed = wp_parse_url( $url ); |
||
228 | if ( ! isset( $parsed['host'] ) ) { |
||
229 | return new WP_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'url' ), compact( 'signature_details' ) ); |
||
230 | } |
||
231 | |||
232 | if ( ! empty( $parsed['port'] ) ) { |
||
233 | $port = $parsed['port']; |
||
234 | } else { |
||
235 | if ( 'http' === $parsed['scheme'] ) { |
||
236 | $port = 80; |
||
237 | } elseif ( 'https' === $parsed['scheme'] ) { |
||
238 | $port = 443; |
||
239 | } else { |
||
240 | return new WP_Error( 'unknown_scheme_port', "The scheme's port is unknown", compact( 'signature_details' ) ); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | View Code Duplication | if ( ! ctype_digit( "$timestamp" ) || 10 < strlen( $timestamp ) ) { // If Jetpack is around in 275 years, you can blame mdawaffe for the bug. |
|
245 | return new WP_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'timestamp' ), compact( 'signature_details' ) ); |
||
246 | } |
||
247 | |||
248 | $local_time = $timestamp - $this->time_diff; |
||
249 | View Code Duplication | if ( $local_time < time() - 600 || $local_time > time() + 300 ) { |
|
250 | return new WP_Error( 'invalid_signature', 'The timestamp is too old.', compact( 'signature_details' ) ); |
||
251 | } |
||
252 | |||
253 | View Code Duplication | if ( 12 < strlen( $nonce ) || preg_match( '/[^a-zA-Z0-9]/', $nonce ) ) { |
|
254 | return new WP_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'nonce' ), compact( 'signature_details' ) ); |
||
255 | } |
||
256 | |||
257 | $normalized_request_pieces = array( |
||
258 | $token, |
||
259 | $timestamp, |
||
260 | $nonce, |
||
261 | $body_hash, |
||
262 | strtoupper( $method ), |
||
263 | strtolower( $parsed['host'] ), |
||
264 | $port, |
||
265 | $parsed['path'], |
||
266 | // Normalized Query String. |
||
267 | ); |
||
268 | |||
269 | $normalized_request_pieces = array_merge( $normalized_request_pieces, $this->normalized_query_parameters( isset( $parsed['query'] ) ? $parsed['query'] : '' ) ); |
||
270 | $flat_normalized_request_pieces = array(); |
||
271 | foreach ( $normalized_request_pieces as $piece ) { |
||
272 | if ( is_array( $piece ) ) { |
||
273 | foreach ( $piece as $subpiece ) { |
||
274 | $flat_normalized_request_pieces[] = $subpiece; |
||
275 | } |
||
276 | } else { |
||
277 | $flat_normalized_request_pieces[] = $piece; |
||
278 | } |
||
279 | } |
||
280 | $normalized_request_pieces = $flat_normalized_request_pieces; |
||
281 | |||
282 | $normalized_request_string = join( "\n", $normalized_request_pieces ) . "\n"; |
||
283 | |||
284 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
||
285 | return base64_encode( hash_hmac( 'sha1', $normalized_request_string, $this->secret, true ) ); |
||
286 | } |
||
287 | |||
365 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: