Completed
Push — v3.2.x ( d8bf29...19c025 )
by smiley
01:36
created

QRMatrix::setLogoSpace()   D

Complexity

Conditions 19
Paths 85

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
nc 85
nop 4
dl 0
loc 51
rs 4.5166
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 QRMatrix
4
 *
5
 * @filesource   QRMatrix.php
6
 * @created      15.11.2017
7
 * @package      chillerlan\QRCode\Data
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Data;
14
15
use chillerlan\QRCode\QRCode;
16
use Closure;
17
18
use function array_fill, array_key_exists, array_push, array_unshift, count, floor, in_array, max, min, range;
19
20
/**
21
 * @link http://www.thonky.com/qr-code-tutorial/format-version-information
22
 */
23
class QRMatrix{
24
25
	public const M_NULL       = 0x00;
26
	public const M_DARKMODULE = 0x02;
27
	public const M_DATA       = 0x04;
28
	public const M_FINDER     = 0x06;
29
	public const M_SEPARATOR  = 0x08;
30
	public const M_ALIGNMENT  = 0x0a;
31
	public const M_TIMING     = 0x0c;
32
	public const M_FORMAT     = 0x0e;
33
	public const M_VERSION    = 0x10;
34
	public const M_QUIETZONE  = 0x12;
35
	public const M_LOGO       = 0x14;
36
	public const M_FINDER_DOT = 0x16;
37
38
	public const M_TEST       = 0xff;
39
40
	/**
41
	 * @link http://www.thonky.com/qr-code-tutorial/alignment-pattern-locations
42
	 *
43
	 *  version -> pattern
44
	 */
45
	protected const alignmentPattern = [
46
		1  => [],
47
		2  => [6, 18],
48
		3  => [6, 22],
49
		4  => [6, 26],
50
		5  => [6, 30],
51
		6  => [6, 34],
52
		7  => [6, 22, 38],
53
		8  => [6, 24, 42],
54
		9  => [6, 26, 46],
55
		10 => [6, 28, 50],
56
		11 => [6, 30, 54],
57
		12 => [6, 32, 58],
58
		13 => [6, 34, 62],
59
		14 => [6, 26, 46, 66],
60
		15 => [6, 26, 48, 70],
61
		16 => [6, 26, 50, 74],
62
		17 => [6, 30, 54, 78],
63
		18 => [6, 30, 56, 82],
64
		19 => [6, 30, 58, 86],
65
		20 => [6, 34, 62, 90],
66
		21 => [6, 28, 50, 72,  94],
67
		22 => [6, 26, 50, 74,  98],
68
		23 => [6, 30, 54, 78, 102],
69
		24 => [6, 28, 54, 80, 106],
70
		25 => [6, 32, 58, 84, 110],
71
		26 => [6, 30, 58, 86, 114],
72
		27 => [6, 34, 62, 90, 118],
73
		28 => [6, 26, 50, 74,  98, 122],
74
		29 => [6, 30, 54, 78, 102, 126],
75
		30 => [6, 26, 52, 78, 104, 130],
76
		31 => [6, 30, 56, 82, 108, 134],
77
		32 => [6, 34, 60, 86, 112, 138],
78
		33 => [6, 30, 58, 86, 114, 142],
79
		34 => [6, 34, 62, 90, 118, 146],
80
		35 => [6, 30, 54, 78, 102, 126, 150],
81
		36 => [6, 24, 50, 76, 102, 128, 154],
82
		37 => [6, 28, 54, 80, 106, 132, 158],
83
		38 => [6, 32, 58, 84, 110, 136, 162],
84
		39 => [6, 26, 54, 82, 110, 138, 166],
85
		40 => [6, 30, 58, 86, 114, 142, 170],
86
	];
87
88
	/**
89
	 * @link http://www.thonky.com/qr-code-tutorial/format-version-tables
90
	 *
91
	 * no version pattern for QR Codes < 7
92
	 */
93
	protected const versionPattern = [
94
		7  => 0b000111110010010100,
95
		8  => 0b001000010110111100,
96
		9  => 0b001001101010011001,
97
		10 => 0b001010010011010011,
98
		11 => 0b001011101111110110,
99
		12 => 0b001100011101100010,
100
		13 => 0b001101100001000111,
101
		14 => 0b001110011000001101,
102
		15 => 0b001111100100101000,
103
		16 => 0b010000101101111000,
104
		17 => 0b010001010001011101,
105
		18 => 0b010010101000010111,
106
		19 => 0b010011010100110010,
107
		20 => 0b010100100110100110,
108
		21 => 0b010101011010000011,
109
		22 => 0b010110100011001001,
110
		23 => 0b010111011111101100,
111
		24 => 0b011000111011000100,
112
		25 => 0b011001000111100001,
113
		26 => 0b011010111110101011,
114
		27 => 0b011011000010001110,
115
		28 => 0b011100110000011010,
116
		29 => 0b011101001100111111,
117
		30 => 0b011110110101110101,
118
		31 => 0b011111001001010000,
119
		32 => 0b100000100111010101,
120
		33 => 0b100001011011110000,
121
		34 => 0b100010100010111010,
122
		35 => 0b100011011110011111,
123
		36 => 0b100100101100001011,
124
		37 => 0b100101010000101110,
125
		38 => 0b100110101001100100,
126
		39 => 0b100111010101000001,
127
		40 => 0b101000110001101001,
128
	];
129
130
	// ECC level -> mask pattern
131
	protected const formatPattern = [
132
		[ // L
133
			0b111011111000100,
134
			0b111001011110011,
135
			0b111110110101010,
136
			0b111100010011101,
137
			0b110011000101111,
138
			0b110001100011000,
139
			0b110110001000001,
140
			0b110100101110110,
141
		],
142
		[ // M
143
			0b101010000010010,
144
			0b101000100100101,
145
			0b101111001111100,
146
			0b101101101001011,
147
			0b100010111111001,
148
			0b100000011001110,
149
			0b100111110010111,
150
			0b100101010100000,
151
		],
152
		[ // Q
153
			0b011010101011111,
154
			0b011000001101000,
155
			0b011111100110001,
156
			0b011101000000110,
157
			0b010010010110100,
158
			0b010000110000011,
159
			0b010111011011010,
160
			0b010101111101101,
161
		],
162
		[ // H
163
			0b001011010001001,
164
			0b001001110111110,
165
			0b001110011100111,
166
			0b001100111010000,
167
			0b000011101100010,
168
			0b000001001010101,
169
			0b000110100001100,
170
			0b000100000111011,
171
		],
172
	];
173
174
	/**
175
	 * @var int
176
	 */
177
	protected $version;
178
179
	/**
180
	 * @var int
181
	 */
182
	protected $eclevel;
183
184
	/**
185
	 * @var int
186
	 */
187
	protected $maskPattern = QRCode::MASK_PATTERN_AUTO;
188
189
	/**
190
	 * @var int
191
	 */
192
	protected $moduleCount;
193
194
	/**
195
	 * @var mixed[]
196
	 */
197
	protected $matrix;
198
199
	/**
200
	 * QRMatrix constructor.
201
	 *
202
	 * @param int $version
203
	 * @param int $eclevel
204
	 *
205
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
206
	 */
207
	public function __construct(int $version, int $eclevel){
208
209
		if(!in_array($version, range(1, 40), true)){
210
			throw new QRCodeDataException('invalid QR Code version');
211
		}
212
213
		if(!array_key_exists($eclevel, QRCode::ECC_MODES)){
214
			throw new QRCodeDataException('invalid ecc level');
215
		}
216
217
		$this->version     = $version;
218
		$this->eclevel     = $eclevel;
219
		$this->moduleCount = $this->version * 4 + 17;
220
		$this->matrix      = array_fill(0, $this->moduleCount, array_fill(0, $this->moduleCount, $this::M_NULL));
221
	}
222
223
	/**
224
	 * Returns the data matrix, returns a pure boolean representation if $boolean is set to true
225
	 *
226
	 * @return int[][]|bool[][]
227
	 */
228
	public function matrix(bool $boolean = false):array{
229
230
		if(!$boolean){
231
			return $this->matrix;
232
		}
233
234
		$matrix = [];
235
236
		foreach($this->matrix as $y => $row){
237
			$matrix[$y] = [];
238
239
			foreach($row as $x => $val){
240
				$matrix[$y][$x] = ($val >> 8) > 0;
241
			}
242
		}
243
244
		return $matrix;
245
	}
246
247
	/**
248
	 * @return int
249
	 */
250
	public function version():int{
251
		return $this->version;
252
	}
253
254
	/**
255
	 * @return int
256
	 */
257
	public function eccLevel():int{
258
		return $this->eclevel;
259
	}
260
261
	/**
262
	 * @return int
263
	 */
264
	public function maskPattern():int{
265
		return $this->maskPattern;
266
	}
267
268
	/**
269
	 * Returns the absoulute size of the matrix, including quiet zone (after setting it).
270
	 *
271
	 * size = version * 4 + 17 [ + 2 * quietzone size]
272
	 *
273
	 * @return int
274
	 */
275
	public function size():int{
276
		return $this->moduleCount;
277
	}
278
279
	/**
280
	 * Returns the value of the module at position [$x, $y]
281
	 *
282
	 * @param int $x
283
	 * @param int $y
284
	 *
285
	 * @return int
286
	 */
287
	public function get(int $x, int $y):int{
288
		return $this->matrix[$y][$x];
289
	}
290
291
	/**
292
	 * Sets the $M_TYPE value for the module at position [$x, $y]
293
	 *
294
	 *   true  => $M_TYPE << 8
295
	 *   false => $M_TYPE
296
	 *
297
	 * @param int  $x
298
	 * @param int  $y
299
	 * @param int  $M_TYPE
300
	 * @param bool $value
301
	 *
302
	 * @return \chillerlan\QRCode\Data\QRMatrix
303
	 */
304
	public function set(int $x, int $y, bool $value, int $M_TYPE):QRMatrix{
305
		$this->matrix[$y][$x] = $M_TYPE << ($value ? 8 : 0);
306
307
		return $this;
308
	}
309
310
	/**
311
	 * Checks whether a module is true (dark) or false (light)
312
	 *
313
	 *   true  => $value >> 8 === $M_TYPE
314
	 *            $value >> 8 > 0
315
	 *
316
	 *   false => $value === $M_TYPE
317
	 *            $value >> 8 === 0
318
	 *
319
	 * @param int $x
320
	 * @param int $y
321
	 *
322
	 * @return bool
323
	 */
324
	public function check(int $x, int $y):bool{
325
		return $this->matrix[$y][$x] >> 8 > 0;
326
	}
327
328
329
	/**
330
	 * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder
331
	 *
332
	 * @return \chillerlan\QRCode\Data\QRMatrix
333
	 */
334
	public function setDarkModule():QRMatrix{
335
		$this->set(8, 4 * $this->version + 9, true, $this::M_DARKMODULE);
336
337
		return $this;
338
	}
339
340
	/**
341
	 * Draws the 7x7 finder patterns in the corners top left/right and bottom left
342
	 *
343
	 * @return \chillerlan\QRCode\Data\QRMatrix
344
	 */
345
	public function setFinderPattern():QRMatrix{
346
347
		$pos = [
348
			[0, 0], // top left
349
			[$this->moduleCount - 7, 0], // bottom left
350
			[0, $this->moduleCount - 7], // top right
351
		];
352
353
		foreach($pos as $c){
354
			for($y = 0; $y < 7; $y++){
355
				for($x = 0; $x < 7; $x++){
356
					// outer (dark) 7*7 square
357
					if($x === 0 || $x === 6 || $y === 0 || $y === 6){
358
						$this->set($c[0] + $y, $c[1] + $x, true, $this::M_FINDER);
359
					}
360
					// inner (light) 5*5 square
361
					elseif($x === 1 || $x === 5 || $y === 1 || $y === 5){
362
						$this->set($c[0] + $y, $c[1] + $x, false, $this::M_FINDER);
363
					}
364
					// 3*3 dot
365
					else{
366
						$this->set($c[0] + $y, $c[1] + $x, true, $this::M_FINDER_DOT);
367
					}
368
				}
369
			}
370
		}
371
372
		return $this;
373
	}
374
375
	/**
376
	 * Draws the separator lines around the finder patterns
377
	 *
378
	 * @return \chillerlan\QRCode\Data\QRMatrix
379
	 */
380
	public function setSeparators():QRMatrix{
381
382
		$h = [
383
			[7, 0],
384
			[$this->moduleCount - 8, 0],
385
			[7, $this->moduleCount - 8],
386
		];
387
388
		$v = [
389
			[7, 7],
390
			[$this->moduleCount - 1, 7],
391
			[7, $this->moduleCount - 8],
392
		];
393
394
		for($c = 0; $c < 3; $c++){
395
			for($i = 0; $i < 8; $i++){
396
				$this->set($h[$c][0]     , $h[$c][1] + $i, false, $this::M_SEPARATOR);
397
				$this->set($v[$c][0] - $i, $v[$c][1]     , false, $this::M_SEPARATOR);
398
			}
399
		}
400
401
		return $this;
402
	}
403
404
405
	/**
406
	 * Draws the 5x5 alignment patterns
407
	 *
408
	 * @return \chillerlan\QRCode\Data\QRMatrix
409
	 */
410
	public function setAlignmentPattern():QRMatrix{
411
412
		foreach($this::alignmentPattern[$this->version] as $y){
413
			foreach($this::alignmentPattern[$this->version] as $x){
414
415
				// skip existing patterns
416
				if($this->matrix[$y][$x] !== $this::M_NULL){
417
					continue;
418
				}
419
420
				for($ry = -2; $ry <= 2; $ry++){
421
					for($rx = -2; $rx <= 2; $rx++){
422
						$v = ($ry === 0 && $rx === 0) || $ry === 2 || $ry === -2 || $rx === 2 || $rx === -2;
423
424
						$this->set($x + $rx, $y + $ry, $v, $this::M_ALIGNMENT);
425
					}
426
				}
427
428
			}
429
		}
430
431
		return $this;
432
	}
433
434
435
	/**
436
	 * Draws the timing pattern (h/v checkered line between the finder patterns)
437
	 *
438
	 * @return \chillerlan\QRCode\Data\QRMatrix
439
	 */
440
	public function setTimingPattern():QRMatrix{
441
442
		foreach(range(8, $this->moduleCount - 8 - 1) as $i){
443
444
			if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){
445
				continue;
446
			}
447
448
			$v = $i % 2 === 0;
449
450
			$this->set($i, 6, $v, $this::M_TIMING); // h
451
			$this->set(6, $i, $v, $this::M_TIMING); // v
452
		}
453
454
		return $this;
455
	}
