| Conditions | 29 |
| Paths | 8449 |
| Total Lines | 88 |
| 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 |
||
| 21 | function sign_current_request( $override = array() ) { |
||
| 22 | if ( isset( $override['scheme'] ) ) { |
||
| 23 | $scheme = $override['scheme']; |
||
| 24 | if ( ! in_array( $scheme, array( 'http', 'https' ) ) ) { |
||
| 25 | return new WP_Error( 'invalid_scheme', 'Invalid URL scheme' ); |
||
| 26 | } |
||
| 27 | } else { |
||
| 28 | if ( is_ssl() ) { |
||
| 29 | $scheme = 'https'; |
||
| 30 | } else { |
||
| 31 | $scheme = 'http'; |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | $host_port = isset( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ? $_SERVER['HTTP_X_FORWARDED_PORT'] : $_SERVER['SERVER_PORT']; |
||
| 36 | |||
| 37 | $connection = new Connection_Manager(); |
||
| 38 | /** |
||
| 39 | * Note: This port logic is tested in the Jetpack_Cxn_Tests->test__server_port_value() test. |
||
| 40 | * Please update the test if any changes are made in this logic. |
||
| 41 | */ |
||
| 42 | if ( is_ssl() ) { |
||
| 43 | // 443: Standard Port |
||
| 44 | // 80: Assume we're behind a proxy without X-Forwarded-Port. Hardcoding "80" here means most sites |
||
| 45 | // with SSL termination proxies (self-served, Cloudflare, etc.) don't need to fiddle with |
||
| 46 | // the JETPACK_SIGNATURE__HTTPS_PORT constant. The code also implies we can't talk to a |
||
| 47 | // site at https://example.com:80/ (which would be a strange configuration). |
||
| 48 | // JETPACK_SIGNATURE__HTTPS_PORT: Set this constant in wp-config.php to the back end webserver's port |
||
| 49 | // if the site is behind a proxy running on port 443 without |
||
| 50 | // X-Forwarded-Port and the back end's port is *not* 80. It's better, |
||
| 51 | // though, to configure the proxy to send X-Forwarded-Port. |
||
| 52 | $https_port = defined( 'JETPACK_SIGNATURE__HTTPS_PORT' ) ? JETPACK_SIGNATURE__HTTPS_PORT : 443; |
||
| 53 | $port = in_array( $host_port, array( 443, 80, $https_port ) ) ? '' : $host_port; |
||
| 54 | } else { |
||
| 55 | // 80: Standard Port |
||
| 56 | // JETPACK_SIGNATURE__HTTPS_PORT: Set this constant in wp-config.php to the back end webserver's port |
||
| 57 | // if the site is behind a proxy running on port 80 without |
||
| 58 | // X-Forwarded-Port. It's better, though, to configure the proxy to |
||
| 59 | // send X-Forwarded-Port. |
||
| 60 | $http_port = defined( 'JETPACK_SIGNATURE__HTTP_PORT' ) ? JETPACK_SIGNATURE__HTTP_PORT : 80; |
||
| 61 | $port = in_array( $host_port, array( 80, $http_port ) ) ? '' : $host_port; |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->current_request_url = "{$scheme}://{$_SERVER['HTTP_HOST']}:{$port}" . stripslashes( $_SERVER['REQUEST_URI'] ); |
||
| 65 | |||
| 66 | if ( array_key_exists( 'body', $override ) && ! empty( $override['body'] ) ) { |
||
| 67 | $body = $override['body']; |
||
| 68 | } elseif ( 'POST' == strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { |
||
| 69 | $body = isset( $GLOBALS['HTTP_RAW_POST_DATA'] ) ? $GLOBALS['HTTP_RAW_POST_DATA'] : null; |
||
| 70 | |||
| 71 | // Convert the $_POST to the body, if the body was empty. This is how arrays are hashed |
||
| 72 | // and encoded on the Jetpack side. |
||
| 73 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 74 | if ( empty( $body ) && is_array( $_POST ) && count( $_POST ) > 0 ) { |
||
| 75 | $body = $_POST; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } elseif ( 'PUT' == strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { |
||
| 79 | // This is a little strange-looking, but there doesn't seem to be another way to get the PUT body |
||
| 80 | $raw_put_data = file_get_contents( 'php://input' ); |
||
| 81 | parse_str( $raw_put_data, $body ); |
||
| 82 | |||
| 83 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 84 | $put_data = json_decode( $raw_put_data, true ); |
||
| 85 | if ( is_array( $put_data ) && count( $put_data ) > 0 ) { |
||
| 86 | $body = $put_data; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } else { |
||
| 90 | $body = null; |
||
| 91 | } |
||
| 92 | |||
| 93 | if ( empty( $body ) ) { |
||
| 94 | $body = null; |
||
| 95 | } |
||
| 96 | |||
| 97 | $a = array(); |
||
| 98 | foreach ( array( 'token', 'timestamp', 'nonce', 'body-hash' ) as $parameter ) { |
||
| 99 | if ( isset( $override[ $parameter ] ) ) { |
||
| 100 | $a[ $parameter ] = $override[ $parameter ]; |
||
| 101 | } else { |
||
| 102 | $a[ $parameter ] = isset( $_GET[ $parameter ] ) ? stripslashes( $_GET[ $parameter ] ) : ''; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | $method = isset( $override['method'] ) ? $override['method'] : $_SERVER['REQUEST_METHOD']; |
||
| 107 | return $this->sign_request( $a['token'], $a['timestamp'], $a['nonce'], $a['body-hash'], $method, $this->current_request_url, $body, true ); |
||
| 108 | } |
||
| 109 | |||
| 268 |
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: