Completed
Push — master ( 693e1e...7ea7b3 )
by smiley
03:06
created

QRImage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 77
Duplicated Lines 24.68 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 19
loc 77
rs 10
c 2
b 0
f 1
wmc 12
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
C dump() 0 41 7
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
 *
19
 */
20
class QRImage extends QROutputAbstract{
21
22
	/**
23
	 * @todo
24
	 * @return string
25
	 */
26
	public function dump():string {
27
		$length     = ($this->moduleCount + ($this->options->addQuietzone ? 8 : 0)) * $this->options->scale;
28
		$image      = imagecreatetruecolor($length, $length);
29
		$background = imagecolorallocate($image, 255, 255, 255);
30
31
		if((bool)$this->options->imageTransparent && $this->options->outputType !== QRCode::OUTPUT_IMAGE_JPG){
32
			imagecolortransparent($image, $background);
33
		}
34
35
		imagefilledrectangle($image, 0, 0, $length, $length, $background);
36
37
		foreach($this->matrix->matrix() as $r => $row){
38
			foreach($row as $c => $pixel){
39
				list($red, $green, $blue) = $this->options->moduleValues[$pixel];
40
41
				imagefilledrectangle(
42
					$image,
43
					 $c      * $this->options->scale,
44
					 $r      * $this->options->scale,
45
					($c + 1) * $this->options->scale - 1,
46
					($r + 1) * $this->options->scale - 1,
47
					imagecolorallocate($image, $red, $green, $blue)
48
				);
49
50
			}
51
		}
52
53
		ob_start();
54
55
		$this->{$this->options->outputType ?? 'png'}($image);
56
		$imageData = ob_get_contents();
57
		imagedestroy($image);
58
59
		ob_end_clean();
60
61
		if((bool)$this->options->imageBase64){
62
			$imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
63
		}
64
65
		return $imageData;
66
	}
67
68 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...
69
		imagepng(
70
			$image,
71
			$this->options->cachefile,
72
			in_array($this->options->pngCompression, range(-1, 9), true)
73
				? $this->options->pngCompression
74
				: -1
75
		);
76
77
	}
78
79
	/**
80
	 * Jiff - like... JitHub!
81
	 */
82
	protected function gif(&$image){
83
		imagegif($image, $this->options->cachefile);
84
	}
85
86 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...
87
		imagejpeg(
88
			$image,
89
			$this->options->cachefile,
90
			in_array($this->options->jpegQuality, range(0, 100), true)
91
				? $this->options->jpegQuality
92
				: 85
93
		);
94
	}
95
96
}
97