Completed
Push — add/e2e-latest-gutenberg-in-ci ( bfab32...c58b86 )
by Yaroslav
15:57 queued 07:19
created

Jetpack_Photon_ImageSizes::generate_sizes()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 51

Duplication

Lines 14
Ratio 27.45 %

Importance

Changes 0
Metric Value
cc 6
nc 10
nop 0
dl 14
loc 51
rs 8.4468
c 0
b 0
f 0

How to fix   Long Method   

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
 * The Image Sizes library.
4
 *
5
 * @package jetpack
6
 */
7
8
jetpack_require_lib( 'class.jetpack-photon-image' );
9
10
/**
11
 * Class Jetpack_Photon_ImageSizes
12
 *
13
 * Manages image resizing via Jetpack CDN Service.
14
 */
15
class Jetpack_Photon_ImageSizes {
16
17
	/**
18
	 * @var array $data Attachment metadata.
19
	 */
20
	public $data;
21
22
	/**
23
	 * @var Image Image to be resized.
24
	 */
25
	public $image;
26
27
	/**
28
	 * @var null|array $sizes Intermediate sizes.
29
	 */
30
	public static $sizes = null;
31
32
	/**
33
	 * Construct new sizes meta
34
	 *
35
	 * @param int   $attachment_id Attachment ID.
36
	 * @param array $data          Attachment metadata.
37
	 */
38
	public function __construct( $attachment_id, $data ) {
39
		$this->data  = $data;
40
		$this->image = new Jetpack_Photon_Image( $data, get_post_mime_type( $attachment_id ) );
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Jetpack_Photon_Imag...e_type($attachment_id)) of type object<Jetpack_Photon_Image> is incompatible with the declared type object<Image> of property $image.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
		$this->generate_sizes();
42
	}
43
44
	/**
45
	 * Generate sizes for attachment.
46
	 *
47
	 * @return array Array of sizes; empty array as failure fallback.
48
	 */
49
	protected function generate_sizes() {
50
51
		// There is no need to generate the sizes a new for every single image.
52
		if ( null !== self::$sizes ) {
53
			return self::$sizes;
54
		}
55
56
		/*
57
		 * The following logic is copied over from wp_generate_attachment_metadata
58
		 */
59
		$_wp_additional_image_sizes = wp_get_additional_image_sizes();
60
61
		$sizes = array();
62
63
		$intermediate_image_sizes = get_intermediate_image_sizes();
64
65
		foreach ( $intermediate_image_sizes as $s ) {
66
			$sizes[ $s ] = array(
67
				'width'  => '',
68
				'height' => '',
69
				'crop'   => false,
70
			);
71 View Code Duplication
			if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) {
72
				// For theme-added sizes.
73
				$sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] );
74
			} else {
75
				// For default sizes set in options.
76
				$sizes[ $s ]['width'] = get_option( "{$s}_size_w" );
77
			}
78
79 View Code Duplication
			if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) {
80
				// For theme-added sizes.
81
				$sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] );
82
			} else {
83
				// For default sizes set in options.
84
				$sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
85
			}
86
87
			if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) {
88
				// For theme-added sizes.
89
				$sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop'];
90
			} else {
91
				// For default sizes set in options.
92
				$sizes[ $s ]['crop'] = get_option( "{$s}_crop" );
93
			}
94
		}
95
96
		self::$sizes = $sizes;
97
98
		return $sizes;
99
	}
100
101
	/**
102
	 * @return array
103
	 */
104
	public function filtered_sizes() {
105
		// Remove filter preventing the creation of advanced sizes.
106
		remove_filter(
107
			'intermediate_image_sizes_advanced',
108
			array( 'Jetpack_Photon', 'filter_photon_noresize_intermediate_sizes' )
109
		);
110
111
		/** This filter is documented in wp-admin/includes/image.php */
112
		$sizes = apply_filters( 'intermediate_image_sizes_advanced', self::$sizes, $this->data );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $this->data.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
113
114
		// Re-add the filter removed above.
115
		add_filter(
116
			'intermediate_image_sizes_advanced',
117
			array( 'Jetpack_Photon', 'filter_photon_noresize_intermediate_sizes' )
118
		);
119
120
		return (array) $sizes;
121
	}
122
123
	/**
124
	 * Standardises and validates the size_data array.
125
	 *
126
	 * @param array $size_data Size data array - at least containing height or width key. Can contain crop as well.
127
	 *
128
	 * @return array Array with populated width, height and crop keys; empty array if no width and height are provided.
129
	 */
130
	public function standardize_size_data( $size_data ) {
131
		$has_at_least_width_or_height = ( isset( $size_data['width'] ) || isset( $size_data['height'] ) );
132
		if ( ! $has_at_least_width_or_height ) {
133
			return array();
134
		}
135
136
		$defaults = array(
137
			'width'  => null,
138
			'height' => null,
139
			'crop'   => false,
140
		);
141
142
		return array_merge( $defaults, $size_data );
143
	}
144
145
	/**
146
	 * Get sizes for attachment post meta.
147
	 *
148
	 * @return array ImageSizes for attachment postmeta.
149
	 */
150
	public function generate_sizes_meta() {
151
152
		$metadata = array();
153
154
		foreach ( $this->filtered_sizes() as $size => $size_data ) {
155
156
			$size_data = $this->standardize_size_data( $size_data );
157
158
			if ( true === empty( $size_data ) ) {
159
				continue;
160
			}
161
162
			$resized_image = $this->resize( $size_data );
163
164
			if ( true === is_array( $resized_image ) ) {
165
				$metadata[ $size ] = $resized_image;
166
			}
167
		}
168
169
		return $metadata;
170
	}
171
172
	/**
173
	 * @param array $size_data
174
	 *
175
	 * @return array|\WP_Error Array for usage in $metadata['sizes']; WP_Error on failure.
176
	 */
177
	protected function resize( $size_data ) {
178
179
		return $this->image->get_size( $size_data );
180
181
	}
182
}
183