RoundCorners::execute()   F
last analyzed

Complexity

Conditions 29
Paths 144

Size

Total Lines 80
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 80
rs 3.8
c 0
b 0
f 0
cc 29
nc 144
nop 5

How to fix   Long Method    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
##DOC-SIGNATURE##
4
5
    This file is part of WideImage.
6
		
7
    WideImage is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU Lesser General Public License as published by
9
    the Free Software Foundation; either version 2.1 of the License, or
10
    (at your option) any later version.
11
		
12
    WideImage is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU Lesser General Public License for more details.
16
		
17
    You should have received a copy of the GNU Lesser General Public License
18
    along with WideImage; if not, write to the Free Software
19
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21
    * @package Internal/Operations
22
  **/
23
24
namespace WideImage\Operation;
25
26
use WideImage\WideImage;
27
28
/**
29
 * ApplyMask operation class
30
 * 
31
 * @package Internal/Operations
32
 */
33
class RoundCorners
34
{
35
	/**
36
	 * @param \WideImage\Image $image
37
	 * @param int $radius
38
	 * @param int $color
39
	 * @param int $smoothness
40
	 * @return \WideImage\Image
41
	 */
42
	public function execute($image, $radius, $color, $smoothness, $corners)
43
	{
44
		if ($smoothness < 1) {
45
			$sample_ratio = 1;
46
		} elseif ($smoothness > 16) {
47
			$sample_ratio = 16;
48
		} else {
49
			$sample_ratio = $smoothness;
50
		}
51
		
52
		$corner = WideImage::createTrueColorImage($radius * $sample_ratio, $radius * $sample_ratio);
53
		
54
		if ($color === null) {
0 ignored issues
show
introduced by
The condition $color === null is always false.
Loading history...
55
			imagepalettecopy($corner->getHandle(), $image->getHandle());
56
			$bg_color = $corner->allocateColor(0, 0, 0);
57
			
58
			$corner->fill(0, 0, $bg_color);
59
			$fg_color = $corner->allocateColor(255, 255, 255);
60
			$corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color);
61
			$corner = $corner->resize($radius, $radius);
62
			
63
			$result = $image->asTrueColor();
64
			
65
			$tc = $result->getTransparentColor();
66
			
67
			if ($tc == -1) {
68
				$tc = $result->allocateColorAlpha(255, 255, 255, 127);
69
				imagecolortransparent($result->getHandle(), $tc);
70
				$result->setTransparentColor($tc);
71
			}
72
			
73
			if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP) {
74
				$result = $result->applyMask($corner, -1, -1);
75
			}
76
			
77
			$corner = $corner->rotate(90);
78
			if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT) {
79
				$result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100);
80
			}
81
			
82
			$corner = $corner->rotate(90);
83
			if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM) {
84
				$result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100);
85
			}
86
			
87
			$corner = $corner->rotate(90);
88
			if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM) {
89
				$result = $result->applyMask($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100);
90
			}
91
			
92
			return $result;
93
		} else {
94
			$bg_color = $color;
95
			
96
			$corner->fill(0, 0, $bg_color);
97
			$fg_color = $corner->allocateColorAlpha(127, 127, 127, 127);
98
			$corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color);
0 ignored issues
show
Bug introduced by
The method filledEllipse() does not exist on WideImage\Canvas. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
			$corner->getCanvas()->/** @scrutinizer ignore-call */ filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color);
Loading history...
99
			$corner = $corner->resize($radius, $radius);
100
			
101
			$result = $image->copy();
102
			if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP) {
103
				$result = $result->merge($corner, -1, -1, 100);
104
			}
105
			
106
			$corner = $corner->rotate(90);
107
			if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT) {
108
				$result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100);
109
			}
110
			
111
			$corner = $corner->rotate(90);
112
			if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM) {
113
				$result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100);
114
			}
115
			
116
			$corner = $corner->rotate(90);
117
			if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM) {
118
				$result = $result->merge($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100);
119
			}
120
			
121
			return $result;
122
		}
123
	}
124
}
125