Conditions | 22 |
Paths | 94 |
Total Lines | 110 |
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 |
||
190 | function get_token( $data ) { |
||
191 | $roles = new Roles(); |
||
192 | $role = $roles->translate_current_user_to_role(); |
||
193 | |||
194 | if ( ! $role ) { |
||
195 | return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) ); |
||
196 | } |
||
197 | |||
198 | $client_secret = Jetpack_Data::get_access_token(); |
||
199 | if ( ! $client_secret ) { |
||
200 | return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) ); |
||
201 | } |
||
202 | |||
203 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
204 | $redirect_uri = ( 'calypso' === $data['auth_type'] ) |
||
205 | ? $data['redirect_uri'] |
||
206 | : add_query_arg( |
||
207 | array( |
||
208 | 'action' => 'authorize', |
||
209 | '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
||
210 | 'redirect' => $redirect ? urlencode( $redirect ) : false, |
||
211 | ), |
||
212 | menu_page_url( 'jetpack', false ) |
||
213 | ); |
||
214 | |||
215 | // inject identity for analytics |
||
216 | $tracks = new Automattic\Jetpack\Tracking(); |
||
217 | $tracks_identity = $tracks->tracks_get_identity( get_current_user_id() ); |
||
218 | |||
219 | $body = array( |
||
220 | 'client_id' => Jetpack_Options::get_option( 'id' ), |
||
221 | 'client_secret' => $client_secret->secret, |
||
222 | 'grant_type' => 'authorization_code', |
||
223 | 'code' => $data['code'], |
||
224 | 'redirect_uri' => $redirect_uri, |
||
225 | '_ui' => $tracks_identity['_ui'], |
||
226 | '_ut' => $tracks_identity['_ut'], |
||
227 | ); |
||
228 | |||
229 | $args = array( |
||
230 | 'method' => 'POST', |
||
231 | 'body' => $body, |
||
232 | 'headers' => array( |
||
233 | 'Accept' => 'application/json', |
||
234 | ), |
||
235 | ); |
||
236 | $response = Client::_wp_remote_request( Connection_Utils::fix_url_for_bad_hosts( Jetpack::connection()->api_url( 'token' ) ), $args ); |
||
237 | |||
238 | if ( is_wp_error( $response ) ) { |
||
239 | return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() ); |
||
240 | } |
||
241 | |||
242 | $code = wp_remote_retrieve_response_code( $response ); |
||
243 | $entity = wp_remote_retrieve_body( $response ); |
||
244 | |||
245 | if ( $entity ) { |
||
246 | $json = json_decode( $entity ); |
||
247 | } else { |
||
248 | $json = false; |
||
249 | } |
||
250 | |||
251 | if ( 200 != $code || ! empty( $json->error ) ) { |
||
252 | if ( empty( $json->error ) ) { |
||
253 | return new Jetpack_Error( 'unknown', '', $code ); |
||
254 | } |
||
255 | |||
256 | $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : ''; |
||
257 | |||
258 | return new Jetpack_Error( (string) $json->error, $error_description, $code ); |
||
259 | } |
||
260 | |||
261 | if ( empty( $json->access_token ) || ! is_scalar( $json->access_token ) ) { |
||
262 | return new Jetpack_Error( 'access_token', '', $code ); |
||
263 | } |
||
264 | |||
265 | if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) { |
||
266 | return new Jetpack_Error( 'token_type', '', $code ); |
||
267 | } |
||
268 | |||
269 | if ( empty( $json->scope ) ) { |
||
270 | return new Jetpack_Error( 'scope', 'No Scope', $code ); |
||
271 | } |
||
272 | |||
273 | @list( $role, $hmac ) = explode( ':', $json->scope ); |
||
274 | if ( empty( $role ) || empty( $hmac ) ) { |
||
275 | return new Jetpack_Error( 'scope', 'Malformed Scope', $code ); |
||
276 | } |
||
277 | |||
278 | if ( Jetpack::connection()->sign_role( $role ) !== $json->scope ) { |
||
279 | return new Jetpack_Error( 'scope', 'Invalid Scope', $code ); |
||
280 | } |
||
281 | |||
282 | $cap = $roles->translate_role_to_cap( $role ); |
||
283 | if ( ! $cap ) { |
||
284 | return new Jetpack_Error( 'scope', 'No Cap', $code ); |
||
285 | } |
||
286 | |||
287 | if ( ! current_user_can( $cap ) ) { |
||
288 | return new Jetpack_Error( 'scope', 'current_user_cannot', $code ); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Fires after user has successfully received an auth token. |
||
293 | * |
||
294 | * @since 3.9.0 |
||
295 | */ |
||
296 | do_action( 'jetpack_user_authorized' ); |
||
297 | |||
298 | return (string) $json->access_token; |
||
299 | } |
||
300 | |||
309 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.