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 — master ( 6e44b1...c642a0 )
by Brad
03:14
created

calculate_dimensions()   C

Complexity

Conditions 11
Paths 21

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 13
nc 21
nop 4
dl 0
loc 21
rs 6.4715
c 0
b 0
f 0

How to fix   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
 * Class to calculate thumb dimensions for a gallery. The default gallery templates
4
 *  require width and height attributes on the thumb img tags. In some cases these need to be
5
 *  calculated based on the aspect ratio of the individual thumbs.
6
 *
7
 * Date: 21/03/2017
8
 */
9
10
11
if ( ! class_exists( 'FooGallery_Thumbnail_Dimensions' ) ) {
12
13
    class FooGallery_Thumbnail_Dimensions
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
    {
15
        function __construct()
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...
16
        {
17
            //hook into the filter that build up the img attributes
18
            // and add the width and height attributes if the gallery requires them
19
            add_filter( 'foogallery_attachment_html_image_attributes', array( $this, 'include_thumb_dimension_attributes' ), 10, 3 );
20
21
            //calculate the thumbnail dimensions for all attachments in the gallery
22
            // for the specific gallery template that is being used.
23
            add_action( 'foogallery_located_template', array( $this, 'calculate_all_thumbnail_dimensions' ) );
24
        }
25
26
        /**
27
         * Helper function to check if the gallery requires thumbnail dimensions
28
         * @param $gallery_template string
29
         * @return bool
30
         */
31
        function does_gallery_template_use_thumbnail_dimensions( $gallery_template ) {
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...
32
            if ( empty( $gallery_template ) ) return false;
33
34
            //first do a check if the template needs thumbnail dimensions calculated
35
            $template_data = foogallery_get_gallery_template( $gallery_template );
36
37
            if ( $template_data && array_key_exists( 'thumbnail_dimensions', $template_data ) && true === $template_data['thumbnail_dimensions'] ) {
38
                //this template requires thumb dimensions to be provided
39
                return true;
40
            }
41
42
            return false;
43
        }
44
45
        function empty_dimensions( $thumbnail_dimensions ) {
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...
46
            if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) {
47
                $thumb_width = (int)$thumbnail_dimensions['width'];
48
                $thumb_height = (int)$thumbnail_dimensions['height'];
49
50
                return $thumb_width === 0 && $thumb_height === 0;
51
            }
52
            return true;
53
        }
54
55
        /**
56
         * Calculates all the thumbnail dimensions for the gallery
57
         * @param $foogallery FooGallery
58
         */
59
        function calculate_all_thumbnail_dimensions( $foogallery ) {
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...
60
            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...
61
            global $current_foogallery_template;
62
            global $current_foogallery_arguments;
63
64
            //check if we are dealing with a gallery. This check ensures this is not done for albums
65
            if ( isset( $current_foogallery ) && isset( $current_foogallery_template ) ) {
66
67
                //first do a check if the template needs thumbnail dimensions calculated
68
                if ($this->does_gallery_template_use_thumbnail_dimensions( $current_foogallery_template ) ) {
69
70
                    //load the thumbnail dimensions specific to the gallery, taking preference to arguments
71
                    $thumbnail_dimensions = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_template, array(), $current_foogallery_arguments );
72
73
                    //if we have no dimensions then load them from the gallery settings
74
                    if ( $this->empty_dimensions( $thumbnail_dimensions ) ) {
75
                        $thumbnail_dimensions = apply_filters( 'foogallery_template_thumbnail_dimensions-' . $current_foogallery_template, $thumbnail_dimensions, $current_foogallery );
76
                    }
77
78
                    if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) {
79
80
                        //$thumbnail_dimensions
81
                        $thumb_width = (int)$thumbnail_dimensions['width'];
82
                        $thumb_height = (int)$thumbnail_dimensions['height'];
83
                        $thumb_crop = (bool)$thumbnail_dimensions['crop'];
84
85
                        //set the appropriate arguments on the attachments so that they can be
86
                        // picked up and used in the 'include_thumb_dimension_attributes' function below
87
                        foreach ($foogallery->attachments() as $attachment) {
88
                            if ( $thumb_crop && $thumb_width > 0 && $thumb_height > 0 ) {
89
                                //we have set width and height and crop = true
90
                                //we do not need to calculate the dimensions
91
                                $calculated_thumb_width = $thumb_width;
92
                                $calculated_thumb_height = $thumb_height;
93
                            } else {
94
                                $size_array = image_resize_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height, $thumb_crop );
95
								if ( false !== $size_array ) {
96
									$calculated_thumb_width = $size_array[4];
97
									$calculated_thumb_height = $size_array[5];
98
								} else {
99
									$size_array = $this->calculate_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height );
100
									if ( false !== $size_array ) {
101
										$calculated_thumb_width = $size_array[0];
102
										$calculated_thumb_height = $size_array[1];
103
									} else {
104
										//fallback for when we cannot calculate dimensions. Use the originals
105
										$calculated_thumb_width = $attachment->width;
106
										$calculated_thumb_height = $attachment->height;
107
									}
108
								}
109
                            }
110
111
                            $attachment->has_thumbnail_dimensions = true;
112
                            $attachment->thumb_width = $calculated_thumb_width;
113
                            $attachment->thumb_height = $calculated_thumb_height;
114
                        }
115
                    }
