GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 3bd383...ce1d31 )
by Brad
02:29
created

FooGallery_Thumbnails::resize()   D

Complexity

Conditions 17
Paths 324

Size

Total Lines 74
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 38
nc 324
nop 3
dl 0
loc 74
rs 4.0369
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/*
3
 * FooGallery Thumbnail Resizing class
4
 */
5
6
if ( !class_exists( 'FooGallery_Thumbnails' ) ) {
7
8
	class FooGallery_Thumbnails {
9
10
		function __construct() {
11
			//generate thumbs using WPThumb
12
			add_filter( 'foogallery_attachment_resize_thumbnail', array( $this, 'resize' ), 10, 3 );
13
		}
14
15
		function resize( $original_image_src, $args, $thumbnail_object ) {
16
			global $current_foogallery;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
17
		    global $foogallery_last_generated_thumb_url;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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
90
		function run_thumbnail_generation_tests() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
            $test_image_url = foogallery_test_thumb_url();
92
93
			//next, generate a thumbnail
94
			$test_args = array(
95
				'width'                   => 20,
96
				'height'                  => 20,
97
				'crop'                    => true,
98
				'jpeg_quality'            => foogallery_thumbnail_jpeg_quality()
99
			);
100
101
            //first, clear any previous cached files
102
            $thumb = new WP_Thumb( $test_image_url, $test_args );
103
            wpthumb_rmdir_recursive( $thumb->getCacheFileDirectory() );
104
105
			$test_thumb = new WP_Thumb( $test_image_url, $test_args );
106
            $generated_thumb = $test_thumb->returnImage();
107
            $success = $test_image_url !== $generated_thumb;
108
			$file_info = wp_check_filetype( $test_image_url );
109
110
			$test_results = array(
111
			    'success' => $success,
112
				'thumb' => $generated_thumb,
113
				'error' => $test_thumb->errored() ? $test_thumb->error : '',
114
				'file_info' => $file_info
115
			);
116
117
            do_action( 'foogallery_thumbnail_generation_test', $test_results );
118
119
            return $test_results;
120
		}
121
	}
122
}
123