Fit   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 129
Duplicated Lines 20.93 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 28
c 2
b 0
f 0
lcom 2
cbo 4
dl 27
loc 129
ccs 0
cts 78
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A apply() 17 17 1
A parseOptions() 10 10 3
C parseOptionsString() 0 54 23

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
namespace SvImages\Transformer;
3
4
use Intervention\Image;
5
use Intervention\Image\ImageManager;
6
use SvImages\Exception\InvalidArgumentException;
7
8
/**
9
 * @author Vytautas Stankus <[email protected]>
10
 * @license MIT
11
 */
12
class Fit extends AbstractTransformer
13
{
14
    protected $positions = [
15
        'top-left',
16
        'top',
17
        'top-right',
18
        'left',
19
        'center',
20
        'right',
21
        'bottom-left',
22
        'bottom',
23
        'bottom-right',
24
        'tl',
25
        't',
26
        'tr',
27
        'l',
28
        'c',
29
        'r',
30
        'bl',
31
        'b',
32
        'br'
33
    ];
34
35
    protected $defaults = [
36
        'position' => 'center',
37
        'width' => null,
38
        'height' => null,
39
    ];
40
41
    /**
42
     * @var ImageManager
43
     */
44
    private $imageManager;
45
46
    public function __construct(ImageManager $imageManager)
47
    {
48
        $this->imageManager = $imageManager;
49
    }
50
51 View Code Duplication
    public function apply($source, $options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
52
    {
53
        $options = $this->parseOptions($options);
54
55
        $image = $this->imageManager->make($source);
56
57
        $image->fit(
58
            $options['width'],
59
            $options['height'],
60
            function ($constraint) {
61
                $constraint->upsize();
62
            },
63
            $options['position']
64
        );
65
66
        return $image->encode($image->mime())->getEncoded();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 View Code Duplication
    protected function parseOptions($options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
73
    {
74
        $options = parent::parseOptions($options);
75
76
        if (empty($options['width']) && empty($options['height'])) {
77
            throw new InvalidArgumentException; //todo
78
        }
79
80
        return $options;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function parseOptionsString($options)
87
    {
88
        $parsed_options = [];
89
        $options_list = explode(',', $options, 3);
90
91
        foreach ($options_list as $option) {
92
            if (preg_match('/^w(?<value>[1-9]\d*)$/', $option, $match)) {
93
                $parsed_options['width'] = $match['value'];
94
            } elseif (preg_match('/^h(?<value>[1-9]\d*)$/', $option, $match)) {
95
                $parsed_options['height'] = $match['value'];
96
            } elseif (in_array($option, $this->positions)) {
97
                switch ($option) {
98
                    case 'top-left':
99
                    case 'tl':
100
                        $parsed_options['position'] = 'top-left';
101
                        break;
102
                    case 'top':
103
                    case 't':
104
                        $parsed_options['position'] = 'top';
105
                        break;
106
                    case 'top-right':
107
                    case 'tr':
108
                        $parsed_options['position'] = 'top-right';
109
                        break;
110
                    case 'left':
111
                    case 'l':
112
                        $parsed_options['position'] = 'left';
113
                        break;
114
                    case 'center':
115
                    case 'c':
116
                        $parsed_options['position'] = 'center';
117
                        break;
118
                    case 'right':
119
                    case 'r':
120
                        $parsed_options['position'] = 'right';
121
                        break;
122
                    case 'bottom-left':
123
                    case 'bl':
124
                        $parsed_options['position'] = 'bottom-left';
125
                        break;
126
                    case 'bottom':
127
                    case 'b':
128
                        $parsed_options['position'] = 'bottom';
129
                        break;
130
                    case 'bottom-right':
131
                    case 'br':
132
                        $parsed_options['position'] = 'bottom-right';
133
                        break;
134
                }
135
            }
136
        }
137
138
        return $parsed_options;
139
    }
140
}
141