Passed
Push — main ( 284c42...6b9f6f )
by smiley
12:10
created

QRMatrix::setFormatInfo()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 18
nc 20
nop 1
dl 0
loc 33
rs 8.8333
c 1
b 0
f 0
1
<?php
2
/**
3
 * Class QRMatrix
4
 *
5
 * @created      15.11.2017
6
 * @author       Smiley <[email protected]>
7
 * @copyright    2017 Smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\QRCode\Data;
12
13
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, ReedSolomonEncoder, Version};
14
use function array_fill, count, floor, range;
15
16
/**
17
 * Holds an array representation of the final QR Code that contains numerical values for later output modifications;
18
 * maps the ECC coded binary data and applies the mask pattern
19
 *
20
 * @see http://www.thonky.com/qr-code-tutorial/format-version-information
21
 */
22
class QRMatrix{
23
24
	/** @var int */
25
	public const IS_DARK          = 0b100000000000;
26
	/** @var int */
27
	public const M_NULL           = 0b000000000000;
28
	/** @var int */
29
	public const M_DARKMODULE     = (0b000000000001 | self::IS_DARK);
30
	/** @var int */
31
	public const M_DATA           = 0b000000000010;
32
	/** @var int */
33
	public const M_DATA_DARK      = (self::M_DATA | self::IS_DARK);
34
	/** @var int */
35
	public const M_FINDER         = 0b000000000100;
36
	/** @var int */
37
	public const M_FINDER_DARK    = (self::M_FINDER | self::IS_DARK);
38
	/** @var int */
39
	public const M_SEPARATOR      = 0b000000001000;
40
	/** @var int */
41
	public const M_ALIGNMENT      = 0b000000010000;
42
	/** @var int */
43
	public const M_ALIGNMENT_DARK = (self::M_ALIGNMENT | self::IS_DARK);
44
	/** @var int */
45
	public const M_TIMING         = 0b000000100000;
46
	/** @var int */
47
	public const M_TIMING_DARK    = (self::M_TIMING | self::IS_DARK);
48
	/** @var int */
49
	public const M_FORMAT         = 0b000001000000;
50
	/** @var int */
51
	public const M_FORMAT_DARK    = (self::M_FORMAT | self::IS_DARK);
52
	/** @var int */
53
	public const M_VERSION        = 0b000010000000;
54
	/** @var int */
55
	public const M_VERSION_DARK   = (self::M_VERSION | self::IS_DARK);
56
	/** @var int */
57
	public const M_QUIETZONE      = 0b000100000000;
58
	/** @var int */
59
	public const M_LOGO           = 0b001000000000;
60
	/** @var int */
61
	public const M_FINDER_DOT     = (0b010000000000 | self::IS_DARK);
62
	/** @var int */
63
	public const M_TEST           = 0b011111111111;
64
	/** @var int */
65
	public const M_TEST_DARK      = (self::M_TEST | self::IS_DARK);
66
67
	/**
68
	 * Map of flag => coord
69
	 *
70
	 * @see \chillerlan\QRCode\Data\QRMatrix::checkNeighbours()
71
	 *
72
	 * @var array
73
	 */
74
	protected const neighbours = [
75
		0b00000001 => [-1, -1],
76
		0b00000010 => [ 0, -1],
77
		0b00000100 => [ 1, -1],
78
		0b00001000 => [ 1,  0],
79
		0b00010000 => [ 1,  1],
80
		0b00100000 => [ 0,  1],
81
		0b01000000 => [-1,  1],
82
		0b10000000 => [-1,  0],
83
	];
84
85
	/**
86
	 * the matrix version - always set in QRMatrix, may be null in BitMatrix
87
	 */
88
	protected ?Version $version = null;
89
90
	/**
91
	 * the current ECC level - always set in QRMatrix, may be null in BitMatrix
92
	 */
93
	protected ?EccLevel $eccLevel = null;
94
95
	/**
96
	 * the mask pattern that was used in the most recent operation, set via:
97
	 *
98
	 * - QRMatrix::setFormatInfo()
99
	 * - QRMatrix::mask()
100
	 * - BitMatrix::readFormatInformation()
101
	 */
102
	protected ?MaskPattern $maskPattern = null;
103
104
	/**
105
	 * the size (side length) of the matrix, including quiet zone (if created)
106
	 */
107
	protected int $moduleCount;
108
109
	/**
110
	 * the actual matrix data array
111
	 *
112
	 * @var int[][]
113
	 */
114
	protected array $matrix;
115
116
	/**
117
	 * QRMatrix constructor.
118
	 */
119
	public function __construct(Version $version, EccLevel $eccLevel){
120
		$this->version     = $version;
121
		$this->eccLevel    = $eccLevel;
122
		$this->moduleCount = $this->version->getDimension();
123
		$this->matrix      = $this->createMatrix($this->moduleCount, $this::M_NULL);
124
	}
125
126
	/**
127
	 * Creates a 2-dimensional array (square) of the given $size
128
	 */
129
	protected function createMatrix(int $size, int $value):array{
130
		return array_fill(0, $size, array_fill(0, $size, $value));
131
	}
132
133
	/**
134
	 * shortcut to initialize the functional patterns
135
	 */
136
	public function initFunctionalPatterns():self{
137
		return $this
138
			->setFinderPattern()
139
			->setSeparators()
140
			->setAlignmentPattern()
141
			->setTimingPattern()
142
			->setDarkModule()
143
			->setVersionNumber()
144
			->setFormatInfo()
145
		;
146
	}
147
148
	/**
149
	 * Returns the data matrix, returns a pure boolean representation if $boolean is set to true
150
	 *
151
	 * @return int[][]|bool[][]
152
	 */
153
	public function getMatrix(bool $boolean = null):array{
154
155
		if(!$boolean){
0 ignored issues
show
Bug Best Practice introduced by
The expression $boolean of type boolean|null 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...
156
			return $this->matrix;
157
		}
158
159
		$matrix = [];
160
161
		foreach($this->matrix as $y => $row){
162
			$matrix[$y] = [];
163
164
			foreach($row as $x => $val){
165
				$matrix[$y][$x] = ($val & $this::IS_DARK) === $this::IS_DARK;
166
			}
167
		}
168
169
		return $matrix;
170
	}
171
172
	/**
173
	 * @deprecated 5.0.0 use QRMatrix::getMatrix() instead
174
	 * @see \chillerlan\QRCode\Data\QRMatrix::getMatrix()
175
	 * @codeCoverageIgnore
176
	 */
177
	public function matrix(bool $boolean = null):array{
178
		return $this->getMatrix($boolean);
179
	}
180
181
	/**
182
	 * Returns the current version number
183
	 */
184
	public function getVersion():?Version{
185
		return $this->version;
186
	}
187
188
	/**
189
	 * @deprecated 5.0.0 use QRMatrix::getVersion() instead
190
	 * @see \chillerlan\QRCode\Data\QRMatrix::getVersion()
191
	 * @codeCoverageIgnore
192
	 */
193
	public function version():?Version{
194
		return $this->getVersion();
195
	}
196
197
	/**
198
	 * Returns the current ECC level
199
	 */
200
	public function getEccLevel():?EccLevel{
201
		return $this->eccLevel;
202
	}
203
204
	/**
205
	 * @deprecated 5.0.0 use QRMatrix::getEccLevel() instead
206
	 * @see \chillerlan\QRCode\Data\QRMatrix::getEccLevel()
207
	 * @codeCoverageIgnore
208
	 */
209
	public function eccLevel():?EccLevel{
210
		return $this->getEccLevel();
211
	}
212
213
	/**
214
	 * Returns the current mask pattern
215
	 */
216
	public function getMaskPattern():?MaskPattern{
217
		return $this->maskPattern;
218
	}
219
220
	/**
221
	 * @deprecated 5.0.0 use QRMatrix::getMaskPattern() instead
222
	 * @see \chillerlan\QRCode\Data\QRMatrix::getMaskPattern()
223
	 * @codeCoverageIgnore
224
	 */
225
	public function maskPattern():?MaskPattern{
226
		return $this->getMaskPattern();
227
	}
228
229
	/**
230
	 * Returns the absoulute size of the matrix, including quiet zone (after setting it).
231
	 *
232
	 * size = version * 4 + 17 [ + 2 * quietzone size]
233
	 */
234
	public function getSize():int{
235
		return $this->moduleCount;
236
	}
237
238
	/**
239
	 * @deprecated 5.0.0 use QRMatrix::getSize() instead
240
	 * @see \chillerlan\QRCode\Data\QRMatrix::getSize()
241
	 * @codeCoverageIgnore
242
	 */
243
	public function size():int{
244
		return $this->getSize();
245
	}
246
247
	/**
248
	 * Returns the value of the module at position [$x, $y] or -1 if the coordinate is outside the matrix
249
	 */
250
	public function get(int $x, int $y):int{
251
252
		if(!isset($this->matrix[$y][$x])){
253
			return -1;
254
		}
255
256
		return $this->matrix[$y][$x];
257
	}
258
259
	/**
260
	 * Sets the $M_TYPE value for the module at position [$x, $y]
261
	 *
262
	 *   true  => $M_TYPE | 0x800
263
	 *   false => $M_TYPE
264
	 */
265
	public function set(int $x, int $y, bool $value, int $M_TYPE):self{
266
267
		if(isset($this->matrix[$y][$x])){
268
			$this->matrix[$y][$x] = (($M_TYPE & ~$this::IS_DARK) | (($value) ? $this::IS_DARK : 0));
269
		}
270
271
		return $this;
272
	}
273
274
	/**
275
	 * Flips the value of the module at ($x, $y)
276
	 */
277
	public function flip(int $x, int $y):self{
278
279
		if(isset($this->matrix[$y][$x])){
280
			$this->matrix[$y][$x] ^= $this::IS_DARK;
281
		}
282
283
		return $this;
284
	}
285
286
	/**
287
	 * Checks whether the module at ($x, $y) is of the given $M_TYPE
288
	 *
289
	 *   true => $value & $M_TYPE === $M_TYPE
290
	 */
291
	public function checkType(int $x, int $y, int $M_TYPE):bool{
292
293
		if(!isset($this->matrix[$y][$x])){
294
			return false;
295
		}
296
297
		return ($this->matrix[$y][$x] & $M_TYPE) === $M_TYPE;
298
	}
299
300
	/**
301
	 * checks whether the module at ($x, $y) is in the given array of $M_TYPES,
302
	 * returns true if a match is found, otherwise false.
303
	 */
304
	public function checkTypeIn(int $x, int $y, array $M_TYPES):bool{
305
306
		foreach($M_TYPES as $type){
307
			if($this->checkType($x, $y, $type)){
308
				return true;
309
			}
310
		}
311
312
		return false;
313
	}
314
315
	/**
316
	 * Checks whether the module at ($x, $y) is true (dark) or false (light)
317
	 */
318
	public function check(int $x, int $y):bool{
319
		return $this->checkType($x, $y, $this::IS_DARK);
320
	}
321
322
	/**
323
	 * Checks the status of the neighbouring modules for the module at ($x, $y) and returns a bitmask with the results.
324
	 *
325
	 * The 8 flags of the bitmask represent the status of each of the neighbouring fields,
326
	 * starting with the lowest bit for top left, going clockwise:
327
	 *
328
	 *   1 2 3
329
	 *   8 # 4
330
	 *   7 6 5
331
	 */
332
	public function checkNeighbours(int $x, int $y, int $M_TYPE_VALUE = null):int{
333
		$bits = 0;
334
335
		foreach($this::neighbours as $bit => $coord){
336
			[$ix, $iy] = $coord;
337
338
			// check if the field is the same type
339
			if(
340
				$M_TYPE_VALUE !== null
341
				&& ($this->get(($x + $ix), ($y + $iy)) | $this::IS_DARK) !== ($M_TYPE_VALUE | $this::IS_DARK)
342
			){
343
				continue;
344
			}
345
346
			if($this->checkType(($x + $ix), ($y + $iy), $this::IS_DARK)){
347
				$bits |= $bit;
348
			}
349
		}
350
351
		return $bits;
352
	}
353
354
	/**
355
	 * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder
356
	 *
357
	 * 4 * version + 9 or moduleCount - 8
358
	 */
359
	public function setDarkModule():self{
360
		$this->set(8, ($this->moduleCount - 8), true, $this::M_DARKMODULE);
361
362
		return $this;
363
	}
364
365
	/**
366
	 * Draws the 7x7 finder patterns in the corners top left/right and bottom left
367
	 *
368
	 * ISO/IEC 18004:2000 Section 7.3.2
369
	 */
370
	public function setFinderPattern():self{
371
372
		$pos = [
373
			[0, 0], // top left
374
			[($this->moduleCount - 7), 0], // top right
375
			[0, ($this->moduleCount - 7)], // bottom left
376
		];
377
378
		foreach($pos as $c){
379
			for($y = 0; $y < 7; $y++){
380
				for($x = 0; $x < 7; $x++){
381
					// outer (dark) 7*7 square
382
					if($x === 0 || $x === 6 || $y === 0 || $y === 6){
383
						$this->set(($c[0] + $y), ($c[1] + $x), true, $this::M_FINDER);
384
					}
385
					// inner (light) 5*5 square
386
					elseif($x === 1 || $x === 5 || $y === 1 || $y === 5){
387
						$this->set(($c[0] + $y), ($c[1] + $x), false, $this::M_FINDER);
388
					}
389
					// 3*3 dot
390
					else{
391
						$this->set(($c[0] + $y), ($c[1] + $x), true, $this::M_FINDER_DOT);
392
					}
393
				}
394
			}
395
		}
396
397
		return $this;
398
	}
399
400
	/**
401
	 * Draws the separator lines around the finder patterns
402
	 *
403
	 * ISO/IEC 18004:2000 Section 7.3.3
404
	 */
405
	public function setSeparators():self{
406
407
		$h = [
408
			[7, 0],
409
			[($this->moduleCount - 8), 0],
410
			[7, ($this->moduleCount - 8)],
411
		];
412
413
		$v = [
414
			[7, 7],
415
			[($this->moduleCount - 1), 7],
416
			[7, ($this->moduleCount - 8)],
417
		];
418
419
		for($c = 0; $c < 3; $c++){
420
			for($i = 0; $i < 8; $i++){
421
				$this->set($h[$c][0]     , ($h[$c][1] + $i), false, $this::M_SEPARATOR);
422
				$this->set(($v[$c][0] - $i), $v[$c][1]     , false, $this::M_SEPARATOR);
423
			}
424
		}
425
426
		return $this;
427
	}
428
429
430
	/**
431
	 * Draws the 5x5 alignment patterns
432
	 *
433
	 * ISO/IEC 18004:2000 Section 7.3.5
434
	 */
435
	public function setAlignmentPattern():self{
436
		$alignmentPattern = $this->version->getAlignmentPattern();
0 ignored issues
show
Bug introduced by
The method getAlignmentPattern() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

436
		/** @scrutinizer ignore-call */ 
437
  $alignmentPattern = $this->version->getAlignmentPattern();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
437
438
		foreach($alignmentPattern as $y){
439
			foreach($alignmentPattern as $x){
440
441
				// skip existing patterns
442
				if($this->matrix[$y][$x] !== $this::M_NULL){
443
					continue;
444
				}
445
446
				for($ry = -2; $ry <= 2; $ry++){
447
					for($rx = -2; $rx <= 2; $rx++){
448
						$v = ($ry === 0 && $rx === 0) || $ry === 2 || $ry === -2 || $rx === 2 || $rx === -2;
449
450
						$this->set(($x + $rx), ($y + $ry), $v, $this::M_ALIGNMENT);
451
					}
452
				}
453
454
			}
455
		}
456
457
		return $this;
458
	}
459
460
461
	/**
462
	 * Draws the timing pattern (h/v checkered line between the finder patterns)
463
	 *
464
	 * ISO/IEC 18004:2000 Section 7.3.4
465
	 */
466
	public function setTimingPattern():self{
467
468
		foreach(range(8, ($this->moduleCount - 8 - 1)) as $i){
469
470
			if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){
471
				continue;
472
			}
473
474
			$v = ($i % 2) === 0;
475
476
			$this->set($i, 6, $v, $this::M_TIMING); // h
477
			$this->set(6, $i, $v, $this::M_TIMING); // v
478
		}
479
480
		return $this;
481
	}
