FileSizeScaler::scale()   C
last analyzed

Complexity

Conditions 12
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 6
Ratio 24 %

Importance

Changes 0
Metric Value
dl 6
loc 25
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 17
nc 4
nop 2

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
namespace Brendt\Image\Scaler;
4
5
use Intervention\Image\Image;
6
use Symfony\Component\Finder\SplFileInfo;
7
8
class FileSizeScaler extends AbstractScaler
9
{
10
11
    /**
12
     * @param SplFileInfo $sourceFile
13
     * @param Image       $imageObject
14
     *
15
     * @return array
16
     */
17
    public function scale(SplFileInfo $sourceFile, Image $imageObject) : array {
18
        $fileSize = $sourceFile->getSize();
19
        $width = $imageObject->getWidth();
20
        $height = $imageObject->getHeight();
21
        $ratio = $height / $width;
22
        $area = $width * $width * $ratio;
23
        $pixelPrice = $fileSize / $area;
24
        
25
        $sizes = [];
26
27 View Code Duplication
        if ($this->includeSource && (!$this->maxWidth || $width <= $this->maxWidth) && (!$this->maxFileSize || $fileSize <= $this->maxFileSize)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
            $sizes[$width] = $height;
29
        }
30
31
        do {
32
            $fileSize = $fileSize * $this->stepModifier;
33
            $newWidth = floor(sqrt(($fileSize / $pixelPrice) / $ratio));
34
35 View Code Duplication
            if ((!$this->maxFileSize || $fileSize <= $this->maxFileSize) && (!$this->maxWidth || $newWidth <= $this->maxWidth)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
                $sizes[(int) $newWidth] = (int) $newWidth * $ratio;
37
            }
38
        } while ($fileSize > $this->minFileSize && $newWidth > $this->minWidth);
39
40
        return $sizes;
41
    }
42
}
43