Crop::execute()   B
last analyzed

Complexity

Conditions 11
Paths 80

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 47
rs 7.3166
c 0
b 0
f 0
cc 11
nc 80
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
##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\Coordinate;
27
use WideImage\PaletteImage;
28
use WideImage\Exception\Exception;
29
use WideImage\Exception\GDFunctionResultException;
30
31
/**
32
 * Crop operation class
33
 * 
34
 * @package Internal/Operations
35
 */
36
class Crop
37
{
38
	/**
39
	 * Returns a cropped image
40
	 *
41
	 * @param \WideImage\Image $img
42
	 * @param smart_coordinate $left
0 ignored issues
show
Bug introduced by
The type WideImage\Operation\smart_coordinate was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
	 * @param smart_coordinate $top
44
	 * @param smart_coordinate $width
45
	 * @param smart_coordinate $height
46
	 * @return \WideImage\Image
47
	 */
48
	public function execute($img, $left, $top, $width, $height)
49
	{
50
		$width  = Coordinate::fix($width, $img->getWidth(), $width);
0 ignored issues
show
Bug introduced by
$width of type WideImage\Operation\smart_coordinate is incompatible with the type integer expected by parameter $sec_dim of WideImage\Coordinate::fix(). ( Ignorable by Annotation )

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

50
		$width  = Coordinate::fix($width, $img->getWidth(), /** @scrutinizer ignore-type */ $width);
Loading history...
51
		$height = Coordinate::fix($height, $img->getHeight(), $height);
52
		$left   = Coordinate::fix($left, $img->getWidth(), $width);
53
		$top    = Coordinate::fix($top, $img->getHeight(), $height);
54
		
55
		if ($left < 0) {
56
			$width = $left + $width;
57
			$left  = 0;
58
		}
59
		
60
		if ($width > $img->getWidth() - $left) {
61
			$width = $img->getWidth() - $left;
62
		}
63
		
64
		if ($top < 0) {
65
			$height = $top + $height;
66
			$top    = 0;
67
		}
68
		
69
		if ($height > $img->getHeight() - $top) {
70
			$height = $img->getHeight() - $top;
71
		}
72
		
73
		if ($width <= 0 || $height <= 0) {
74
			throw new Exception("Can't crop outside of an image.");
75
		}
76
		
77
		$new = $img->doCreate($width, $height);
0 ignored issues
show
Bug introduced by
The method doCreate() does not exist on WideImage\Image. 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

77
		/** @scrutinizer ignore-call */ 
78
  $new = $img->doCreate($width, $height);
Loading history...
78
		
79
		if ($img->isTransparent() || $img instanceof PaletteImage) {
80
			$new->copyTransparencyFrom($img);
81
			
82
			if (!imagecopyresized($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height)) {
83
				throw new GDFunctionResultException("imagecopyresized() returned false");
84
			}
85
		} else {
86
			$new->alphaBlending(false);
0 ignored issues
show
Bug introduced by
The method alphaBlending() does not exist on WideImage\Image. 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

86
			$new->/** @scrutinizer ignore-call */ 
87
         alphaBlending(false);
Loading history...
87
			$new->saveAlpha(true);
0 ignored issues
show
Bug introduced by
The method saveAlpha() does not exist on WideImage\Image. 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

87
			$new->/** @scrutinizer ignore-call */ 
88
         saveAlpha(true);
Loading history...
88
			
89
			if (!imagecopyresampled($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height)) {
90
				throw new GDFunctionResultException("imagecopyresampled() returned false");
91
			}
92
		}
93
		
94
		return $new;
95
	}
96
}
97