Completed
Push — master ( d072d1...74ad36 )
by Michael
11:11
created

ResizeCanvas   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 0
cbo 4
dl 0
loc 76
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C execute() 0 58 12
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
use WideImage\Coordinate;
28
29
/**
30
 * ResizeCanvas operation class
31
 * 
32
 * @package Internal/Operations
33
 */
34
class ResizeCanvas
35
{
36
	/**
37
	 * Returns an image with a resized canvas
38
	 * 
39
	 * The image is filled with $color. Use $scale to determine, when to resize.
40
	 *
41
	 * @param \WideImage\Image $img
42
	 * @param smart_coordinate $width
43
	 * @param smart_coordinate $height
44
	 * @param smart_coordinate $left
45
	 * @param smart_coordinate $top
46
	 * @param int $color
47
	 * @param string $scale 'up', 'down', 'any'
48
	 * @param boolean $merge
49
	 * @return \WideImage\Image
50
	 */
51
	public function execute($img, $width, $height, $left, $top, $color, $scale, $merge)
52
	{
53
		$new_width  = Coordinate::fix($width, $img->getWidth());
54
		$new_height = Coordinate::fix($height, $img->getHeight());
55
		
56
		if ($scale == 'down') {
57
			$new_width  = min($new_width, $img->getWidth());
58
			$new_height = min($new_height, $img->getHeight());
59
		} elseif ($scale == 'up') {
60
			$new_width  = max($new_width, $img->getWidth());
61
			$new_height = max($new_height, $img->getHeight());
62
		}
63
		
64
		$new = WideImage::createTrueColorImage($new_width, $new_height);
65
		
66
		if ($img->isTrueColor()) {
67
			if ($color === null) {
68
				$color = $new->allocateColorAlpha(0, 0, 0, 127);
69
			}
70
		} else {
71
			imagepalettecopy($new->getHandle(), $img->getHandle());
72
			
73
			if ($img->isTransparent()) {
74
				$new->copyTransparencyFrom($img);
75
				$tc_rgb  = $img->getTransparentColorRGB();
76
				$t_color = $new->allocateColorAlpha($tc_rgb);
77
			}
78
			
79
			if ($color === null) {
80
				if ($img->isTransparent()) {
81
					$color = $t_color;
0 ignored issues
show
Bug introduced by
The variable $t_color does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
82
				} else {
83
					$color = $new->allocateColorAlpha(255, 0, 127, 127);
84
				}
85
				
86
				imagecolortransparent($new->getHandle(), $color);
87
			}
88
		}
89
		
90
		$new->fill(0, 0, $color);
91
		
92
		$x = Coordinate::fix($left, $new->getWidth(), $img->getWidth());
93
		$y = Coordinate::fix($top, $new->getHeight(), $img->getHeight());
94
		
95
		// blending for truecolor images
96
		if ($img->isTrueColor()) {
97
			$new->alphaBlending($merge);
0 ignored issues
show
Unused Code introduced by
The call to the method WideImage\TrueColorImage::alphaBlending() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
98
		}
99
		
100
		// not-blending for palette images
101
		if (!$merge && !$img->isTrueColor() && isset($t_color)) {
102
			$new->getCanvas()->filledRectangle($x, $y, $x + $img->getWidth(), $y + $img->getHeight(), $t_color);
0 ignored issues
show
Documentation Bug introduced by
The method filledRectangle 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...
103
		}
104
		
105
		$img->copyTo($new, $x, $y);
106
		
107
		return $new;
108
	}
109
}
110