Completed
Pull Request — master (#732)
by 12345
03:41
created

WatermarkFilterLoader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 17.02 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 16
c 2
b 0
f 1
lcom 1
cbo 5
dl 16
loc 94
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C load() 16 72 15

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Liip\ImagineBundle\Imagine\Filter\Loader;
4
5
use Imagine\Image\Box;
6
use Imagine\Image\Point;
7
use Imagine\Image\ImageInterface;
8
use Imagine\Image\ImagineInterface;
9
10
class WatermarkFilterLoader implements LoaderInterface
11
{
12
    /**
13
     * @var ImagineInterface
14
     */
15
    protected $imagine;
16
17
    /**
18
     * @var string
19
     */
20
    protected $rootPath;
21
22
    public function __construct(ImagineInterface $imagine, $rootPath)
23
    {
24
        $this->imagine = $imagine;
25
        $this->rootPath = $rootPath;
26
    }
27
28
    /**
29
     * @see Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface::load()
30
     */
31
    public function load(ImageInterface $image, array $options = array())
32
    {
33
        $options += array(
34
            'size' => null,
35
            'position' => 'center',
36
        );
37
38
        if (substr($options['size'], -1) == '%') {
39
            $options['size'] = substr($options['size'], 0, -1) / 100;
40
        }
41
42
        $watermark = $this->imagine->open($this->rootPath.'/'.$options['image']);
43
44
        $size = $image->getSize();
45
        $watermarkSize = $watermark->getSize();
46
47
        // If 'null': Downscale if needed
48
        if (!$options['size'] && ($size->getWidth() < $watermarkSize->getWidth() || $size->getHeight() < $watermarkSize->getHeight())) {
49
            $options['size'] = 1.0;
50
        }
51
52
        if ($options['size']) {
53
            $factor = $options['size'] * min($size->getWidth() / $watermarkSize->getWidth(), $size->getHeight() / $watermarkSize->getHeight());
54
55
            $watermark->resize(new Box($watermarkSize->getWidth() * $factor, $watermarkSize->getHeight() * $factor));
56
            $watermarkSize = $watermark->getSize();
57
        }
58
59
        switch ($options['position']) {
60
            case 'topleft':
61
                $x = 0;
62
                $y = 0;
63
                break;
64
            case 'top':
65
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
66
                $y = 0;
67
                break;
68
            case 'topright':
69
                $x = $size->getWidth() - $watermarkSize->getWidth();
70
                $y = 0;
71
                break;
72
            case 'left':
73
                $x = 0;
74
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
75
                break;
76 View Code Duplication
            case 'center':
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...
77
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
78
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
79
                break;
80 View Code Duplication
            case 'right':
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...
81
                $x = $size->getWidth() - $watermarkSize->getWidth();
82
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
83
                break;
84
            case 'bottomleft':
85
                $x = 0;
86
                $y = $size->getHeight() - $watermarkSize->getHeight();
87
                break;
88 View Code Duplication
            case 'bottom':
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...
89
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
90
                $y = $size->getHeight() - $watermarkSize->getHeight();
91
                break;
92 View Code Duplication
            case 'bottomright':
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...
93
                $x = $size->getWidth() - $watermarkSize->getWidth();
94
                $y = $size->getHeight() - $watermarkSize->getHeight();
95
                break;
96
            default:
97
                throw new \InvalidArgumentException("Unexpected position '{$options['position']}'");
98
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
99
        }
100
101
        return $image->paste($watermark, new Point($x, $y));
102
    }
103
}
104