456
457
	/**
458
	 * Draws the version information, 2x 3x6 pixel
459
	 *
460
	 * @param bool|null  $test
461
	 *
462
	 * @return \chillerlan\QRCode\Data\QRMatrix
463
	 */
464
	public function setVersionNumber(bool $test = null):QRMatrix{
465
		$bits = $this::versionPattern[$this->version] ?? false;
466
467
		if($bits !== false){
468
469
			for($i = 0; $i < 18; $i++){
470
				$a = (int)floor($i / 3);
471
				$b = $i % 3 + $this->moduleCount - 8 - 3;
472
				$v = !$test && (($bits >> $i) & 1) === 1;
0 ignored issues
show
Bug Best Practice introduced by
The expression $test of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
473
474
				$this->set($b, $a, $v, $this::M_VERSION); // ne
475
				$this->set($a, $b, $v, $this::M_VERSION); // sw
476
			}
477
478
		}
479
480
		return $this;
481
	}
482
483
	/**
484
	 * Draws the format info along the finder patterns
485
	 *
486
	 * @param int        $maskPattern
487
	 * @param bool|null  $test
488
	 *
489
	 * @return \chillerlan\QRCode\Data\QRMatrix
490
	 */
491
	public function setFormatInfo(int $maskPattern, bool $test = null):QRMatrix{
492
		$bits = $this::formatPattern[QRCode::ECC_MODES[$this->eclevel]][$maskPattern] ?? 0;
493
494
		for($i = 0; $i < 15; $i++){
495
			$v = !$test && (($bits >> $i) & 1) === 1;
0 ignored issues
show
Bug Best Practice introduced by
The expression $test of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
496
497
			if($i < 6){
498
				$this->set(8, $i, $v, $this::M_FORMAT);
499
			}
500
			elseif($i < 8){
501
				$this->set(8, $i + 1, $v, $this::M_FORMAT);
502
			}
503
			else{
504
				$this->set(8, $this->moduleCount - 15 + $i, $v, $this::M_FORMAT);
505
			}
506
507
			if($i < 8){
508
				$this->set($this->moduleCount - $i - 1, 8, $v, $this::M_FORMAT);
509
			}
510
			elseif($i < 9){
511
				$this->set(15 - $i, 8, $v, $this::M_FORMAT);
512
			}
513
			else{
514
				$this->set(15 - $i - 1, 8, $v, $this::M_FORMAT);
515
			}
516
517
		}
518
519
		$this->set(8, $this->moduleCount - 8, !$test, $this::M_FORMAT);
520
521
		return $this;
522
	}
