Total Complexity | 47 |
Total Lines | 313 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like MetaSliderImageHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MetaSliderImageHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class MetaSliderImageHelper |
||
7 | { |
||
8 | private $smart_crop = 'false'; |
||
9 | private $container_width; // slideshow width |
||
10 | private $container_height; // slideshow height |
||
11 | private $id; // slide/attachment ID |
||
12 | private $url; |
||
13 | private $path; // path to attachment on server |
||
14 | private $use_image_editor; |
||
15 | |||
16 | /** |
||
17 | * Constructor |
||
18 | * |
||
19 | * @param int $slide_id |
||
20 | * @param int $width - required width of image |
||
21 | * @param int $height - required height of image |
||
22 | * @param string $smart_crop |
||
23 | * @param bool $use_image_editor |
||
24 | */ |
||
25 | public function __construct($slide_id, $width, $height, $smart_crop, $use_image_editor = true) |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Return the crop dimensions. |
||
40 | * |
||
41 | * Smart Crop: If the image is smaller than the container width or height, then return |
||
42 | * dimensions that respect the container size ratio. This ensures image displays in a |
||
43 | * sane manner in responsive sliders |
||
44 | * |
||
45 | * @param int $image_width |
||
46 | * @param int $image_height |
||
47 | * @return array image dimensions |
||
48 | */ |
||
49 | private function get_crop_dimensions($image_width, $image_height) |
||
50 | { |
||
51 | if ('false' === $this->smart_crop) { |
||
52 | return ['width' => $this->container_width, 'height' => $this->container_height]; |
||
53 | } |
||
54 | |||
55 | $container_width = $this->container_width; |
||
56 | $container_height = $this->container_height; |
||
57 | |||
58 | /** |
||
59 | * Slideshow Width == Slide Width |
||
60 | */ |
||
61 | if ($image_width == $container_width && $image_height == $container_height) { |
||
62 | $new_slide_width = $container_width; |
||
63 | $new_slide_height = $container_height; |
||
64 | } |
||
65 | |||
66 | if ($image_width == $container_width && $image_height < $container_height) { |
||
67 | $new_slide_height = $image_height; |
||
68 | $new_slide_width = $container_width / ($container_height / $image_height); |
||
69 | } |
||
70 | |||
71 | if ($image_width == $container_width && $image_height > $container_height) { |
||
72 | $new_slide_width = $container_width; |
||
73 | $new_slide_height = $container_height; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Slideshow Width < Slide Width |
||
78 | */ |
||
79 | if ($image_width < $container_width && $image_height == $container_height) { |
||
80 | $new_slide_width = $image_width; |
||
81 | $new_slide_height = $image_height / ($container_width / $image_width); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Slide is smaller than slidehow - both width and height |
||
86 | */ |
||
87 | if ($image_width < $container_width && $image_height < $container_height) { |
||
88 | if ($container_width > $container_height) { |
||
89 | // wide |
||
90 | |||
91 | if ($image_width > $image_height) { |
||
92 | // wide |
||
93 | $new_slide_height = $image_height; |
||
94 | $new_slide_width = $container_width / ($container_height / $image_height); |
||
95 | |||
96 | if ($new_slide_width > $image_width) { |
||
97 | $new_slide_width = $image_width; |
||
98 | $new_slide_height = $container_height / ($container_width / $image_width); |
||
99 | } |
||
100 | } else { |
||
101 | // tall |
||
102 | $new_slide_width = $image_width; |
||
103 | $new_slide_height = $container_height / ($container_width / $image_width); |
||
104 | |||
105 | if ($new_slide_height > $image_height) { |
||
106 | $new_slide_height = $image_height; |
||
107 | $new_slide_width = $container_width / ($container_height / $image_height); |
||
108 | } |
||
109 | } |
||
110 | } else { |
||
111 | // tall |
||
112 | if ($image_width > $image_height) { |
||
113 | // wide |
||
114 | $new_slide_height = $image_height; |
||
115 | $new_slide_width = $container_width / ($container_height / $image_height); |
||
116 | |||
117 | if ($new_slide_width > $image_width) { |
||
118 | $new_slide_width = $image_width; |
||
119 | $new_slide_height = $container_height / ($container_width / $image_width); |
||
120 | } |
||
121 | } else { |
||
122 | // tall |
||
123 | $new_slide_width = $image_width; |
||
124 | $new_slide_height = $container_height / ($container_width / $image_width); |
||
125 | |||
126 | if ($new_slide_height > $image_height) { |
||
127 | $new_slide_height = $image_height; |
||
128 | $new_slide_width = $container_width / ($container_height / $image_height); |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | if ($image_width < $container_width && $image_height > $container_height) { |
||
135 | $new_slide_width = $image_width; |
||
136 | $new_slide_height = $container_height / ($container_width / $image_width); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Slideshow Width > Slide Width |
||
141 | */ |
||
142 | if ($image_width > $container_width && $image_height == $container_height) { |
||
143 | $new_slide_width = $container_width; |
||
144 | $new_slide_height = $container_height; |
||
145 | } |
||
146 | |||
147 | if ($image_width > $container_width && $image_height < $container_height) { |
||
148 | $new_slide_height = $image_height; |
||
149 | $new_slide_width = $container_width / ($container_height / $image_height); |
||
150 | } |
||
151 | |||
152 | if ($image_width > $container_width && $image_height > $container_height) { |
||
153 | $new_slide_width = $container_width; |
||
154 | $new_slide_height = $container_height; |
||
155 | } |
||
156 | |||
157 | return ['width' => floor($new_slide_width), 'height' => floor($new_slide_height)]; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Return the image URL, crop the image to the correct dimensions if required |
||
162 | * |
||
163 | * @return string resized image URL |
||
164 | */ |
||
165 | public function get_image_url() |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Get the image dimensions for the original image. |
||
218 | * |
||
219 | * Fall back to using the WP_Image_Editor if the size is not stored in metadata |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | private function get_original_image_dimensions() |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Return the file name for the required image size |
||
250 | * |
||
251 | * @param array $dest_size image dimensions (width/height) in pixels |
||
252 | * @return string |
||
253 | */ |
||
254 | private function get_destination_file_name($dest_size) |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Use WP_Image_Editor to create a resized image and return the URL for that image |
||
267 | * |
||
268 | * @param array $orig_size |
||
269 | * @param array $dest_size |
||
270 | * @param $dest_file_name |
||
271 | * @return string |
||
272 | */ |
||
273 | private function resize_image($orig_size, $dest_size, $dest_file_name) |
||
319 | } |
||
320 | } |
||
321 |