116
                }
117
            }
118
        }
119
120
		/**
121
		 * Returns a resized width and height value given the original dimensions and then either the new width or height value, one can be 0.
122
		 *
123
		 * @param {int} $orig_width - The original width of the image.
0 ignored issues
show
Documentation introduced by
The doc-type {int} could not be parsed: Unknown type name "{int}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
124
		 * @param {int} $orig_height - The original height of the image.
0 ignored issues
show
Documentation introduced by
The doc-type {int} could not be parsed: Unknown type name "{int}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
125
		 * @param {int} $new_width - The new width for the image or 0 to calculate it from the supplied new height and original dimensions.
0 ignored issues
show
Documentation introduced by
The doc-type {int} could not be parsed: Unknown type name "{int}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
126
		 * @param {int} $new_height - The new height for the image or 0 to calculate it from the supplied new width and original dimensions.
0 ignored issues
show
Documentation introduced by
The doc-type {int} could not be parsed: Unknown type name "{int}" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
127
		 *
128
		 * @return array|false - Returns an array with the width value at index 0 and the height value at index 1.
129
		 * Returns false if the original dimensions are invalid or if both the new width and height are invalid.
130
		 *
131
		 * @example
132
		 *
133
		 * $size_1 = calculate_dimensions(800, 600, 0, 300); // => [400, 300]
134
		 *
135
		 * $size_2 = calculate_dimensions(800, 600, 400, 0); // => [400, 300]
136
		 */
137
		function calculate_dimensions($orig_width, $orig_height, $new_width, $new_height){
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...
138
			// if we have an invalid original size provided exit early and return false
139
			if (!is_numeric($orig_width) || $orig_width === 0 || !is_numeric($orig_height) || $orig_height === 0){
140
				return false;
141
			}
142
			$needs_width = !is_numeric($new_width) || $new_width === 0;
143
			$needs_height = !is_numeric($new_height) || $new_height === 0;
144
			// if we have an invalid new size provided i.e. both new values are not numbers or both are 0 then exit early and return false
145
			if ($needs_width && $needs_height){
146
				return false;
147
			}
148
			// otherwise get the ratio of the original so we can calculate any missing new values
149
			$ratio = $orig_width / $orig_height;
150
			if ($needs_width){
151
				$new_width = $new_height * $ratio;
152
			}
153
			if ($needs_height){
154
				$new_height = $new_width / $ratio;
155
			}
156
			return array($new_width, $new_height);
157
		}
158
159
        /**
160
         * Include the thumb dimension html attributes in the rendered HTML
161
         *
162
         * @param $attr
163
         * @param $args
164
         * @param $foogallery_attachment
165
         *
166
         * @return array
167
         */
168
        function include_thumb_dimension_attributes( $attr, $args, $foogallery_attachment ) {
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...
169
            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...
170
171
            //check if we are dealing with a gallery. This check ensures this is not done for albums
172
            if ( isset( $current_foogallery ) ) {
173
174
                //check if we have anything set
175
                if ( isset( $foogallery_attachment->has_thumbnail_dimensions ) ) {
176
                    if ( $foogallery_attachment->thumb_width > 0 ) {
177
                        $attr['width'] = $foogallery_attachment->thumb_width;
178
                    }
179
                    if ( $foogallery_attachment->thumb_height > 0 ) {
180
                        $attr['height'] = $foogallery_attachment->thumb_height;
181
                    }
182
                }
183
            }
184
185
            return $attr;
186
        }
187
188
    }
189
}