Completed
Push — master ( 388212...f32b2c )
by Jonathan
02:07
created

Size   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 343
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.74%

Importance

Changes 10
Bugs 1 Features 2
Metric Value
wmc 41
c 10
b 1
f 2
lcom 1
cbo 2
dl 0
loc 343
ccs 130
cts 133
cp 0.9774
rs 8.2769

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setMaxImageSize() 0 4 1
A getMaxImageSize() 0 4 1
A run() 0 19 3
A getWidth() 0 12 3
A getHeight() 0 12 3
B getFit() 0 25 3
A getCrop() 0 20 2
B resolveMissingDimensions() 0 20 5
A applyDpr() 0 10 1
A limitImageSize() 0 16 3
A runContainResize() 0 6 1
A runMaxResize() 0 7 1
A runFillResize() 0 6 1
A runStretchResize() 0 4 1
A runCropResize() 0 10 1
A getDpr() 0 12 4
B runResize() 0 24 6

How to fix   Complexity   

Complex Class

Complex classes like Size 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 Size, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace League\Glide\Manipulators;
4
5
use Intervention\Image\Image;
6
7
/**
8
 * @property string $dpr
9
 * @property string $fit
10
 * @property string $h
11
 * @property string $w
12
 */
13
class Size extends BaseManipulator
14
{
15
    /**
16
     * Maximum image size in pixels.
17
     * @var int|null
18
     */
19
    protected $maxImageSize;
20
21
    /**
22
     * Create Size instance.
23
     * @param int|null $maxImageSize Maximum image size in pixels.
24
     */
25 66
    public function __construct($maxImageSize = null)
26
    {
27 66
        $this->maxImageSize = $maxImageSize;
28 66
    }
29
30
    /**
31
     * Set the maximum image size.
32
     * @param int|null Maximum image size in pixels.
33
     */
34 6
    public function setMaxImageSize($maxImageSize)
35
    {
36 6
        $this->maxImageSize = $maxImageSize;
37 6
    }
38
39
    /**
40
     * Get the maximum image size.
41
     * @return int|null Maximum image size in pixels.
42
     */
43 6
    public function getMaxImageSize()
44
    {
45 6
        return $this->maxImageSize;
46
    }
47
48
    /**
49
     * Perform size image manipulation.
50
     * @param  Image $image The source image.
51
     * @return Image The manipulated image.
52
     */
53 6
    public function run(Image $image)
54
    {
55 6
        $width = $this->getWidth();
56 6
        $height = $this->getHeight();
57 6
        $fit = $this->getFit();
58 6
        $crop = $this->getCrop();
59 6
        $dpr = $this->getDpr();
60
61 6
        list($width, $height) = $this->resolveMissingDimensions($image, $width, $height);
62 6
        list($width, $height) = $this->applyDpr($width, $height, $dpr);
63 6
        list($width, $height) = $this->limitImageSize($width, $height);
64
65 6
        if (round($width) !== round($image->width()) or
66 6
            round($height) !== round($image->height())) {
67 6
            $image = $this->runResize($image, $fit, round($width), round($height), $crop);
68 4
        }
69
70 6
        return $image;
71
    }
72
73
    /**
74
     * Resolve width.
75
     * @return string The resolved width.
76
     */
77 9
    public function getWidth()
78
    {
79 9
        if (!is_numeric($this->w)) {
80 3
            return;
81
        }
82
83 9
        if ($this->w <= 0) {
84 3
            return;
85
        }
86
87 9
        return (double) $this->w;
88
    }
89
90
    /**
91
     * Resolve height.
92
     * @return string The resolved height.
93
     */
94 9
    public function getHeight()
95
    {
96 9
        if (!is_numeric($this->h)) {
97 6
            return;
98
        }
99
100 6
        if ($this->h <= 0) {
101 3
            return;
102
        }
103
104 6
        return (double) $this->h;
105
    }
106
107
    /**
108
     * Resolve fit.
109
     * @return string The resolved fit.
110
     */
111 9
    public function getFit()
112
    {
113 9
        if (in_array($this->fit, ['contain', 'fill', 'max', 'stretch'], true)) {
114 3
            return $this->fit;
115
        }
116
117
        $cropMethods = [
118 9
            'crop',
119 6
            'crop-top-left',
120 6
            'crop-top',
121 6
            'crop-top-right',
122 6
            'crop-left',
123 6
            'crop-center',
124 6
            'crop-right',
125 6
            'crop-bottom-left',
126 6
            'crop-bottom',
127 6
            'crop-bottom-right',
128 6
        ];
129
130 9
        if (in_array($this->fit, $cropMethods, true)) {
131 3
            return 'crop';
132
        }
133
134 9
        return 'contain';
135
    }
136
137
    /**
138
     * Resolve crop.
139
     * @return string The resolved crop.
140
     */
141 9
    public function getCrop()
142
    {
143
        $cropMethods = [
144 9
            'crop-top-left',
145 6
            'crop-top',
146 6
            'crop-top-right',
147 6
            'crop-left',
148 6
            'crop-center',
149 6
            'crop-right',
150 6
            'crop-bottom-left',
151 6
            'crop-bottom',
152 6
            'crop-bottom-right',
153 6
        ];
154
155 9
        if (!in_array($this->fit, $cropMethods, true)) {
156 9
            return 'center';
157
        }
158
159 3
        return substr($this->fit, 5);
160
    }
161
162
    /**
163
     * Resolve the device pixel ratio.
164
     * @return double The device pixel ratio.
165
     */
166 9
    public function getDpr()
167
    {
168 9
        if (!is_numeric($this->dpr)) {
169 9
            return 1.0;
170
        }
171
172 3
        if ($this->dpr < 0 or $this->dpr > 8) {
173 3
            return 1.0;
174
        }
175
176 3
        return (double) $this->dpr;
177
    }
178
179
    /**
180
     * Resolve missing image dimensions.
181
     * @param  Image       $image  The source image.
182
     * @param  double|null $width  The image width.
183
     * @param  double|null $height The image height.
184
     * @return double[]    The resolved width and height.
185
     */
186 9
    public function resolveMissingDimensions(Image $image, $width, $height)
187
    {
188 9
        if (!$width and !$height) {
189 3
            $width = $image->width();
190 3
            $height = $image->height();
191 2
        }
192
193 9
        if (!$width) {
194 3
            $width = $height * ($image->width() / $image->height());
195 2
        }
196
197 9
        if (!$height) {
198 6
            $height = $width / ($image->width() / $image->height());
199 4
        }
200
201
        return [
202 9
            (double) $width,
203 9
            (double) $height,
204 6
        ];
205
    }
206
207
    /**
208
     * Apply the device pixel ratio.
209
     * @param  double   $width  The target image width.
210
     * @param  double   $height The target image height.
211
     * @param  double   $dpr    The device pixel ratio.
212
     * @return double[] The modified width and height.
213
     */
214 6
    public function applyDpr($width, $height, $dpr)
215
    {
216 6
        $width = $width * $dpr;
217 6
        $height = $height * $dpr;
218
219
        return [
220 6
            (double) $width,
221 6
            (double) $height,
222 4
        ];
223
    }
224
225
    /**
226
     * Limit image size to maximum allowed image size.
227
     * @param  double   $width  The image width.
228
     * @param  double   $height The image height.
229
     * @return double[] The limited width and height.
230
     */
231 9
    public function limitImageSize($width, $height)
232
    {
233 9
        if ($this->maxImageSize !== null) {
234 3
            $imageSize = $width * $height;
235
236 3
            if ($imageSize > $this->maxImageSize) {
237 3
                $width = $width / sqrt($imageSize / $this->maxImageSize);
238 3
                $height = $height / sqrt($imageSize / $this->maxImageSize);
239 2
            }
240 2
        }
241
242
        return [
243 9
            (double) $width,
244 9
            (double) $height,
245 6
        ];
246
    }
247
248
    /**
249
     * Perform resize image manipulation.
250
     * @param  Image       $image  The source image.
251
     * @param  string      $fit    The fit.
252
     * @param  string      $width  The width.
253
     * @param  string      $height The height.
254
     * @param  string|null $crop   The crop.
255
     * @return Image       The manipulated image.
256
     */
257 9
    public function runResize(Image $image, $fit, $width, $height, $crop = null)
258
    {
259 9
        if ($fit === 'contain') {
260 9
            return $this->runContainResize($image, $width, $height);
261
        }
262
263 3
        if ($fit === 'fill') {
264 3
            return $this->runFillResize($image, $width, $height);
265
        }
266
267 3
        if ($fit === 'max') {
268 3
            return $this->runMaxResize($image, $width, $height);
269
        }
270
271 3
        if ($fit === 'stretch') {
272 3
            return $this->runStretchResize($image, $width, $height);
273
        }
274
275 3
        if ($fit === 'crop') {
276 3
            return $this->runCropResize($image, $width, $height, $crop);
277
        }
278
279 3
        return $image;
280
    }
281
282
    /**
283
     * Perform contain resize image manipulation.
284
     * @param  Image  $image  The source image.
285
     * @param  string $width  The width.
286
     * @param  string $height The height.
287
     * @return Image  The manipulated image.
288
     */
289 12
    public function runContainResize(Image $image, $width, $height)
290
    {
291
        return $image->resize($width, $height, function ($constraint) {
292
            $constraint->aspectRatio();
293 12
        });
294
    }
295
296
    /**
297
     * Perform max resize image manipulation.
298
     * @param  Image  $image  The source image.
299
     * @param  string $width  The width.
300
     * @param  string $height The height.
301
     * @return Image  The manipulated image.
302
     */
303 9
    public function runMaxResize(Image $image, $width, $height)
304
    {
305
        return $image->resize($width, $height, function ($constraint) {
306
            $constraint->aspectRatio();
307
            $constraint->upsize();
308 9
        });
309
    }
310
311
    /**
312
     * Perform fill resize image manipulation.
313
     * @param  Image  $image  The source image.
314
     * @param  string $width  The width.
315
     * @param  string $height The height.
316
     * @return Image  The manipulated image.
317
     */
318 6
    public function runFillResize($image, $width, $height)
319
    {
320 6
        $image = $this->runMaxResize($image, $width, $height);
321
322 6
        return $image->resizeCanvas($width, $height, 'center');
323
    }
324
325
    /**
326
     * Perform stretch resize image manipulation.
327
     * @param  Image  $image  The source image.
328
     * @param  string $width  The width.
329
     * @param  string $height The height.
330
     * @return Image  The manipulated image.
331
     */
332 6
    public function runStretchResize(Image $image, $width, $height)
333
    {
334 6
        return $image->resize($width, $height);
335
    }
336
337
    /**
338
     * Perform crop resize image manipulation.
339
     * @param  Image  $image  The source image.
340
     * @param  string $width  The width.
341
     * @param  string $height The height.
342
     * @param  string $crop   The crop.
343
     * @return Image  The manipulated image.
344
     */
345 6
    public function runCropResize(Image $image, $width, $height, $crop)
346
    {
347 6
        return $image->fit(
348 4
            $width,
349 4
            $height,
350 6
            function () {
351 6
            },
352
            $crop
353 4
        );
354
    }
355
}
356