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