482
483
	/**
484
	 * Draws the version information, 2x 3x6 pixel
485
	 *
486
	 * ISO/IEC 18004:2000 Section 8.10
487
	 */
488
	public function setVersionNumber():self{
489
		$bits = $this->version->getVersionPattern();
490
491
		if($bits !== null){
492
493
			for($i = 0; $i < 18; $i++){
494
				$a = (int)($i / 3);
495
				$b = (($i % 3) + ($this->moduleCount - 8 - 3));
496
				$v = (($bits >> $i) & 1) === 1;
497
498
				$this->set($b, $a, $v, $this::M_VERSION); // ne
499
				$this->set($a, $b, $v, $this::M_VERSION); // sw
500
			}
501
502
		}
503
504
		return $this;
505
	}
506
507
	/**
508
	 * Draws the format info along the finder patterns. If no $maskPattern, all format info modules will be set to false.
509
	 *
510
	 * ISO/IEC 18004:2000 Section 8.9
511
	 */
512
	public function setFormatInfo(MaskPattern $maskPattern = null):self{
513
		$this->maskPattern = $maskPattern;
514
515
		$bits = ($this->maskPattern instanceof MaskPattern)
516
			? $this->eccLevel->getformatPattern($this->maskPattern)
0 ignored issues
show
Bug introduced by
The method getformatPattern() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

516
			? $this->eccLevel->/** @scrutinizer ignore-call */ getformatPattern($this->maskPattern)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
517
			: 0; // sets all format fields to false (test mode)
518
519
		for($i = 0; $i < 15; $i++){
520
			$v = (($bits >> $i) & 1) === 1;
521
522
			if($i < 6){
523
				$this->set(8, $i, $v, $this::M_FORMAT);
524
			}
525
			elseif($i < 8){
526
				$this->set(8, ($i + 1), $v, $this::M_FORMAT);
527
			}
528
			else{
529
				$this->set(8, ($this->moduleCount - 15 + $i), $v, $this::M_FORMAT);
530
			}
531
532
			if($i < 8){
533
				$this->set(($this->moduleCount - $i - 1), 8, $v, $this::M_FORMAT);
534
			}
535
			elseif($i < 9){
536
				$this->set(((15 - $i)), 8, $v, $this::M_FORMAT);
537
			}
538
			else{
539
				$this->set((15 - $i - 1), 8, $v, $this::M_FORMAT);
540
			}
541
542
		}
543
544
		return $this;
545
	}
