Passed
Push — master ( 514bca...891b04 )
by smiley
02:57
created

QRMarkup::svg()   B

Complexity

Conditions 10
Paths 42

Size

Total Lines 58
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 28
nc 42
nop 0
dl 0
loc 58
rs 7.6666
c 0
b 0
f 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 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
use function is_string, sprintf, strip_tags, trim;
18
19
/**
20
 * Converts the matrix into markup types: HTML, SVG, ...
21
 */
22
class QRMarkup extends QROutputAbstract{
23
24
	protected string $defaultMode = QRCode::OUTPUT_MARKUP_SVG;
25
26
	/**
27
	 * @see \sprintf()
28
	 */
29
	protected string $svgHeader = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="qr-svg %1$s" style="width: 100%%; height: auto;" viewBox="0 0 %2$d %2$d">';
30
31
	/**
32
	 * @inheritDoc
33
	 */
34
	protected function setModuleValues():void{
35
36
		foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
37
			$v = $this->options->moduleValues[$M_TYPE] ?? null;
38
39
			if(!is_string($v)){
40
				$this->moduleValues[$M_TYPE] = $defaultValue
41
					? $this->options->markupDark
42
					: $this->options->markupLight;
43
			}
44
			else{
45
				$this->moduleValues[$M_TYPE] = trim(strip_tags($v), '\'"');
46
			}
47
48
		}
49
50
	}
51
52
	/**
53
	 *
54
	 */
55
	protected function html():string{
56
		$html = '<div class="'.$this->options->cssClass.'">'.$this->options->eol;
57
58
		foreach($this->matrix->matrix() as $row){
59
			$html .= '<div>';
60
61
			foreach($row as $M_TYPE){
62
				$html .= '<span style="background: '.$this->moduleValues[$M_TYPE].';"></span>';
63
			}
64
65
			$html .= '</div>'.$this->options->eol;
66
		}
67
68
		$html .= '</div>'.$this->options->eol;
69
70
		if($this->options->cachefile){
71
			return '<!DOCTYPE html><head><meta charset="UTF-8"></head><body>'.$this->options->eol.$html.'</body>';
72
		}
73
74
		return $html;
75
	}
76
77
	/**
78
	 * @link https://github.com/codemasher/php-qrcode/pull/5
79
	 */
80
	protected function svg():string{
81
		$matrix = $this->matrix->matrix();
82
83
		$svg = sprintf($this->svgHeader, $this->options->cssClass, $this->options->svgViewBoxSize ?? $this->moduleCount)
84
		       .$this->options->eol
85
		       .'<defs>'.$this->options->svgDefs.'</defs>'
86
		       .$this->options->eol;
87
88
		foreach($this->moduleValues as $M_TYPE => $value){
89
			$path = '';
90
91
			foreach($matrix as $y => $row){
92
				//we'll combine active blocks within a single row as a lightweight compression technique
93
				$start = null;
94
				$count = 0;
95
96
				foreach($row as $x => $module){
97
98
					if($module === $M_TYPE){
99
						$count++;
100
101
						if($start === null){
102
							$start = $x;
103
						}
104
105
						if(isset($row[$x + 1])){
106
							continue;
107
						}
108
					}
109
110
					if($count > 0){
111
						$len = $count;
112
						$path .= sprintf('M%s %s h%s v1 h-%sZ ', $start, $y, $len, $len);
113
114
						// reset count
115
						$count = 0;
116
						$start = null;
117
					}
118
119
				}
120
121
			}
122
123
			if(!empty($path)){
124
				$svg .= sprintf('<path class="qr-%s %s" stroke="transparent" fill="%s" fill-opacity="%s" d="%s" />', $M_TYPE, $this->options->cssClass, $value, $this->options->svgOpacity, $path);
125
			}
126
127
		}
128
129
		// close svg
130
		$svg .= '</svg>'.$this->options->eol;
131
132
		// if saving to file, append the correct headers
133
		if($this->options->cachefile){
134
			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;
135
		}
136
137
		return $svg;
138
	}
139
140
}
141