Completed
Push — master ( 7ea7b3...88f800 )
by smiley
01:49
created

QRImage   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 24.36 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 19
loc 78
rs 10
c 2
b 0
f 1
wmc 11
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
B dump() 0 35 6
A png() 10 10 2
A gif() 0 3 1
A jpg() 9 9 2

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
 * 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
 * Converts the matrix into images, raw or base64 output
19
 */
20
class QRImage extends QROutputAbstract{
21
22
	/**
23
	 * @return string
24
	 */
25
	public function dump():string{
26
		$scale      = $this->options->scale;
27
		$length     = $this->moduleCount * $scale;
28
		$image      = imagecreatetruecolor($length, $length);
29
		$background = imagecolorallocate($image, ...$this->options->imageTransparencyBG);
30
31
		if((bool)$this->options->imageTransparent && in_array($this->options->outputType, [QRCode::OUTPUT_IMAGE_PNG, QRCode::OUTPUT_IMAGE_GIF,], true)){
32
			imagecolortransparent($image, $background);
33
		}
34
35
		imagefilledrectangle($image, 0, 0, $length, $length, $background);
36
37
		foreach($this->matrix->matrix() as $y => $row){
38
			foreach($row as $x => $pixel){
39
				$color = imagecolorallocate($image, ...$this->options->moduleValues[$pixel]);
40
41
				imagefilledrectangle($image, $x * $scale, $y * $scale, ($x + 1) * $scale - 1, ($y + 1) * $scale - 1, $color);
42
			}
43
		}
44
45
		ob_start();
46
47
		call_user_func_array([$this, $this->options->outputType ?? QRCode::OUTPUT_IMAGE_PNG], [&$image]);
48
49
		$imageData = ob_get_contents();
50
		imagedestroy($image);
51
52
		ob_end_clean();
53
54
		if((bool)$this->options->imageBase64){
55
			$imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
56
		}
57
58
		return $imageData;
59
	}
60
61
	/**
62
	 * @param $image
63
	 */
64 View Code Duplication
	protected function png(&$image){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
65
		imagepng(
66
			$image,
67
			$this->options->cachefile,
68
			in_array($this->options->pngCompression, range(-1, 9), true)
69
				? $this->options->pngCompression
70
				: -1
71
		);
72
73
	}
74
75
	/**
76
	 * Jiff - like... JitHub!
77
	 *
78
	 * @param $image
79
	 */
80
	protected function gif(&$image){
81
		imagegif($image, $this->options->cachefile);
82
	}
83
84
	/**
85
	 * @param $image
86
	 */
87 View Code Duplication
	protected function jpg(&$image){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
88
		imagejpeg(
89
			$image,
90
			$this->options->cachefile,
91
			in_array($this->options->jpegQuality, range(0, 100), true)
92
				? $this->options->jpegQuality
93
				: 85
94
		);
95
	}
96
97
}
98