546
547
	/**
548
	 * Draws the "quiet zone" of $size around the matrix
549
	 *
550
	 * ISO/IEC 18004:2000 Section 7.3.7
551
	 *
552
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
553
	 */
554
	public function setQuietZone(int $quietZoneSize):self{
555
556
		if($this->matrix[($this->moduleCount - 1)][($this->moduleCount - 1)] === $this::M_NULL){
557
			throw new QRCodeDataException('use only after writing data');
558
		}
559
560
		// create a matrix with the new size
561
		$newSize   = ($this->moduleCount + ($quietZoneSize * 2));
562
		$newMatrix = $this->createMatrix($newSize, $this::M_QUIETZONE);
563
564
		// copy over the current matrix
565
		for($y = 0; $y < $this->moduleCount; $y++){
566
			for($x = 0; $x < $this->moduleCount; $x++){
567
				$newMatrix[($y + $quietZoneSize)][($x + $quietZoneSize)] = $this->matrix[$y][$x];
568
			}
569
		}
570
571
		// set the new values
572
		$this->moduleCount = $newSize;
573
		$this->matrix      = $newMatrix;
574
575
		return $this;
576
	}
577
578
	/**
579
	 * Clears a space of $width * $height in order to add a logo or text.
580
	 * If no $height is given, the space will be assumed a square of $width.
581
	 *
582
	 * Additionally, the logo space can be positioned within the QR Code - respecting the main functional patterns -
583
	 * using $startX and $startY. If either of these are null, the logo space will be centered in that direction.
584
	 * ECC level "H" (30%) is required.
585
	 *
586
	 * Please note that adding a logo space minimizes the error correction capacity of the QR Code and
587
	 * created images may become unreadable, especially when printed with a chance to receive damage.
588
	 * Please test thoroughly before using this feature in production.
589
	 *
590
	 * This method should be called from within an output module (after the matrix has been filled with data).
591
	 * Note that there is no restiction on how many times this method could be called on the same matrix instance.
592
	 *
593
	 * @link https://github.com/chillerlan/php-qrcode/issues/52
594
	 *
595
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
596
	 */
