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 ( e1c9dd...dff668 )
by Brad
02:14
created

FooGallery_Thumbnails::resize()   F

Complexity

Conditions 17
Paths 324

Size

Total Lines 79
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 41
nc 324
nop 3
dl 0
loc 79
rs 3.8585
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
			add_filter( 'foogallery_thumbnail_resize_args', array( $this, 'check_for_force_original_thumb') );
15
		}
16
17
		function check_for_force_original_thumb( $args ){
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...
18
			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...
19
20
			if ( isset( $current_foogallery ) ) {
21
				$args['force_use_original_thumb'] = $current_foogallery->force_use_original_thumbs;
22
			}
23
24
			return $args;
25
		}
26
27
		function resize( $original_image_src, $args, $thumbnail_object ) {
28
			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...
29
		    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...
30
31
			$arg_defaults = array(
32
				'width'                   => 0,
33
				'height'                  => 0,
34
				'crop'                    => true,
35
				'jpeg_quality'            => foogallery_thumbnail_jpeg_quality(),
36
				'thumb_resize_animations' => foogallery_get_setting( 'thumb_resize_animations' ),
37
				'foogallery_attachment_id'=> $thumbnail_object->ID
38
			);
39
40
			if ( isset( $current_foogallery ) ) {
41
				$arg_defaults['foogallery_id'] = $current_foogallery->ID;
42
			}
43
44
			$args = wp_parse_args( $args, $arg_defaults );
45
46
			//allow for plugins to change the thumbnail creation args
47
			$args = apply_filters( 'foogallery_thumbnail_resize_args', $args, $original_image_src, $thumbnail_object );
48
49
			$width  = (int)$args['width'];
50
			$height = (int)$args['height'];
51
			$crop   = (bool)$args['crop'];
52
53
			//we can force the use of the originally uploaded full-size image
54
			$force_use_original_image = isset( $args['force_use_original_image'] ) && true === $args['force_use_original_image'];
55
56
			if ( $force_use_original_image ) {
57
				$fullsize = wp_get_attachment_image_src( $thumbnail_object->ID, 'fullsize' );
58
59
				return $fullsize[0];
60
			}
61
62
			//we can force the use of the original WP icon or WP-generated thumb by passing through args individually
63
			$force_use_original_thumb = isset( $args['force_use_original_thumb'] ) && true === $args['force_use_original_thumb'];
64
65
			if ( $force_use_original_thumb ) {
66
				$thumbnail_icon = wp_get_attachment_image_src( $thumbnail_object->ID, array( $width, $height ) );
67
68
				return $thumbnail_icon[0];
69
			}
70
71
			//we can force the use of original WP thumbs by passing through args individually, or by saved settings
72
			$use_original_thumbs = ( isset( $args['use_original_thumbs'] ) && true === $args['use_original_thumbs'] ) || 'on' === foogallery_get_setting( 'use_original_thumbs' );
73
74
			if ( $use_original_thumbs ) {
75
76
				$option_thumbnail_size_w = get_option( 'thumbnail_size_w' );
77
				$option_thumbnail_size_h = get_option( 'thumbnail_size_h' );
78
				$option_thumbnail_crop = get_option( 'thumbnail_crop' );
79
80
				//check if we are trying to get back the default thumbnail that we already have
81
				if ( $thumbnail_object->ID > 0 && $width == $option_thumbnail_size_w && $height == $option_thumbnail_size_h && $crop == $option_thumbnail_crop ) {
82
					$thumbnail_attributes = wp_get_attachment_image_src( $thumbnail_object->ID );
83
84
					return $thumbnail_attributes[0];
85
				}
86
			}
87
88
			if ( $thumbnail_object->ID > 0 ) {
89
				$crop_from_position = get_post_meta( $thumbnail_object->ID, 'wpthumb_crop_pos', true );
90
91
				if ( !empty( $crop_from_position ) ) {
92
					$args['crop_from_position'] = $crop_from_position;
93
				}
94
			}
95
96
			//remove invalid resize args
97
			if ( array_key_exists( 'height', $args ) && 0 === $args['height'] ) {
98
				unset( $args['height'] );
99
			}
100
101
			//save the generated thumb url to a global so that we can use it later if needed
102
            $foogallery_last_generated_thumb_url = wpthumb( $original_image_src, $args );
103
104
            return $foogallery_last_generated_thumb_url;
105
		}
106
107
		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...
108
            $test_image_url = foogallery_test_thumb_url();
109
110
			//next, generate a thumbnail
111
			$test_args = array(
112
				'width'                   => 20,
113
				'height'                  => 20,
114
				'crop'                    => true,
115
				'jpeg_quality'            => foogallery_thumbnail_jpeg_quality()
116
			);
117
118
            //first, clear any previous cached files
119
            $thumb = new WP_Thumb( $test_image_url, $test_args );
120
            wpthumb_rmdir_recursive( $thumb->getCacheFileDirectory() );
121
122
			$test_thumb = new WP_Thumb( $test_image_url, $test_args );
123
            $generated_thumb = $test_thumb->returnImage();
124
            $success = $test_image_url !== $generated_thumb;
125
			$file_info = wp_check_filetype( $test_image_url );
126
127
			$test_results = array(
128
			    'success' => $success,
129
				'thumb' => $generated_thumb,
130
				'error' => $test_thumb->errored() ? $test_thumb->error : '',
131
				'file_info' => $file_info
132
			);
133
134
            do_action( 'foogallery_thumbnail_generation_test', $test_results );
135
136
            return $test_results;
137
		}
138
	}
139
}
140