523
524
	/**
525
	 * Draws the "quiet zone" of $size around the matrix
526
	 *
527
	 * @param int|null $size
528
	 *
529
	 * @return \chillerlan\QRCode\Data\QRMatrix
530
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
531
	 */
532
	public function setQuietZone(int $size = null):QRMatrix{
533
534
		if($this->matrix[$this->moduleCount - 1][$this->moduleCount - 1] === $this::M_NULL){
535
			throw new QRCodeDataException('use only after writing data');
536
		}
537
538
		$size = $size !== null
539
			? max(0, min($size, floor($this->moduleCount / 2)))
540
			: 4;
541
542
		for($y = 0; $y < $this->moduleCount; $y++){
543
			for($i = 0; $i < $size; $i++){
544
				array_unshift($this->matrix[$y], $this::M_QUIETZONE);
545
				array_push($this->matrix[$y], $this::M_QUIETZONE);
546
			}
547
		}
548
549
		$this->moduleCount += ($size * 2);
550
551
		$r = array_fill(0, $this->moduleCount, $this::M_QUIETZONE);
552
553
		for($i = 0; $i < $size; $i++){
554
			array_unshift($this->matrix, $r);
555
			array_push($this->matrix, $r);
556
		}
557
558
		return $this;
559
	}
560
561
	/**
562
	 * Clears a space of $width * $height in order to add a logo or text.
563
	 *
564
	 * Additionally, the logo space can be positioned within the QR Code - respecting the main functional patterns -
565
	 * using $startX and $startY. If either of these are null, the logo space will be centered in that direction.
566
	 * ECC level "H" (30%) is required.
567
	 *
568
	 * Please note that adding a logo space minimizes the error correction capacity of the QR Code and
569
	 * created images may become unreadable, especially when printed with a chance to receive damage.
570
	 * Please test thoroughly before using this feature in production.
571
	 *
572
	 * This method should be called from within an output module (after the matrix has been filled with data).
573
	 * Note that there is no restiction on how many times this method could be called on the same matrix instance.
574
	 *
575
	 * @link https://github.com/chillerlan/php-qrcode/issues/52
576
	 *
577
	 * @param int      $width
578
	 * @param int      $height
579
	 * @param int|null $startX
580
	 * @param int|null $startY
581
	 *
582
	 * @return \chillerlan\QRCode\Data\QRMatrix
583
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
584
	 */
