Completed
Push — develop ( 92230b...218aa8 )
by Peter
02:10
created

WhitespaceCropper::execute()   D

Complexity

Conditions 13
Paths 256

Size

Total Lines 67
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 67
rs 4.3252
cc 13
eloc 44
nc 256
nop 1

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
 * Webino (https://github.com/webino/)
4
 *
5
 * @link      https://github.com/webino/WebinoImageThumb/ for the canonical source repository
6
 * @copyright Copyright (c) 2013-2014 Webino, s. r. o. (http://webino.sk/)
7
 * @license   BSD-3-Clause
8
 */
9
10
namespace WebinoImageThumb\PhpThumb\Plugin;
11
12
use PHPThumb\GD as PHPThumb;
13
14
/**
15
 * Crop a whitespace around image
16
 *
17
 * @author Peter Bačinský <[email protected]>
18
 */
19
class WhitespaceCropper implements \PHPThumb\PluginInterface
20
{
21
    /**
22
     * @var int
23
     */
24
    protected $margin = 0;
25
26
    /**
27
     * @var int
28
     */
29
    protected $color = 0xFFFFFF;
30
31
    /**
32
     *
33
     * @param int $margin
34
     * @param int $color
35
     */
36
    public function __construct($margin = 0, $color = null)
37
    {
38
        empty($margin)  or $this->margin = $margin;
39
        is_null($color) or $this->color  = $color;
40
    }
41
42
    /**
43
     * @param PHPThumb $phpthumb
44
     * @return PHPThumb
45
     */
46
    public function execute($phpthumb)
47
    {
48
        $currentDimensions = $phpthumb->getCurrentDimensions();
49
        $oldImage          = $phpthumb->getOldImage();
50
51
        $borderTop = 0;
52
        for (; $borderTop < imagesy($oldImage); ++$borderTop) {
53
            for ($x = 0; $x < imagesx($oldImage); ++$x) {
54
                if (imagecolorat($oldImage, $x, $borderTop) !== $this->color) {
55
                    $borderTop -= $this->margin;
56
                    break 2;
57
                }
58
            }
59
        }
60
61
        $borderBottom = 0;
62
        for (; $borderBottom < imagesy($oldImage); ++$borderBottom) {
63
            for ($x = 0; $x < imagesx($oldImage); ++$x) {
64
                if (imagecolorat($oldImage, $x, imagesy($oldImage) - $borderBottom - 1) != $this->color) {
65
                    $borderBottom -= $this->margin;
66
                    break 2;
67
                }
68
            }
69
        }
70
71
        $borderLeft = 0;
72
        for (; $borderLeft < imagesx($oldImage); ++$borderLeft) {
73
            for ($y = 0; $y < imagesy($oldImage); ++$y) {
74
                if (imagecolorat($oldImage, $borderLeft, $y) !== $this->color) {
75
                    $borderLeft -= $this->margin;
76
                    break 2;
77
                }
78
            }
79
        }
80
81
        $borderRight = 0;
82
        for (; $borderRight < imagesx($oldImage); ++$borderRight) {
83
            for ($y = 0; $y < imagesy($oldImage); ++$y) {
84
                if (imagecolorat($oldImage, imagesx($oldImage) - $borderRight - 1, $y) !== $this->color) {
85
                    $borderRight -= $this->margin;
86
                    break 2;
87
                }
88
            }
89
        }
90
91
        $width        = imagesx($oldImage) - ($borderLeft + $borderRight);
92
        $height       = imagesy($oldImage) - ($borderTop + $borderBottom);
93
        $workingImage = imagecreatetruecolor($width, $height);
94
95
        imagecopy(
96
            $workingImage,
97
            $oldImage,
98
            0,
99
            0,
100
            $borderLeft,
101
            $borderTop,
102
            $width,
103
            $height
104
        );
105
106
        $phpthumb->setOldImage($workingImage);
0 ignored issues
show
Documentation introduced by
$workingImage is of type resource, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
        $currentDimensions['width']  = $width;
108
        $currentDimensions['height'] = $height;
109
        $phpthumb->setCurrentDimensions($currentDimensions);
110
111
        return $phpthumb;
112
    }
113
}
114