Conditions | 11 |
Paths | 36 |
Total Lines | 107 |
Lines | 39 |
Ratio | 36.45 % |
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 // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
224 | public function instant_api( array $args ) { |
||
225 | $endpoint = sprintf( '/sites/%s/search', $this->jetpack_blog_id ); |
||
226 | $service_url = 'https://public-api.wordpress.com/rest/v1.3' . $endpoint; |
||
227 | |||
228 | $do_authenticated_request = false; |
||
229 | View Code Duplication | if ( class_exists( 'Automattic\\Jetpack\\Connection\\Client' ) && |
|
230 | isset( $args['authenticated_request'] ) && |
||
231 | true === $args['authenticated_request'] ) { |
||
232 | $do_authenticated_request = true; |
||
233 | } |
||
234 | |||
235 | unset( $args['authenticated_request'] ); |
||
236 | |||
237 | $request_args = array( |
||
238 | 'headers' => array( |
||
239 | 'Content-Type' => 'application/json', |
||
240 | ), |
||
241 | 'timeout' => 10, |
||
242 | 'user-agent' => 'jetpack_search', |
||
243 | ); |
||
244 | |||
245 | $request_body = wp_json_encode( $args ); |
||
246 | |||
247 | $start_time = microtime( true ); |
||
248 | |||
249 | View Code Duplication | if ( $do_authenticated_request ) { |
|
250 | $request_args['method'] = 'POST'; |
||
251 | |||
252 | $request = Client::wpcom_json_api_request_as_blog( $endpoint, Client::WPCOM_JSON_API_VERSION, $request_args, $request_body ); |
||
253 | } else { |
||
254 | $request_args = array_merge( |
||
255 | $request_args, |
||
256 | array( |
||
257 | 'body' => $request_body, |
||
258 | ) |
||
259 | ); |
||
260 | |||
261 | $request = wp_remote_post( $service_url, $request_args ); |
||
262 | } |
||
263 | |||
264 | $end_time = microtime( true ); |
||
265 | |||
266 | if ( is_wp_error( $request ) ) { |
||
267 | return $request; |
||
268 | } |
||
269 | |||
270 | $response_code = wp_remote_retrieve_response_code( $request ); |
||
271 | |||
272 | $response = json_decode( wp_remote_retrieve_body( $request ), true ); |
||
273 | |||
274 | $took = is_array( $response ) && ! empty( $response['took'] ) |
||
275 | ? $response['took'] |
||
276 | : null; |
||
277 | |||
278 | $query = array( |
||
279 | 'args' => $args, |
||
280 | 'response' => $response, |
||
281 | 'response_code' => $response_code, |
||
282 | 'elapsed_time' => ( $end_time - $start_time ) * 1000, // Convert from float seconds to ms. |
||
283 | 'es_time' => $took, |
||
284 | 'url' => $service_url, |
||
285 | ); |
||
286 | |||
287 | /** |
||
288 | * Fires after a search request has been performed. |
||
289 | * |
||
290 | * Includes the following info in the $query parameter: |
||
291 | * |
||
292 | * array args Array of Elasticsearch arguments for the search |
||
293 | * array response Raw API response, JSON decoded |
||
294 | * int response_code HTTP response code of the request |
||
295 | * float elapsed_time Roundtrip time of the search request, in milliseconds |
||
296 | * float es_time Amount of time Elasticsearch spent running the request, in milliseconds |
||
297 | * string url API url that was queried |
||
298 | * |
||
299 | * @module search |
||
300 | * |
||
301 | * @since 5.0.0 |
||
302 | * @since 5.8.0 This action now fires on all queries instead of just successful queries. |
||
303 | * |
||
304 | * @param array $query Array of information about the query performed |
||
305 | */ |
||
306 | do_action( 'did_jetpack_search_query', $query ); |
||
307 | |||
308 | View Code Duplication | if ( ! $response_code || $response_code < 200 || $response_code >= 300 ) { |
|
309 | /** |
||
310 | * Fires after a search query request has failed |
||
311 | * |
||
312 | * @module search |
||
313 | * |
||
314 | * @since 5.6.0 |
||
315 | * |
||
316 | * @param array Array containing the response code and response from the failed search query |
||
317 | */ |
||
318 | do_action( |
||
319 | 'failed_jetpack_search_query', |
||
320 | array( |
||
321 | 'response_code' => $response_code, |
||
322 | 'json' => $response, |
||
323 | ) |
||
324 | ); |
||
325 | |||
326 | return new WP_Error( 'invalid_search_api_response', 'Invalid response from API - ' . $response_code ); |
||
327 | } |
||
328 | |||
329 | return $response; |
||
330 | } |
||
331 | |||
335 |
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.