585
	public function setLogoSpace(int $width, int $height, int $startX = null, int $startY = null):QRMatrix{
586
587
		// for logos we operate in ECC H (30%) only
588
		if($this->eclevel !== 0b10){
589
			throw new QRCodeDataException('ECC level "H" required to add logo space');
590
		}
591
592
		// we need uneven sizes to center the logo space, adjust if needed
593
		if($startX === null && ($width % 2) === 0){
594
			$width++;
595
		}
596
597
		if($startY === null && ($height % 2) === 0){
598
			$height++;
599
		}
600
601
		// $this->moduleCount includes the quiet zone (if created), we need the QR size here
602
		$length = $this->version * 4 + 17;
603
604
		// throw if the logo space exceeds the maximum error correction capacity
605
		if($width * $height > floor($length * $length * 0.2)){
606
			throw new QRCodeDataException('logo space exceeds the maximum error correction capacity');
607
		}
608
609
		// quiet zone size
610
		$qz    = ($this->moduleCount - $length) / 2;
611
		// skip quiet zone and the first 9 rows/columns (finder-, mode-, version- and timing patterns)
612
		$start = $qz + 9;
613
		// skip quiet zone
614
		$end   = $this->moduleCount - $qz;
615
616
		// determine start coordinates
617
		$startX = ($startX !== null ? $startX : ($length - $width) / 2) + $qz;
618
		$startY = ($startY !== null ? $startY : ($length - $height) / 2) + $qz;
619
620
		// clear the space
621
		foreach($this->matrix as $y => $row){
622
			foreach($row as $x => $val){
623
				// out of bounds, skip
624
				if($x < $start || $y < $start ||$x >= $end || $y >= $end){
625
					continue;
626
				}
627
				// a match
628
				if($x >= $startX && $x < ($startX + $width) && $y >= $startY && $y < ($startY + $height)){
629
					$this->set($x, $y, false, $this::M_LOGO);
630
				}
631
			}
632
		}
633
634
		return $this;
635
	}
