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 |
||
98 | function get_token( $data ) { |
||
99 | $roles = new Roles(); |
||
100 | $role = $roles->translate_current_user_to_role(); |
||
101 | |||
102 | if ( ! $role ) { |
||
103 | return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) ); |
||
104 | } |
||
105 | |||
106 | $client_secret = Jetpack_Data::get_access_token(); |
||
107 | if ( ! $client_secret ) { |
||
108 | return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) ); |
||
109 | } |
||
110 | |||
111 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
112 | $redirect_uri = ( 'calypso' === $data['auth_type'] ) |
||
113 | ? $data['redirect_uri'] |
||
114 | : add_query_arg( |
||
115 | array( |
||
116 | 'action' => 'authorize', |
||
117 | '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
||
118 | 'redirect' => $redirect ? urlencode( $redirect ) : false, |
||
119 | ), |
||
120 | menu_page_url( 'jetpack', false ) |
||
121 | ); |
||
122 | |||
123 | // inject identity for analytics |
||
124 | $tracks = new Automattic\Jetpack\Tracking(); |
||
125 | $tracks_identity = $tracks->tracks_get_identity( get_current_user_id() ); |
||
126 | |||
127 | $body = array( |
||
128 | 'client_id' => Jetpack_Options::get_option( 'id' ), |
||
129 | 'client_secret' => $client_secret->secret, |
||
130 | 'grant_type' => 'authorization_code', |
||
131 | 'code' => $data['code'], |
||
132 | 'redirect_uri' => $redirect_uri, |
||
133 | '_ui' => $tracks_identity['_ui'], |
||
134 | '_ut' => $tracks_identity['_ut'], |
||
135 | ); |
||
136 | |||
137 | $args = array( |
||
138 | 'method' => 'POST', |
||
139 | 'body' => $body, |
||
140 | 'headers' => array( |
||
141 | 'Accept' => 'application/json', |
||
142 | ), |
||
143 | ); |
||
144 | $response = Client::_wp_remote_request( Connection_Utils::fix_url_for_bad_hosts( Jetpack::connection()->api_url( 'token' ) ), $args ); |
||
145 | |||
146 | if ( is_wp_error( $response ) ) { |
||
147 | return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() ); |
||
148 | } |
||
149 | |||
150 | $code = wp_remote_retrieve_response_code( $response ); |
||
151 | $entity = wp_remote_retrieve_body( $response ); |
||
152 | |||
153 | if ( $entity ) { |
||
154 | $json = json_decode( $entity ); |
||
155 | } else { |
||
156 | $json = false; |
||
157 | } |
||
158 | |||
159 | if ( 200 != $code || ! empty( $json->error ) ) { |
||
160 | if ( empty( $json->error ) ) { |
||
161 | return new Jetpack_Error( 'unknown', '', $code ); |
||
162 | } |
||
163 | |||
164 | $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : ''; |
||
165 | |||
166 | return new Jetpack_Error( (string) $json->error, $error_description, $code ); |
||
167 | } |
||
168 | |||
169 | if ( empty( $json->access_token ) || ! is_scalar( $json->access_token ) ) { |
||
170 | return new Jetpack_Error( 'access_token', '', $code ); |
||
171 | } |
||
172 | |||
173 | if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) { |
||
174 | return new Jetpack_Error( 'token_type', '', $code ); |
||
175 | } |
||
176 | |||
177 | if ( empty( $json->scope ) ) { |
||
178 | return new Jetpack_Error( 'scope', 'No Scope', $code ); |
||
179 | } |
||
180 | |||
181 | @list( $role, $hmac ) = explode( ':', $json->scope ); |
||
182 | if ( empty( $role ) || empty( $hmac ) ) { |
||
183 | return new Jetpack_Error( 'scope', 'Malformed Scope', $code ); |
||
184 | } |
||
185 | |||
186 | if ( Jetpack::connection()->sign_role( $role ) !== $json->scope ) { |
||
187 | return new Jetpack_Error( 'scope', 'Invalid Scope', $code ); |
||
188 | } |
||
189 | |||
190 | $cap = $roles->translate_role_to_cap( $role ); |
||
191 | if ( ! $cap ) { |
||
192 | return new Jetpack_Error( 'scope', 'No Cap', $code ); |
||
193 | } |
||
194 | |||
195 | if ( ! current_user_can( $cap ) ) { |
||
196 | return new Jetpack_Error( 'scope', 'current_user_cannot', $code ); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Fires after user has successfully received an auth token. |
||
201 | * |
||
202 | * @since 3.9.0 |
||
203 | */ |
||
204 | do_action( 'jetpack_user_authorized' ); |
||
205 | |||
206 | return (string) $json->access_token; |
||
207 | } |
||
208 | |||
217 |
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.