Completed
Push — master ( 645168...459db1 )
by smiley
03:19
created

QRMarkup   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 113
Duplicated Lines 8.85 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dump() 10 10 2
B html() 0 19 5
C svg() 0 67 12

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 QRMarkup
4
 *
5
 * @filesource   QRMarkup.php
6
 * @created      17.12.2016
7
 * @package      chillerlan\QRCode\Output
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2016 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Output;
14
15
use chillerlan\QRCode\QRCode;
16
17
/**
18
 * Converts the matrix into markup types: HTML, SVG, ...
19
 */
20
class QRMarkup extends QROutputAbstract{
21
22
	/**
23
	 * @return string
24
	 */
25 View Code Duplication
	public function dump(){
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...
26
27
		$data = call_user_func([$this, $this->outputMode ?? QRCode::OUTPUT_MARKUP_SVG]);
28
29
		if($this->options->cachefile !== null){
30
			$this->saveToFile($data);
31
		}
32
33
		return $data;
34
	}
35
36
	/**
37
	 * @return string|bool
38
	 */
39
	protected function html(){
40
		$html = '';
41
42
		foreach($this->matrix->matrix() as $row){
43
			$html .= '<div>';
44
45
			foreach($row as $pixel){
46
				$html .= '<span style="background: '.($this->options->moduleValues[$pixel] ?: 'lightgrey').';"></span>';
47
			}
48
49
			$html .= '</div>'.$this->options->eol;
50
		}
51
52
		if($this->options->cachefile){
53
			return '<!DOCTYPE html><head><meta charset="UTF-8"></head><body>'.$this->options->eol.$html.'</body>';
54
		}
55
56
		return $html;
57
	}
58
59
	/**
60
	 * @link https://github.com/codemasher/php-qrcode/pull/5
61
	 *
62
	 * @return string|bool
63
	 */
64
	protected function svg(){
65
		$scale  = $this->options->scale;
66
		$length = $this->moduleCount * $scale;
67
		$matrix = $this->matrix->matrix();
68
69
		$svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="'.$length.'px" height="'.$length.'px">'
70
		       .$this->options->eol
71
		       .'<defs>'.$this->options->svgDefs.'</defs>'
72
		       .$this->options->eol;
73
74
		foreach($this->options->moduleValues as $M_TYPE => $value){
75
76
			// fallback
77
			if(is_bool($value)){
78
				$value = $value ? '#000' : '#fff';
79
			}
80
81
			$path = '';
82
83
			foreach($matrix as $y => $row){
84
				//we'll combine active blocks within a single row as a lightweight compression technique
85
				$start = null;
86
				$count = 0;
87
88
				foreach($row as $x => $module){
89
90
					if($module === $M_TYPE){
91
						$count++;
92
93
						if($start === null){
94
							$start = $x * $scale;
95
						}
96
97
						if($row[$x + 1] ?? false){
98
							continue;
99
						}
100
					}
101
102
					if($count > 0){
103
						$len = $count * $scale;
104
						$path .= 'M' .$start. ' ' .($y * $scale). ' h'.$len.' v'.$scale.' h-'.$len.'Z ';
105
106
						// reset count
107
						$count = 0;
108
						$start = null;
109
					}
110
111
				}
112
113
			}
114
115
			if(!empty($path)){
116
				$svg .= '<path class="qr-'.$M_TYPE.' '.$this->options->cssClass.'" stroke="transparent" fill="'.$value.'" fill-opacity="'.$this->options->svgOpacity.'" d="'.$path.'" />';
117
			}
118
119
		}
120
121
		// close svg
122
		$svg .= '</svg>'.$this->options->eol;
123
124
		// if saving to file, append the correct headers
125
		if($this->options->cachefile){
126
			return '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'.$this->options->eol.$svg;
127
		}
128
129
		return $svg;
130
	}
131
132
}
133