Conditions | 29 |
Paths | 522 |
Total Lines | 112 |
Lines | 6 |
Ratio | 5.36 % |
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 |
||
106 | function sign_request( $token = '', $timestamp = 0, $nonce = '', $body_hash = '', $method = '', $url = '', $body = null, $verify_body_hash = true ) { |
||
107 | if ( !$this->secret ) { |
||
108 | return new Jetpack_Error( 'invalid_secret', 'Invalid secret' ); |
||
109 | } |
||
110 | |||
111 | if ( !$this->token ) { |
||
112 | return new Jetpack_Error( 'invalid_token', 'Invalid token' ); |
||
113 | } |
||
114 | |||
115 | list( $token ) = explode( '.', $token ); |
||
116 | |||
117 | if ( 0 !== strpos( $token, "$this->token:" ) ) { |
||
118 | return new Jetpack_Error( 'token_mismatch', 'Incorrect token' ); |
||
119 | } |
||
120 | |||
121 | // If we got an array at this point, let's encode it, so we can see what it looks like as a string. |
||
122 | if ( is_array( $body ) ) { |
||
123 | if ( count( $body ) > 0 ) { |
||
124 | $body = json_encode( $body ); |
||
125 | |||
126 | } else { |
||
127 | $body = ''; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | $required_parameters = array( 'token', 'timestamp', 'nonce', 'method', 'url' ); |
||
132 | if ( !is_null( $body ) ) { |
||
133 | $required_parameters[] = 'body_hash'; |
||
134 | if ( !is_string( $body ) ) { |
||
135 | return new Jetpack_Error( 'invalid_body', 'Body is malformed.' ); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | foreach ( $required_parameters as $required ) { |
||
140 | View Code Duplication | if ( !is_scalar( $$required ) ) { |
|
141 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', str_replace( '_', '-', $required ) ) ); |
||
142 | } |
||
143 | |||
144 | View Code Duplication | if ( !strlen( $$required ) ) { |
|
145 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is missing.', str_replace( '_', '-', $required ) ) ); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | if ( empty( $body ) ) { |
||
150 | if ( $body_hash ) { |
||
151 | return new Jetpack_Error( 'invalid_body_hash', 'The body hash does not match.' ); |
||
152 | } |
||
153 | } else { |
||
154 | if ( $verify_body_hash && jetpack_sha1_base64( $body ) !== $body_hash ) { |
||
155 | return new Jetpack_Error( 'invalid_body_hash', 'The body hash does not match.' ); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | $parsed = parse_url( $url ); |
||
160 | if ( !isset( $parsed['host'] ) ) { |
||
161 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'url' ) ); |
||
162 | } |
||
163 | |||
164 | if ( !empty( $parsed['port'] ) ) { |
||
165 | $port = $parsed['port']; |
||
166 | } else { |
||
167 | if ( 'http' == $parsed['scheme'] ) { |
||
168 | $port = 80; |
||
169 | } else if ( 'https' == $parsed['scheme'] ) { |
||
170 | $port = 443; |
||
171 | } else { |
||
172 | return new Jetpack_Error( 'unknown_scheme_port', "The scheme's port is unknown" ); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | if ( !ctype_digit( "$timestamp" ) || 10 < strlen( $timestamp ) ) { // If Jetpack is around in 275 years, you can blame mdawaffe for the bug. |
||
177 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'timestamp' ) ); |
||
178 | } |
||
179 | |||
180 | $local_time = $timestamp - $this->time_diff; |
||
181 | if ( $local_time < time() - 600 || $local_time > time() + 300 ) { |
||
182 | return new Jetpack_Error( 'invalid_signature', 'The timestamp is too old.' ); |
||
183 | } |
||
184 | |||
185 | if ( 12 < strlen( $nonce ) || preg_match( '/[^a-zA-Z0-9]/', $nonce ) ) { |
||
186 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'nonce' ) ); |
||
187 | } |
||
188 | |||
189 | $normalized_request_pieces = array( |
||
190 | $token, |
||
191 | $timestamp, |
||
192 | $nonce, |
||
193 | $body_hash, |
||
194 | strtoupper( $method ), |
||
195 | strtolower( $parsed['host'] ), |
||
196 | $port, |
||
197 | $parsed['path'], |
||
198 | // Normalized Query String |
||
199 | ); |
||
200 | |||
201 | $normalized_request_pieces = array_merge( $normalized_request_pieces, $this->normalized_query_parameters( isset( $parsed['query'] ) ? $parsed['query'] : '' ) ); |
||
202 | $flat_normalized_request_pieces = array(); |
||
203 | foreach ($normalized_request_pieces as $piece) { |
||
204 | if ( is_array( $piece ) ) { |
||
205 | foreach ( $piece as $subpiece ) { |
||
206 | $flat_normalized_request_pieces[] = $subpiece; |
||
207 | } |
||
208 | } else { |
||
209 | $flat_normalized_request_pieces[] = $piece; |
||
210 | } |
||
211 | } |
||
212 | $normalized_request_pieces = $flat_normalized_request_pieces; |
||
213 | |||
214 | $normalized_request_string = join( "\n", $normalized_request_pieces ) . "\n"; |
||
215 | |||
216 | return base64_encode( hash_hmac( 'sha1', $normalized_request_string, $this->secret, true ) ); |
||
217 | } |
||
218 | |||
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: