Passed
Push — master ( fa12b4...2a73a2 )
by smiley
02:29
created

QRImage::dump()   D

Complexity

Conditions 13
Paths 128

Size

Total Lines 74
Code Lines 50

Duplication

Lines 17
Ratio 22.97 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 17
loc 74
rs 4.9869
cc 13
eloc 50
nc 128
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 string
24
	 */
25
	protected $optionsInterface = QRImageOptions::class;
26
27
	/**
28
	 * @var array
29
	 */
30
	protected $types = [
31
		QRCode::OUTPUT_IMAGE_GIF,
32
		QRCode::OUTPUT_IMAGE_JPG,
33
		QRCode::OUTPUT_IMAGE_PNG,
34
	];
35
36
	/**
37
	 * @return string
38
	 */
39
	public function dump():string {
40
		// clamp input (@todo: determine sane values!)
41
		$this->options->pixelSize = max(1, min(25, (int)$this->options->pixelSize));
42
		$this->options->marginSize = max(0, min(25, (int)$this->options->marginSize));
43
44
		foreach(['fgRed', 'fgGreen', 'fgBlue', 'bgRed', 'bgGreen', 'bgBlue'] as $val){
45
			$this->options->{$val} = max(0, min(255, (int)$this->options->{$val}));
46
		}
47
48
		$length     = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2;
49
		$image      = imagecreatetruecolor($length, $length);
50
		$foreground = imagecolorallocate($image, $this->options->fgRed, $this->options->fgGreen, $this->options->fgBlue);
51
		$background = imagecolorallocate($image, $this->options->bgRed, $this->options->bgGreen, $this->options->bgBlue);
52
53
		if((bool)$this->options->transparent && $this->options->type !== QRCode::OUTPUT_IMAGE_JPG){
54
			imagecolortransparent($image, $background);
55
		}
56
57
		imagefilledrectangle($image, 0, 0, $length, $length, $background);
58
59
		foreach($this->matrix as $r => $row){
60
			foreach($row as $c => $pixel){
61
				if($pixel){
62
					imagefilledrectangle($image,
63
						$this->options->marginSize +  $c      * $this->options->pixelSize,
64
						$this->options->marginSize +  $r      * $this->options->pixelSize,
65
						$this->options->marginSize + ($c + 1) * $this->options->pixelSize - 1,
66
						$this->options->marginSize + ($r + 1) * $this->options->pixelSize - 1,
67
						$foreground);
68
				}
69
			}
70
		}
71
72
		ob_start();
73
74
		switch($this->options->type){
75 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...
76
				// @codeCoverageIgnoreStart
77
				imagejpeg(
78
					$image,
79
					$this->options->cachefile,
80
					in_array($this->options->jpegQuality, range(0, 100), true)
81
						? $this->options->jpegQuality
82
						: 85
83
				);
84
				break;
85
				// @codeCoverageIgnoreEnd
86
			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...
87
				imagegif(
88
					$image,
89
					$this->options->cachefile
90
				);
91
				break;
92
			case QRCode::OUTPUT_IMAGE_PNG:
93 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...
94
				imagepng(
95
					$image,
96
					$this->options->cachefile,
97
					in_array($this->options->pngCompression, range(-1, 9), true)
98
						? $this->options->pngCompression
99
						: -1
100
				);
101
		}
102
103
		$imageData = ob_get_contents();
104
		imagedestroy($image);
105
		ob_end_clean();
106
107
		if((bool)$this->options->base64){
108
			$imageData = 'data:image/'.$this->options->type.';base64,'.base64_encode($imageData);
109
		}
110
111
		return $imageData;
112
	}
113
114
}
115