Conditions | 22 |
Paths | 94 |
Total Lines | 99 |
Code Lines | 57 |
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 |
||
163 | function get_token( $data ) { |
||
164 | $role = Jetpack::translate_current_user_to_role(); |
||
165 | |||
166 | if ( ! $role ) { |
||
167 | return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) ); |
||
168 | } |
||
169 | |||
170 | $client_secret = Jetpack_Data::get_access_token(); |
||
171 | if ( ! $client_secret ) { |
||
172 | return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) ); |
||
173 | } |
||
174 | |||
175 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
176 | $redirect_uri = ( 'calypso' === $data['auth_type'] ) |
||
177 | ? $data['redirect_uri'] |
||
178 | : add_query_arg( array( |
||
179 | 'action' => 'authorize', |
||
180 | '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
||
181 | 'redirect' => $redirect ? urlencode( $redirect ) : false, |
||
182 | ), menu_page_url( 'jetpack', false ) ); |
||
183 | |||
184 | $body = array( |
||
185 | 'client_id' => Jetpack_Options::get_option( 'id' ), |
||
186 | 'client_secret' => $client_secret->secret, |
||
187 | 'grant_type' => 'authorization_code', |
||
188 | 'code' => $data['code'], |
||
189 | 'redirect_uri' => $redirect_uri, |
||
190 | ); |
||
191 | |||
192 | $args = array( |
||
193 | 'method' => 'POST', |
||
194 | 'body' => $body, |
||
195 | 'headers' => array( |
||
196 | 'Accept' => 'application/json', |
||
197 | ), |
||
198 | ); |
||
199 | $response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'token' ) ), $args ); |
||
200 | |||
201 | if ( is_wp_error( $response ) ) { |
||
202 | return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() ); |
||
203 | } |
||
204 | |||
205 | $code = wp_remote_retrieve_response_code( $response ); |
||
206 | $entity = wp_remote_retrieve_body( $response ); |
||
207 | |||
208 | if ( $entity ) { |
||
209 | $json = json_decode( $entity ); |
||
210 | } else { |
||
211 | $json = false; |
||
212 | } |
||
213 | |||
214 | if ( 200 != $code || ! empty( $json->error ) ) { |
||
215 | if ( empty( $json->error ) ) { |
||
216 | return new Jetpack_Error( 'unknown', '', $code ); |
||
217 | } |
||
218 | |||
219 | $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : ''; |
||
220 | |||
221 | return new Jetpack_Error( (string) $json->error, $error_description, $code ); |
||
222 | } |
||
223 | |||
224 | if ( empty( $json->access_token ) || ! is_scalar( $json->access_token ) ) { |
||
225 | return new Jetpack_Error( 'access_token', '', $code ); |
||
226 | } |
||
227 | |||
228 | if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) { |
||
229 | return new Jetpack_Error( 'token_type', '', $code ); |
||
230 | } |
||
231 | |||
232 | if ( empty( $json->scope ) ) { |
||
233 | return new Jetpack_Error( 'scope', 'No Scope', $code ); |
||
234 | } |
||
235 | |||
236 | @list( $role, $hmac ) = explode( ':', $json->scope ); |
||
237 | if ( empty( $role ) || empty( $hmac ) ) { |
||
238 | return new Jetpack_Error( 'scope', 'Malformed Scope', $code ); |
||
239 | } |
||
240 | |||
241 | if ( Jetpack::sign_role( $role ) !== $json->scope ) { |
||
242 | return new Jetpack_Error( 'scope', 'Invalid Scope', $code ); |
||
243 | } |
||
244 | |||
245 | if ( ! $cap = Jetpack::translate_role_to_cap( $role ) ) { |
||
246 | return new Jetpack_Error( 'scope', 'No Cap', $code ); |
||
247 | } |
||
248 | |||
249 | if ( ! current_user_can( $cap ) ) { |
||
250 | return new Jetpack_Error( 'scope', 'current_user_cannot', $code ); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Fires after user has successfully received an auth token. |
||
255 | * |
||
256 | * @since 3.9.0 |
||
257 | */ |
||
258 | do_action( 'jetpack_user_authorized' ); |
||
259 | |||
260 | return (string) $json->access_token; |
||
261 | } |
||
262 | |||
279 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.