Conditions | 27 |
Paths | 690 |
Total Lines | 105 |
Code Lines | 62 |
Lines | 6 |
Ratio | 5.71 % |
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 |
||
97 | function sign_request( $token = '', $timestamp = 0, $nonce = '', $body_hash = '', $method = '', $url = '', $body = null, $verify_body_hash = true ) { |
||
98 | if ( !$this->secret ) { |
||
99 | return new Jetpack_Error( 'invalid_secret', 'Invalid secret' ); |
||
100 | } |
||
101 | |||
102 | if ( !$this->token ) { |
||
103 | return new Jetpack_Error( 'invalid_token', 'Invalid token' ); |
||
104 | } |
||
105 | |||
106 | list( $token ) = explode( '.', $token ); |
||
107 | |||
108 | if ( 0 !== strpos( $token, "$this->token:" ) ) { |
||
109 | return new Jetpack_Error( 'token_mismatch', 'Incorrect token' ); |
||
110 | } |
||
111 | |||
112 | // If we got an array at this point, let's encode it, so we can see what it looks like as a string. |
||
113 | if ( is_array( $body ) ) { |
||
114 | if ( count( $body ) > 0 ) { |
||
115 | $body = json_encode( $body ); |
||
116 | |||
117 | } else { |
||
118 | $body = ''; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | $required_parameters = array( 'token', 'timestamp', 'nonce', 'method', 'url' ); |
||
123 | if ( !is_null( $body ) ) { |
||
124 | $required_parameters[] = 'body_hash'; |
||
125 | if ( !is_string( $body ) ) { |
||
126 | return new Jetpack_Error( 'invalid_body', 'Body is malformed.' ); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | foreach ( $required_parameters as $required ) { |
||
131 | View Code Duplication | if ( !is_scalar( $$required ) ) { |
|
132 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', str_replace( '_', '-', $required ) ) ); |
||
133 | } |
||
134 | |||
135 | View Code Duplication | if ( !strlen( $$required ) ) { |
|
136 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is missing.', str_replace( '_', '-', $required ) ) ); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | if ( empty( $body ) ) { |
||
141 | if ( $body_hash ) { |
||
142 | return new Jetpack_Error( 'invalid_body_hash', 'The body hash does not match.' ); |
||
143 | } |
||
144 | } else { |
||
145 | if ( $verify_body_hash && jetpack_sha1_base64( $body ) !== $body_hash ) { |
||
146 | return new Jetpack_Error( 'invalid_body_hash', 'The body hash does not match.' ); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | $parsed = parse_url( $url ); |
||
151 | if ( !isset( $parsed['host'] ) ) { |
||
152 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'url' ) ); |
||
153 | } |
||
154 | |||
155 | if ( $parsed['host'] === JETPACK__WPCOM_JSON_API_HOST ) { |
||
156 | $parsed['host'] = 'public-api.wordpress.com'; |
||
157 | } |
||
158 | |||
159 | if ( !empty( $parsed['port'] ) ) { |
||
160 | $port = $parsed['port']; |
||
161 | } else { |
||
162 | if ( 'http' == $parsed['scheme'] ) { |
||
163 | $port = 80; |
||
164 | } else if ( 'https' == $parsed['scheme'] ) { |
||
165 | $port = 443; |
||
166 | } else { |
||
167 | return new Jetpack_Error( 'unknown_scheme_port', "The scheme's port is unknown" ); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | if ( !ctype_digit( "$timestamp" ) || 10 < strlen( $timestamp ) ) { // If Jetpack is around in 275 years, you can blame mdawaffe for the bug. |
||
172 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'timestamp' ) ); |
||
173 | } |
||
174 | |||
175 | $local_time = $timestamp - $this->time_diff; |
||
176 | if ( $local_time < time() - 600 || $local_time > time() + 300 ) { |
||
177 | return new Jetpack_Error( 'invalid_signature', 'The timestamp is too old.' ); |
||
178 | } |
||
179 | |||
180 | if ( 12 < strlen( $nonce ) || preg_match( '/[^a-zA-Z0-9]/', $nonce ) ) { |
||
181 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'nonce' ) ); |
||
182 | } |
||
183 | |||
184 | $normalized_request_pieces = array( |
||
185 | $token, |
||
186 | $timestamp, |
||
187 | $nonce, |
||
188 | $body_hash, |
||
189 | strtoupper( $method ), |
||
190 | strtolower( $parsed['host'] ), |
||
191 | $port, |
||
192 | $parsed['path'], |
||
193 | // Normalized Query String |
||
194 | ); |
||
195 | |||
196 | $normalized_request_pieces = array_merge( $normalized_request_pieces, $this->normalized_query_parameters( isset( $parsed['query'] ) ? $parsed['query'] : '' ) ); |
||
197 | |||
198 | $normalized_request_string = join( "\n", $normalized_request_pieces ) . "\n"; |
||
199 | |||
200 | return base64_encode( hash_hmac( 'sha1', $normalized_request_string, $this->secret, true ) ); |
||
201 | } |
||
202 | |||
236 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.