Completed
Push — master ( 2b7a2e...74ad36 )
by Michael
11:15
created

RoundCorners::execute()   D

Complexity

Conditions 29
Paths 144

Size

Total Lines 82
Code Lines 52

Duplication

Lines 24
Ratio 29.27 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 24
loc 82
rs 4.5787
c 1
b 0
f 1
cc 29
eloc 52
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) {
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);
0 ignored issues
show
Documentation Bug introduced by
The method filledEllipse does not exist on object<WideImage\Canvas>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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 View Code Duplication
			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 View Code Duplication
			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);
0 ignored issues
show
Unused Code introduced by
The call to Image::applyMask() has too many arguments starting with 100.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
80
			}
81
			
82
			$corner = $corner->rotate(90);
83 View Code Duplication
			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);
0 ignored issues
show
Unused Code introduced by
The call to Image::applyMask() has too many arguments starting with 100.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
85
			}
86
			
87
			$corner = $corner->rotate(90);
88 View Code Duplication
			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);
0 ignored issues
show
Unused Code introduced by
The call to Image::applyMask() has too many arguments starting with 100.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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
Documentation Bug introduced by
The method filledEllipse does not exist on object<WideImage\Canvas>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
99
			$corner = $corner->resize($radius, $radius);
100
			
101
			$result = $image->copy();
102 View Code Duplication
			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 View Code Duplication
			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 View Code Duplication
			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 View Code Duplication
			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