597
	public function setLogoSpace(int $width, int $height = null, int $startX = null, int $startY = null):self{
598
599
		// for logos, we operate in ECC H (30%) only
600
		if($this->eccLevel->getLevel() !== EccLevel::H){
601
			throw new QRCodeDataException('ECC level "H" required to add logo space');
602
		}
603
604
		if($height === null){
605
			$height = $width;
606
		}
607
608
		// if width and height happen to be negative or 0 (default value), just return - nothing to do
609
		if($width <= 0 || $height <= 0){
610
			return $this; // @codeCoverageIgnore
611
		}
612
613
		// $this->moduleCount includes the quiet zone (if created), we need the QR size here
614
		$length = $this->version->getDimension();
615
616
		// throw if the size is exceeds the qrcode size
617
		if($width > $length || $height > $length){
618
			throw new QRCodeDataException('logo dimensions exceed matrix size');
619
		}
620
621
		// we need uneven sizes to center the logo space, adjust if needed
622
		if($startX === null && ($width % 2) === 0){
623
			$width++;
624
		}
625
626
		if($startY === null && ($height % 2) === 0){
627
			$height++;
628
		}
629
630
		// throw if the logo space exceeds the maximum error correction capacity
631
		if(($width * $height) > floor($length * $length * 0.2)){
632
			throw new QRCodeDataException('logo space exceeds the maximum error correction capacity');
633
		}
634
635
		// quiet zone size
636
		$qz    = (($this->moduleCount - $length) / 2);
637
		// skip quiet zone and the first 9 rows/columns (finder-, mode-, version- and timing patterns)
638
		$start = ($qz + 9);
639
		// skip quiet zone
640
		$end   = ($this->moduleCount - $qz);
641
642
		// determine start coordinates
643
		$startX = ((($startX !== null) ? $startX : ($length - $width) / 2) + $qz);
644
		$startY = ((($startY !== null) ? $startY : ($length - $height) / 2) + $qz);
645
646
		// clear the space
647
		for($y = 0; $y < $this->moduleCount; $y++){
648
			for($x = 0; $x < $this->moduleCount; $x++){
649
				// out of bounds, skip
650
				if($x < $start || $y < $start ||$x >= $end || $y >= $end){
651
					continue;
652
				}
653
				// a match
654
				if($x >= $startX && $x < ($startX + $width) && $y >= $startY && $y < ($startY + $height)){
655
					$this->set($x, $y, false, $this::M_LOGO);
656
				}
657
			}
658
		}
659
660
		return $this;
661
	}