636
637
	/**
638
	 * Maps the binary $data array from QRDataInterface::maskECC() on the matrix, using $maskPattern
639
	 *
640
	 * @see \chillerlan\QRCode\Data\QRDataAbstract::maskECC()
641
	 *
642
	 * @param int[] $data
643
	 * @param int   $maskPattern
644
	 *
645
	 * @return \chillerlan\QRCode\Data\QRMatrix
646
	 */
647
	public function mapData(array $data, int $maskPattern):QRMatrix{
648
		$this->maskPattern = $maskPattern;
649
		$byteCount         = count($data);
650
		$size              = $this->moduleCount - 1;
651
		$mask              = $this->getMask($this->maskPattern);
652
653
		for($i = $size, $y = $size, $inc = -1, $byteIndex = 0, $bitIndex  = 7; $i > 0; $i -= 2){
654
655
			if($i === 6){
656
				$i--;
657
			}
658
659
			while(true){
660
				for($c = 0; $c < 2; $c++){
661
					$x = $i - $c;
662
663
					if($this->matrix[$y][$x] === $this::M_NULL){
664
						$v = false;
665
666
						if($byteIndex < $byteCount){
667
							$v = (($data[$byteIndex] >> $bitIndex) & 1) === 1;
668
						}
669
670
						if($mask($x, $y) === 0){
671
							$v = !$v;
672
						}
673
674
						$this->matrix[$y][$x] = $this::M_DATA << ($v ? 8 : 0);
675
						$bitIndex--;
676
677
						if($bitIndex === -1){
678
							$byteIndex++;
679
							$bitIndex = 7;
680
						}
681
682
					}
683
				}
684
685
				$y += $inc;
686
687
				if($y < 0 || $this->moduleCount <= $y){
688
					$y   -=  $inc;
689
					$inc  = -$inc;
690
691
					break;
692
				}
693
694
			}
695
		}
696
697
		return $this;
698
	}
