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

PaletteImage   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 119
Duplicated Lines 15.13 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 17
lcom 1
cbo 6
dl 18
loc 119
rs 10
c 1
b 0
f 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 8 8 3
A doCreate() 0 4 1
A isTrueColor() 0 4 1
A asPalette() 0 4 1
B copyAsNew() 0 26 4
A asTrueColor() 0 16 3
A getChannels() 10 10 3
A copyNoAlpha() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 WideImage
22
  **/
23
24
namespace WideImage;
25
26
use WideImage\Exception\InvalidImageDimensionException;
27
use WideImage\Exception\GDFunctionResultException;
28
29
/**
30
 * @package WideImage
31
 */
32
class PaletteImage extends Image
33
{
34
	/**
35
	 * Create a palette image
36
	 *
37
	 * @param int $width
38
	 * @param int $height
39
	 * @return \WideImage\PaletteImage
40
	 */
41 View Code Duplication
	public static function create($width, $height)
42
	{
43
		if ($width * $height <= 0 || $width < 0) {
44
			throw new InvalidImageDimensionException("Can't create an image with dimensions [$width, $height].");
45
		}
46
		
47
		return new PaletteImage(imagecreate($width, $height));
48
	}
49
	
50
	public function doCreate($width, $height)
51
	{
52
		return static::create($width, $height);
53
	}
54
	
55
	/**
56
	 * (non-PHPdoc)
57
	 * @see \WideImage\Image#isTrueColor()
58
	 */
59
	public function isTrueColor()
60
	{
61
		return false;
62
	}
63
	
64
	/**
65
	 * (non-PHPdoc)
66
	 * @see \WideImage\Image#asPalette($nColors, $dither, $matchPalette)
67
	 */
68
	public function asPalette($nColors = 255, $dither = null, $matchPalette = true)
69
	{
70
		return $this->copy();
71
	}
72
	
73
	/**
74
	 * Returns a copy of the image
75
	 * 
76
	 * @param $trueColor True if the new image should be truecolor
77
	 * @return \WideImage\Image
78
	 */
79
	protected function copyAsNew($trueColor = false)
80
	{
81
		$width = $this->getWidth();
82
		$height = $this->getHeight();
83
		
84
		if ($trueColor) {
85
			$new = TrueColorImage::create($width, $height);
86
		} else {
87
			$new = PaletteImage::create($width, $height);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
88
		}
89
		
90
		// copy transparency of source to target
91
		if ($this->isTransparent()) {
92
			$rgb = $this->getTransparentColorRGB();
93
			
94
			if (is_array($rgb)) {
95
				$tci = $new->allocateColor($rgb['red'], $rgb['green'], $rgb['blue']);
96
				$new->fill(0, 0, $tci);
97
				$new->setTransparentColor($tci);
98
			}
99
		}
100
		
101
		imageCopy($new->getHandle(), $this->handle, 0, 0, 0, 0, $width, $height);
102
		
103
		return $new;
104
	}
105
	
106
	/**
107
	 * (non-PHPdoc)
108
	 * @see \WideImage\Image#asTrueColor()
109
	 */
110
	public function asTrueColor()
111
	{
112
		$width  = $this->getWidth();
113
		$height = $this->getHeight();
114
		$new    = WideImage::createTrueColorImage($width, $height);
115
		
116
		if ($this->isTransparent()) {
117
			$new->copyTransparencyFrom($this);
118
		}
119
		
120
		if (!imageCopy($new->getHandle(), $this->handle, 0, 0, 0, 0, $width, $height)) {
121
			throw new GDFunctionResultException("imagecopy() returned false");
122
		}
123
		
124
		return $new;
125
	}
126
	
127
	/**
128
	 * (non-PHPdoc)
129
	 * @see \WideImage\Image#getChannels()
130
	 */
131 View Code Duplication
	public function getChannels()
132
	{
133
		$args = func_get_args();
134
		
135
		if (count($args) == 1 && is_array($args[0])) {
136
			$args = $args[0];
137
		}
138
		
139
		return OperationFactory::get('CopyChannelsPalette')->execute($this, $args);
140
	}
141
	
142
	/**
143
	 * (non-PHPdoc)
144
	 * @see \WideImage\Image#copyNoAlpha()
145
	 */
146
	public function copyNoAlpha()
147
	{
148
		return WideImage::loadFromString($this->asString('png'));
149
	}
150
}
151