Completed
Push — master ( 3cb15c...9044bc )
by smiley
02:37
created

QRImage::dump()   C

Complexity

Conditions 12
Paths 64

Size

Total Lines 64
Code Lines 46

Duplication

Lines 17
Ratio 26.56 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 17
loc 64
rs 6.0561
cc 12
eloc 46
nc 64
nop 0

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
 * Class QRImage
4
 *
5
 * @filesource   QRImage.php
6
 * @created      05.12.2015
7
 * @package      chillerlan\QRCode\Output
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2015 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Output;
14
15
use chillerlan\QRCode\QRCode;
16
17
/**
18
 *
19
 */
20
class QRImage extends QROutputAbstract{
21
22
	/**
23
	 * @var \chillerlan\QRCode\Output\QRImageOptions $outputOptions
24
	 */
25
	protected $options;
26
27
	/**
28
	 * @var \chillerlan\QRCode\Output\QRImageOptions $outputOptions
29
	 */
30
	public function __construct(QRImageOptions $outputOptions = null){
31
		$this->options = $outputOptions;
32
33
		if(!$this->options instanceof QRImageOptions){
34
			$this->options = new QRImageOptions;
35
		}
36
37
		// clamp input (determine sane values!)
38
		$this->options->pixelSize = max(1, min(25, (int)$this->options->pixelSize));
39
		$this->options->marginSize = max(0, min(25, (int)$this->options->marginSize));
40
41
		foreach(['fgRed', 'fgGreen', 'fgBlue', 'bgRed', 'bgGreen', 'bgBlue'] as $val){
42
			$this->options->{$val} = max(0, min(255, (int)$this->options->{$val}));
43
		}
44
45
	}
46
47
	/**
48
	 * @return string
49
	 */
50
	public function dump(){
51
		$length     = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2;
52
		$image      = imagecreatetruecolor($length, $length);
53
		$foreground = imagecolorallocate($image, $this->options->fgRed, $this->options->fgGreen, $this->options->fgBlue);
54
		$background = imagecolorallocate($image, $this->options->bgRed, $this->options->bgGreen, $this->options->bgBlue);
55
56
		if((bool)$this->options->transparent && $this->options->type !== QRCode::OUTPUT_IMAGE_JPG){
57
			imagecolortransparent($image, $background);
58
		}
59
60
		imagefilledrectangle($image, 0, 0, $length, $length, $background);
61
62
		foreach($this->matrix as $r => $row){
63
			foreach($row as $c => $pixel){
64
				if($pixel){
65
					imagefilledrectangle($image,
66
						$this->options->marginSize +  $c      * $this->options->pixelSize,
67
						$this->options->marginSize +  $r      * $this->options->pixelSize,
68
						$this->options->marginSize + ($c + 1) * $this->options->pixelSize - 1,
69
						$this->options->marginSize + ($r + 1) * $this->options->pixelSize - 1,
70
						$foreground);
71
				}
72
			}
73
		}
74
75
		ob_start();
76
77
		switch($this->options->type){
78 View Code Duplication
			case QRCode::OUTPUT_IMAGE_JPG:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
				imagejpeg(
80
					$image,
81
					$this->options->cachefile,
82
					in_array($this->options->jpegQuality, range(0, 100), true)
83
						? $this->options->jpegQuality
84
						: 85
85
				);
86
				break;
87
			case QRCode::OUTPUT_IMAGE_GIF: /** Actually, it's pronounced "DJIFF". *hides* */
1 ignored issue
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
88
				imagegif(
89
					$image,
90
					$this->options->cachefile
91
				);
92
				break;
93
			case QRCode::OUTPUT_IMAGE_PNG:
94 View Code Duplication
			default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
				imagepng(
96
					$image,
97
					$this->options->cachefile,
98
					in_array($this->options->pngCompression, range(-1, 9), true)
99
						? $this->options->pngCompression
100
						: -1
101
				);
102
		}
103
104
		$imageData = ob_get_contents();
105
		imagedestroy($image);
106
		ob_end_clean();
107
108
		if((bool)$this->options->base64){
109
			$imageData = 'data:image/'.$this->options->type.';base64,'.base64_encode($imageData);
110
		}
111
112
		return $imageData;
113
	}
114
115
}
116