Dimmensions   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 130
Duplicated Lines 18.46 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 0
dl 24
loc 130
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C getResizeDimmensions() 24 41 14
A getIntegerValue() 0 14 5
A getPositionValue() 0 27 4

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 Imagecow\Utils;
4
5
/**
6
 * Usefull dimmensions calculations.
7
 */
8
class Dimmensions
9
{
10
    protected static $positionsKeywordsX = [
11
        'left' => '0%',
12
        'center' => '50%',
13
        'right' => '100%',
14
    ];
15
16
    protected static $positionsKeywordsY = [
17
        'top' => '0%',
18
        'middle' => '50%',
19
        'bottom' => '100%',
20
    ];
21
22
    /**
23
     * Calculate the dimensions of a resize.
24
     *
25
     * @param int  $oldWidth
26
     * @param int  $oldHeight
27
     * @param int  $newWidth
28
     * @param int  $newHeight
29
     * @param bool $cover
30
     *
31
     * @return array [width, height]
32
     */
33
    public static function getResizeDimmensions($oldWidth, $oldHeight, $newWidth, $newHeight, $cover = false)
34
    {
35 View Code Duplication
        if (empty($newHeight)) {
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
            return [(int) $newWidth, (int) ceil(($newWidth / $oldWidth) * $oldHeight)];
37
        }
38
39 View Code Duplication
        if (empty($newWidth)) {
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...
40
            return [(int) ceil(($newHeight / $oldHeight) * $oldWidth), (int) $newHeight];
41
        }
42
43
        $scaleWidth = $newWidth / $oldWidth;
44
        $scaleHeight = $newHeight / $oldHeight;
45
46
        if ($cover) {
47 View Code Duplication
            if ($scaleWidth > $scaleHeight) {
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...
48
                return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
49
            }
50
51 View Code Duplication
            if ($scaleWidth < $scaleHeight) {
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...
52
                return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
53
            }
54
        } else {
55 View Code Duplication
            if ($scaleWidth < $scaleHeight) {
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...
56
                return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
57
            }
58
59 View Code Duplication
            if ($scaleWidth > $scaleHeight) {
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...
60
                return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
61
            }
62
        }
63
64 View Code Duplication
        if ($scaleWidth < $scaleHeight || ($cover && $scaleWidth > $scaleHeight)) {
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...
65
            return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
66
        }
67
68 View Code Duplication
        if ($scaleWidth > $scaleHeight || ($cover && $scaleWidth < $scaleHeight)) {
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...
69
            return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
70
        }
71
72
        return [(int) $newWidth, (int) $newHeight];
73
    }
74
75
    /**
76
     * Calculate a dimension value.
77
     *
78
     * @param string     $direction
79
     * @param int|string $value
80
     * @param int        $relatedValue
81
     * @param bool       $position
82
     *
83
     * @return int
84
     */
85
    public static function getIntegerValue($direction, $value, $relatedValue, $position = false)
86
    {
87
        $keywords = ($direction === 'y') ? static::$positionsKeywordsY : static::$positionsKeywordsX;
88
89
        if ($position && isset($keywords[$value])) {
90
            $value = $keywords[$value];
91
        }
92
93
        if (substr($value, -1) === '%') {
94
            return intval(($relatedValue / 100) * floatval(substr($value, 0, -1)));
95
        }
96
97
        return intval($value);
98
    }
99
100
    /**
101
     * Calculates the x/y position.
102
     *
103
     * @param string          $direction (y or x)
104
     * @param string|int|null $position
105
     * @param int             $newValue
106
     * @param int             $oldValue
107
     *
108
     * @return int
109
     */
110
    public static function getPositionValue($direction, $position, $newValue, $oldValue)
111
    {
112
        $split = preg_split('/([\+\-])/', str_replace(' ', '', $position), 2, PREG_SPLIT_DELIM_CAPTURE);
113
114
        //Base value
115
        $value = $split[0];
116
117
        if (is_numeric($value)) {
118
            $value = (int) $value;
119
        } else {
120
            $newCenter = static::getIntegerValue($direction, $value, $newValue, true);
121
            $oldCenter = static::getIntegerValue($direction, $value, $oldValue, true);
122
123
            $value = $oldCenter - $newCenter;
124
        }
125
126
        //Offset
127
        $offset = isset($split[2]) ? $split[1].$split[2] : 0;
128
129
        if (is_numeric($offset)) {
130
            $offset = (int) $offset;
131
        } else {
132
            $offset = static::getIntegerValue($direction, $offset, $oldValue, true);
133
        }
134
135
        return $value + $offset;
136
    }
137
}
138