662
663
	/**
664
	 * Maps the interleaved binary $data on the matrix
665
	 */
666
	public function writeCodewords(BitBuffer $bitBuffer):self{
667
		$data      = (new ReedSolomonEncoder($this->version, $this->eccLevel))->interleaveEcBytes($bitBuffer);
0 ignored issues
show
Bug introduced by
It seems like $this->eccLevel can also be of type null; however, parameter $eccLevel of chillerlan\QRCode\Common...nEncoder::__construct() does only seem to accept chillerlan\QRCode\Common\EccLevel, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

667
		$data      = (new ReedSolomonEncoder($this->version, /** @scrutinizer ignore-type */ $this->eccLevel))->interleaveEcBytes($bitBuffer);
Loading history...
Bug introduced by
It seems like $this->version can also be of type null; however, parameter $version of chillerlan\QRCode\Common...nEncoder::__construct() does only seem to accept chillerlan\QRCode\Common\Version, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

667
		$data      = (new ReedSolomonEncoder(/** @scrutinizer ignore-type */ $this->version, $this->eccLevel))->interleaveEcBytes($bitBuffer);
Loading history...
668
		$byteCount = count($data);
669
		$iByte     = 0;
670
		$iBit      = 7;
671
		$direction = true;
672
673
		for($i = ($this->moduleCount - 1); $i > 0; $i -= 2){
674
675
			// skip vertical alignment pattern
676
			if($i === 6){
677
				$i--;
678
			}
679
680
			for($count = 0; $count < $this->moduleCount; $count++){
681
				$y = ($direction) ? ($this->moduleCount - 1 - $count) : $count;
682
683
				for($col = 0; $col < 2; $col++){
684
					$x = ($i - $col);
685
686
					// skip functional patterns
687
					if($this->get($x, $y) !== $this::M_NULL){
688
						continue;
689
					}
690
691
					$v = $iByte < $byteCount && (($data[$iByte] >> $iBit--) & 1) === 1;
692
693
					$this->set($x, $y, $v, $this::M_DATA);
694
695
					if($iBit === -1){
696
						$iByte++;
697
						$iBit = 7;
698
					}
699
				}
700
			}
701
702
			$direction = !$direction; // switch directions
0 ignored issues
show
introduced by
The condition $direction is always true.
Loading history...
703
		}
704
705
		return $this;
706
	}
707
708
	/**
709
	 * Applies/reverses the mask pattern
710
	 *
711
	 * ISO/IEC 18004:2000 Section 8.8.1
712
	 */
713
	public function mask(MaskPattern $maskPattern):self{
714
		$this->maskPattern = $maskPattern;
715
		$mask              = $this->maskPattern->getMask();
716
717
		foreach($this->matrix as $y => $row){
718
			foreach($row as $x => $val){
719
				if($mask($x, $y) && ($val & $this::M_DATA) === $this::M_DATA){
720
					$this->flip($x, $y);
721
				}
722
			}
723
		}
724
725
		return $this;
726
	}
727
728
}
729