699
700
	/**
701
	 * ISO/IEC 18004:2000 Section 8.8.1
702
	 *
703
	 * Note that some versions of the QR code standard have had errors in the section about mask patterns.
704
	 * The information below has been corrected. (https://www.thonky.com/qr-code-tutorial/mask-patterns)
705
	 *
706
	 * @see \chillerlan\QRCode\QRMatrix::mapData()
707
	 *
708
	 * @internal
709
	 *
710
	 * @param int $maskPattern
711
	 *
712
	 * @return \Closure
713
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
714
	 */
715
	protected function getMask(int $maskPattern):Closure{
716
717
		if((0b111 & $maskPattern) !== $maskPattern){
718
			throw new QRCodeDataException('invalid mask pattern'); // @codeCoverageIgnore
719
		}
720
721
		return [
722
			0b000 => function($x, $y):int{ return ($x + $y) % 2; },
723
			0b001 => function($x, $y):int{ return $y % 2; },
724
			0b010 => function($x, $y):int{ return $x % 3; },
0 ignored issues
show
Unused Code introduced by
The parameter $y is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
725
			0b011 => function($x, $y):int{ return ($x + $y) % 3; },
726
			0b100 => function($x, $y):int{ return ((int)($y / 2) + (int)($x / 3)) % 2; },
727
			0b101 => function($x, $y):int{ return (($x * $y) % 2) + (($x * $y) % 3); },
728
			0b110 => function($x, $y):int{ return ((($x * $y) % 2) + (($x * $y) % 3)) % 2; },
729
			0b111 => function($x, $y):int{ return ((($x * $y) % 3) + (($x + $y) % 2)) % 2; },
730
		][$maskPattern];
731
	}
732
733
}
734