Completed
Push — master ( c2562a...15ecc0 )
by Lukas Kahwe
09:33 queued 06:51
created

ScaleFilterLoader::load()   C

Complexity

Conditions 11
Paths 23

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 22
nc 23
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 Liip\ImagineBundle\Imagine\Filter\Loader;
4
5
use Imagine\Filter\Basic\Resize;
6
use Imagine\Image\ImageInterface;
7
use Imagine\Image\Box;
8
9
/**
10
 * Scale filter.
11
 *
12
 * @author Devi Prasad <https://github.com/deviprsd21>
13
 */
14
class ScaleFilterLoader implements LoaderInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $dimensionKey;
20
21
    /**
22
     * @var string
23
     */
24
    protected $ratioKey;
25
26
    /**
27
     * @var bool
28
     */
29
    protected $absoluteRatio;
30
31
    public function __construct($dimensionKey = 'dim', $ratioKey = 'to', $absoluteRatio = true)
32
    {
33
        $this->dimensionKey = $dimensionKey;
34
        $this->ratioKey = $ratioKey;
35
        $this->absoluteRatio = $absoluteRatio;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function load(ImageInterface $image, array $options = array())
42
    {
43
        if (!isset($options[$this->dimensionKey]) && !isset($options[$this->ratioKey])) {
44
            throw new \InvalidArgumentException("Missing $this->dimensionKey or $this->ratioKey option.");
45
        }
46
47
        $size = $image->getSize();
48
        $origWidth = $size->getWidth();
49
        $origHeight = $size->getHeight();
50
51
        if (isset($options[$this->ratioKey])) {
52
            $ratio = $this->absoluteRatio ? $options[$this->ratioKey] : $this->calcAbsoluteRatio($options[$this->ratioKey]);
53
        } elseif (isset($options[$this->dimensionKey])) {
54
            $size = $options[$this->dimensionKey];
55
            $width = isset($size[0]) ? $size[0] : null;
56
            $height = isset($size[1]) ? $size[1] : null;
57
58
            $widthRatio = $width / $origWidth;
59
            $heightRatio = $height / $origHeight;
60
61
            if (null == $width || null == $height) {
62
                $ratio = max($widthRatio, $heightRatio);
63
            } else {
64
                $ratio = min($widthRatio, $heightRatio);
65
            }
66
        }
67
68
        if ($this->isImageProcessable($ratio)) {
69
            $filter = new Resize(new Box(round($origWidth * $ratio), round($origHeight * $ratio)));
0 ignored issues
show
Bug introduced by
The variable $ratio does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
70
71
            return $filter->apply($image);
72
        }
73
74
        return $image;
75
    }
76
77
    protected function calcAbsoluteRatio($ratio)
78
    {
79
        return $ratio;
80
    }
81
82
    protected function isImageProcessable($ratio)
83
    {
84
        return true;
85
    }
86
}
87