Conditions | 74 |
Paths | 7909 |
Total Lines | 240 |
Code Lines | 137 |
Lines | 14 |
Ratio | 5.83 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
257 | public function update_settings() { |
||
258 | |||
259 | // $this->input() retrieves posted arguments whitelisted and casted to the $request_format |
||
260 | // specs that get passed in when this class is instantiated |
||
261 | /** |
||
262 | * Filters the settings to be updated on the site. |
||
263 | * |
||
264 | * @module json-api |
||
265 | * |
||
266 | * @since 3.6.0 |
||
267 | * |
||
268 | * @param array $input Associative array of site settings to be updated. |
||
269 | */ |
||
270 | $input = apply_filters( 'rest_api_update_site_settings', $this->input() ); |
||
271 | |||
272 | $jetpack_relatedposts_options = array(); |
||
273 | $sharing_options = array(); |
||
274 | $updated = array(); |
||
275 | |||
276 | foreach ( $input as $key => $value ) { |
||
277 | |||
278 | if ( ! is_array( $value ) ) { |
||
279 | $value = trim( $value ); |
||
280 | } |
||
281 | $value = wp_unslash( $value ); |
||
282 | |||
283 | switch ( $key ) { |
||
284 | |||
285 | case 'default_ping_status': |
||
286 | case 'default_comment_status': |
||
287 | // settings are stored as closed|open |
||
288 | $coerce_value = ( $value ) ? 'open' : 'closed'; |
||
289 | if ( update_option( $key, $coerce_value ) ) { |
||
290 | $updated[ $key ] = $value; |
||
291 | }; |
||
292 | break; |
||
293 | case 'jetpack_protect_whitelist': |
||
294 | if ( function_exists( 'jetpack_protect_save_whitelist' ) ) { |
||
295 | $result = jetpack_protect_save_whitelist( $value ); |
||
296 | if ( is_wp_error( $result ) ) { |
||
297 | return $result; |
||
298 | } |
||
299 | $updated[ $key ] = jetpack_protect_format_whitelist(); |
||
300 | } |
||
301 | break; |
||
302 | case 'jetpack_sync_non_public_post_stati': |
||
303 | Jetpack_Options::update_option( 'sync_non_public_post_stati', $value ); |
||
304 | break; |
||
305 | case 'jetpack_relatedposts_enabled': |
||
306 | case 'jetpack_relatedposts_show_thumbnails': |
||
307 | case 'jetpack_relatedposts_show_headline': |
||
308 | if ( ! $this->jetpack_relatedposts_supported() ) { |
||
309 | break; |
||
310 | } |
||
311 | if ( 'jetpack_relatedposts_enabled' === $key && method_exists( 'Jetpack', 'is_module_active' ) && $this->jetpack_relatedposts_supported() ) { |
||
312 | $before_action = Jetpack::is_module_active('related-posts'); |
||
313 | if ( $value ) { |
||
314 | Jetpack::activate_module( 'related-posts', false, false ); |
||
315 | } else { |
||
316 | Jetpack::deactivate_module( 'related-posts' ); |
||
317 | } |
||
318 | $after_action = Jetpack::is_module_active('related-posts'); |
||
319 | if ( $after_action == $before_action ) { |
||
320 | break; |
||
321 | } |
||
322 | } |
||
323 | $just_the_key = substr( $key, 21 ); |
||
324 | $jetpack_relatedposts_options[ $just_the_key ] = $value; |
||
325 | break; |
||
326 | |||
327 | case 'social_notifications_like': |
||
328 | case 'social_notifications_reblog': |
||
329 | case 'social_notifications_subscribe': |
||
330 | // settings are stored as on|off |
||
331 | $coerce_value = ( $value ) ? 'on' : 'off'; |
||
332 | if ( update_option( $key, $coerce_value ) ) { |
||
333 | $updated[ $key ] = $value; |
||
334 | } |
||
335 | break; |
||
336 | case 'wga': |
||
337 | if ( ! isset( $value['code'] ) || ! preg_match( '/^$|^UA-[\d-]+$/i', $value['code'] ) ) { |
||
338 | return new WP_Error( 'invalid_code', 'Invalid UA ID' ); |
||
339 | } |
||
340 | $wga = get_option( 'wga', array() ); |
||
341 | $wga['code'] = $value['code']; // maintain compatibility with wp-google-analytics |
||
342 | if ( update_option( 'wga', $wga ) ) { |
||
343 | $updated[ $key ] = $value; |
||
344 | } |
||
345 | |||
346 | $enabled_or_disabled = $wga['code'] ? 'enabled' : 'disabled'; |
||
347 | |||
348 | /** This action is documented in modules/widgets/social-media-icons.php */ |
||
349 | do_action( 'jetpack_bump_stats_extras', 'google-analytics', $enabled_or_disabled ); |
||
350 | |||
351 | $business_plugins = WPCOM_Business_Plugins::instance(); |
||
352 | $business_plugins->activate_plugin( 'wp-google-analytics' ); |
||
353 | break; |
||
354 | |||
355 | case 'jetpack_testimonial': |
||
356 | case 'jetpack_portfolio': |
||
357 | View Code Duplication | case 'jetpack_comment_likes_enabled': |
|
358 | // settings are stored as 1|0 |
||
359 | $coerce_value = (int) $value; |
||
360 | if ( update_option( $key, $coerce_value ) ) { |
||
361 | $updated[ $key ] = (bool) $value; |
||
362 | } |
||
363 | break; |
||
364 | |||
365 | case 'jetpack_testimonial_posts_per_page': |
||
366 | View Code Duplication | case 'jetpack_portfolio_posts_per_page': |
|
367 | // settings are stored as numeric |
||
368 | $coerce_value = (int) $value; |
||
369 | if ( update_option( $key, $coerce_value ) ) { |
||
370 | $updated[ $key ] = $coerce_value; |
||
371 | } |
||
372 | break; |
||
373 | |||
374 | // Sharing options |
||
375 | case 'sharing_button_style': |
||
376 | case 'sharing_show': |
||
377 | case 'sharing_open_links': |
||
378 | $sharing_options[ preg_replace( '/^sharing_/', '', $key ) ] = $value; |
||
379 | break; |
||
380 | case 'sharing_label': |
||
381 | $sharing_options[ $key ] = $value; |
||
382 | break; |
||
383 | |||
384 | // Keyring token option |
||
385 | case 'eventbrite_api_token': |
||
386 | // These options can only be updated for sites hosted on WordPress.com |
||
387 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
388 | if ( empty( $value ) || WPCOM_JSON_API::is_falsy( $value ) ) { |
||
389 | if ( delete_option( $key ) ) { |
||
390 | $updated[ $key ] = null; |
||
391 | } |
||
392 | } else if ( update_option( $key, $value ) ) { |
||
393 | $updated[ $key ] = (int) $value; |
||
394 | } |
||
395 | } |
||
396 | break; |
||
397 | |||
398 | case 'holidaysnow': |
||
399 | if ( empty( $value ) || WPCOM_JSON_API::is_falsy( $value ) ) { |
||
400 | if ( function_exists( 'jetpack_holiday_snow_option_name' ) && delete_option( jetpack_holiday_snow_option_name() ) ) { |
||
401 | $updated[ $key ] = false; |
||
402 | } |
||
403 | } else if ( function_exists( 'jetpack_holiday_snow_option_name' ) && update_option( jetpack_holiday_snow_option_name(), 'letitsnow' ) ) { |
||
404 | $updated[ $key ] = true; |
||
405 | } |
||
406 | break; |
||
407 | |||
408 | case 'timezone_string': |
||
409 | // Map UTC+- timezones to gmt_offsets and set timezone_string to empty |
||
410 | // https://github.com/WordPress/WordPress/blob/4.4.2/wp-admin/options.php#L175 |
||
411 | if ( ! empty( $value ) && preg_match( '/^UTC[+-]/', $value ) ) { |
||
412 | $gmt_offset = preg_replace( '/UTC\+?/', '', $value ); |
||
413 | if ( update_option( 'gmt_offset', $gmt_offset ) ) { |
||
414 | $updated[ 'gmt_offset' ] = $gmt_offset; |
||
415 | } |
||
416 | |||
417 | $value = ''; |
||
418 | } |
||
419 | |||
420 | // Always set timezone_string either with the given value or with an |
||
421 | // empty string |
||
422 | if ( update_option( $key, $value ) ) { |
||
423 | $updated[ $key ] = $value; |
||
424 | } |
||
425 | break; |
||
426 | |||
427 | default: |
||
428 | //allow future versions of this endpoint to support additional settings keys |
||
429 | if ( has_filter( 'site_settings_endpoint_update_' . $key ) ) { |
||
430 | /** |
||
431 | * Filter current site setting value to be updated. |
||
432 | * |
||
433 | * @module json-api |
||
434 | * |
||
435 | * @since 3.9.3 |
||
436 | * |
||
437 | * @param mixed $response_item A single site setting value. |
||
438 | */ |
||
439 | $value = apply_filters( 'site_settings_endpoint_update_' . $key, $value ); |
||
440 | $updated[ $key ] = $value; |
||
441 | continue; |
||
442 | } |
||
443 | |||
444 | // no worries, we've already whitelisted and casted arguments above |
||
445 | if ( update_option( $key, $value ) ) { |
||
446 | $updated[ $key ] = $value; |
||
447 | } |
||
448 | } |
||
449 | } |
||
450 | |||
451 | if ( count( $jetpack_relatedposts_options ) ) { |
||
452 | // track new jetpack_relatedposts options against old |
||
453 | $old_relatedposts_options = Jetpack_Options::get_option( 'relatedposts' ); |
||
454 | if ( Jetpack_Options::update_option( 'relatedposts', $jetpack_relatedposts_options ) ) { |
||
455 | foreach( $jetpack_relatedposts_options as $key => $value ) { |
||
456 | if ( $value !== $old_relatedposts_options[ $key ] ) { |
||
457 | $updated[ 'jetpack_relatedposts_' . $key ] = $value; |
||
458 | } |
||
459 | } |
||
460 | } |
||
461 | } |
||
462 | |||
463 | if ( ! empty( $sharing_options ) && class_exists( 'Sharing_Service' ) ) { |
||
464 | $ss = new Sharing_Service(); |
||
465 | |||
466 | // Merge current values with updated, since Sharing_Service expects |
||
467 | // all values to be included when updating |
||
468 | $current_sharing_options = $ss->get_global_options(); |
||
469 | foreach ( $current_sharing_options as $key => $val ) { |
||
470 | if ( ! isset( $sharing_options[ $key ] ) ) { |
||
471 | $sharing_options[ $key ] = $val; |
||
472 | } |
||
473 | } |
||
474 | |||
475 | $updated_social_options = $ss->set_global_options( $sharing_options ); |
||
476 | |||
477 | if ( isset( $input['sharing_button_style'] ) ) { |
||
478 | $updated['sharing_button_style'] = (string) $updated_social_options['button_style']; |
||
479 | } |
||
480 | if ( isset( $input['sharing_label'] ) ) { |
||
481 | // Sharing_Service won't report label as updated if set to default |
||
482 | $updated['sharing_label'] = (string) $sharing_options['sharing_label']; |
||
483 | } |
||
484 | if ( isset( $input['sharing_show'] ) ) { |
||
485 | $updated['sharing_show'] = (array) $updated_social_options['show']; |
||
486 | } |
||
487 | if ( isset( $input['sharing_open_links'] ) ) { |
||
488 | $updated['sharing_open_links'] = (string) $updated_social_options['open_links']; |
||
489 | } |
||
490 | } |
||
491 | |||
492 | return array( |
||
493 | 'updated' => $updated |
||
494 | ); |
||
495 | |||
496 | } |
||
497 | } |
||
498 |
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.