| Conditions | 17 |
| Paths | 324 |
| Total Lines | 74 |
| Code Lines | 38 |
| 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 |
||
| 15 | function resize( $original_image_src, $args, $thumbnail_object ) { |
||
| 16 | global $current_foogallery; |
||
|
|
|||
| 17 | global $foogallery_last_generated_thumb_url; |
||
| 18 | |||
| 19 | $arg_defaults = array( |
||
| 20 | 'width' => 0, |
||
| 21 | 'height' => 0, |
||
| 22 | 'crop' => true, |
||
| 23 | 'jpeg_quality' => foogallery_thumbnail_jpeg_quality(), |
||
| 24 | 'thumb_resize_animations' => foogallery_get_setting( 'thumb_resize_animations' ), |
||
| 25 | 'foogallery_attachment_id'=> $thumbnail_object->ID |
||
| 26 | ); |
||
| 27 | |||
| 28 | if ( isset( $current_foogallery ) ) { |
||
| 29 | $arg_defaults['foogallery_id'] = $current_foogallery->ID; |
||
| 30 | } |
||
| 31 | |||
| 32 | $args = wp_parse_args( $args, $arg_defaults ); |
||
| 33 | |||
| 34 | //allow for plugins to change the thumbnail creation args |
||
| 35 | $args = apply_filters( 'foogallery_thumbnail_resize_args', $args, $original_image_src, $thumbnail_object ); |
||
| 36 | |||
| 37 | $width = (int)$args['width']; |
||
| 38 | $height = (int)$args['height']; |
||
| 39 | $crop = (bool)$args['crop']; |
||
| 40 | |||
| 41 | //we can force the use of the originally uploaded full-size image |
||
| 42 | $force_use_original_image = isset( $args['force_use_original_image'] ) && true === $args['force_use_original_image']; |
||
| 43 | |||
| 44 | if ( $force_use_original_image ) { |
||
| 45 | $fullsize = wp_get_attachment_image_src( $thumbnail_object->ID, 'fullsize' ); |
||
| 46 | |||
| 47 | return $fullsize[0]; |
||
| 48 | } |
||
| 49 | |||
| 50 | //we can force the use of the original WP icon or WP-generated thumb by passing through args individually |
||
| 51 | $force_use_original_thumb = isset( $args['force_use_original_thumb'] ) && true === $args['force_use_original_thumb']; |
||
| 52 | |||
| 53 | if ( $force_use_original_thumb ) { |
||
| 54 | $thumbnail_icon = wp_get_attachment_image_src( $thumbnail_object->ID, array( $width, $height ) ); |
||
| 55 | |||
| 56 | return $thumbnail_icon[0]; |
||
| 57 | } |
||
| 58 | |||
| 59 | //we can force the use of original WP thumbs by passing through args individually, or by saved settings |
||
| 60 | $use_original_thumbs = ( isset( $args['use_original_thumbs'] ) && true === $args['use_original_thumbs'] ) || 'on' === foogallery_get_setting( 'use_original_thumbs' ); |
||
| 61 | |||
| 62 | if ( $use_original_thumbs ) { |
||
| 63 | //check if we are trying to get back the default thumbnail that we already have |
||
| 64 | if ( $thumbnail_object->ID > 0 && $width == get_option( 'thumbnail_size_w' ) && $height == get_option( 'thumbnail_size_h' ) && $crop == get_option( 'thumbnail_crop' ) ) { |
||
| 65 | $thumbnail_attributes = wp_get_attachment_image_src( $thumbnail_object->ID ); |
||
| 66 | |||
| 67 | return $thumbnail_attributes[0]; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | if ( $thumbnail_object->ID > 0 ) { |
||
| 72 | $crop_from_position = get_post_meta( $thumbnail_object->ID, 'wpthumb_crop_pos', true ); |
||
| 73 | |||
| 74 | if ( !empty( $crop_from_position ) ) { |
||
| 75 | $args['crop_from_position'] = $crop_from_position; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | //remove invalid resize args |
||
| 80 | if ( array_key_exists( 'height', $args ) && 0 === $args['height'] ) { |
||
| 81 | unset( $args['height'] ); |
||
| 82 | } |
||
| 83 | |||
| 84 | //save the generated thumb url to a global so that we can use it later if needed |
||
| 85 | $foogallery_last_generated_thumb_url = wpthumb( $original_image_src, $args ); |
||
| 86 | |||
| 87 | return $foogallery_last_generated_thumb_url; |
||
| 88 | } |
||
| 89 | |||
| 123 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state