Completed
Pull Request — master (#23)
by
unknown
02:15
created

Dimmensions::getResizeDimmensions()   C

Complexity

Conditions 14
Paths 12

Size

Total Lines 41
Code Lines 22

Duplication

Lines 24
Ratio 58.54 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 24
loc 41
rs 5.0864
cc 14
eloc 22
nc 12
nop 5

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 Imagecow\Utils;
4
5
use Imagecow\ImageException;
6
7
/**
8
 * Usefull dimmensions calculations.
9
 */
10
class Dimmensions
11
{
12
    protected static $positionsKeywords = [
13
        'top' => '0%',
14
        'left' => '0%',
15
        'middle' => '50%',
16
        'center' => '50%',
17
        'right' => '100%',
18
        'bottom' => '100%',
19
    ];
20
21
    /**
22
     * Calculate the dimensions of a resize.
23
     *
24
     * @param int  $oldWidth
25
     * @param int  $oldHeight
26
     * @param int  $newWidth
27
     * @param int  $newHeight
28
     * @param bool $cover
29
     *
30
     * @return array [width, height]
31
     */
32
    public static function getResizeDimmensions($oldWidth, $oldHeight, $newWidth, $newHeight, $cover = false)
33
    {
34 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...
35
            return [(int) $newWidth, (int) ceil(($newWidth / $oldWidth) * $oldHeight)];
36
        }
37
38 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...
39
            return [(int) ceil(($newHeight / $oldHeight) * $oldWidth), (int) $newHeight];
40
        }
41
42
        $scaleWidth = $newWidth / $oldWidth;
43
        $scaleHeight = $newHeight / $oldHeight;
44
45
        if ($cover) {
46 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...
47
                return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
48
            }
49
50 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...
51
                return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
52
            }
53
        } else {
54 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...
55
                return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
56
            }
57
58 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...
59
                return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
60
            }
61
        }
62
63 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...
64
            return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
65
        }
66
67 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...
68
            return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
69
        }
70
71
        return [(int) $newWidth, (int) $newHeight];
72
    }
73
74
    /**
75
     * Calculate a dimension value.
76
     *
77
     * @param int|string $value
78
     * @param int        $relatedValue
79
     * @param bool       $position
80
     *
81
     * @return int
82
     */
83
    public static function getIntegerValue($value, $relatedValue, $position = false)
84
    {
85
        if ($position && isset(static::$positionsKeywords[$value])) {
86
            $value = static::$positionsKeywords[$value];
87
        }
88
89
        if (substr($value, -1) === '%') {
90
            return intval(($relatedValue / 100) * floatval(substr($value, 0, -1)));
91
        }
92
93
        return intval($value);
94
    }
95
96
    /**
97
     * Calculate a dimension value.
98
     *
99
     * @param int|string $value
100
     * @param int        $relatedValue
101
     * @param bool       $position
102
     *
103
     * @return string
104
     */
105
    public static function getPercentageValue($value, $relatedValue, $position = false)
106
    {
107
        if ($position && isset(static::$positionsKeywords[$value])) {
108
            return static::$positionsKeywords[$value];
109
        }
110
111
        if (substr($value, -1) === '%') {
112
            return $value;
113
        }
114
115
        if (is_numeric($value)) {
116
            return empty($value) ? '0%' : (($value / $relatedValue) * 100).'%';
117
        }
118
119
        throw new ImageException("Invalid position: {$value}");
120
    }
121
122
    /**
123
     * Calculates the x/y position.
124
     *
125
     * @param string|int|null $position
126
     * @param int             $newValue
127
     * @param int             $oldValue
128
     *
129
     * @return int
130
     */
131
    public static function getPositionValue($position, $newValue, $oldValue)
132
    {
133
        if (is_numeric($position)) {
134
            return intval($position);
135
        }
136
137
        $newCenter = static::getIntegerValue($position, $newValue, true);
138
        $oldCenter = static::getIntegerValue($position, $oldValue, true);
139
140
        return $oldCenter - $newCenter;
141
    }
142
143
    /**
144
     * Calculates the x/y position.
145
     *
146
     * @param string|int|null $position
147
     * @param int             $original
148
     * @param int             $reference
149
     * @param int             $padding
150
     *
151
     * @return int
152
     */
153
    public static function getPositionFromReferenceValue($position, $original, $reference, $padding = 0)
154
    {
155
        if (is_numeric($position)) {
156
            $value = intval($position);
157
        } else {
158
            $value = static::getIntegerValue($position, $original, true);
159
        }
160
161
        $padding = static::getIntegerValue($padding, $original, true);
162
163
        if (($position === 'top') || ($position === 'left')) {
164
            $value = $padding;
165
        } elseif (($position === 'bottom') || ($position === 'right')) {
166
            $value -= $reference + $padding;
167
        } elseif (($position === 'middle') || ($position === 'center')) {
168
            $value = ($original / 2) - ($reference / 2);
169
        }
170
171
        return $value;
172
    }
173
}
174