Completed
Push — master ( 319645...655917 )
by Lukas Kahwe
14s
created

ScaleFilterLoader   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 56
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
D load() 0 33 9
A calcAbsoluteRatio() 0 4 1
A isImageProcessable() 0 4 1
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
    public function __construct($dimentionKey = 'dim', $ratioKey = 'to', $absoluteRatio = true)
17
    {
18
        $this->dimentionKey = $dimentionKey;
0 ignored issues
show
Bug introduced by
The property dimentionKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
        $this->ratioKey = $ratioKey;
0 ignored issues
show
Bug introduced by
The property ratioKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
        $this->absoluteRatio = $absoluteRatio;
0 ignored issues
show
Bug introduced by
The property absoluteRatio does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function load(ImageInterface $image, array $options = array())
27
    {
28
        if (!isset($options[$this->dimentionKey]) && !isset($options[$this->ratioKey])) {
29
            throw new \InvalidArgumentException("Missing $this->dimentionKey or $this->ratioKey option.");
30
        }
31
32
        $size = $image->getSize();
33
        $origWidth = $size->getWidth();
34
        $origHeight = $size->getHeight();
35
36
        if (isset($options[$this->ratioKey])) {
37
            $ratio = $this->absoluteRatio ? $options[$this->ratioKey] : $this->calcAbsoluteRatio($options[$this->ratioKey]);
38
        } elseif (isset($options[$this->dimentionKey])) {
39
            list($width, $height) = $options[$this->dimentionKey];
40
41
            $widthRatio = $width / $origWidth;
42
            $heightRatio = $height / $origHeight;
43
44
            if (null == $width || null == $height) {
45
                $ratio = max($widthRatio, $heightRatio);
46
            } else {
47
                $ratio = min($widthRatio, $heightRatio);
48
            }
49
        }
50
51
        if ($this->isImageProcessable($ratio)) {
52
            $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...
53
54
            return $filter->apply($image);
55
        }
56
57
        return $image;
58
    }
59
60
    protected function calcAbsoluteRatio($ratio)
61
    {
62
        return $ratio;
63
    }
64
65
    protected function isImageProcessable($ratio)
66
    {
67
        return true;
68
    }
69
}
70