Passed
Push — main ( 05e99b...cf4e5a )
by smiley
12:39
created

QRMatrix::setLogoSpace()   F

Complexity

Conditions 20
Paths 141

Size

Total Lines 64
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 20
eloc 28
nc 141
nop 4
dl 0
loc 64
rs 3.825
c 4
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
 * @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     = 0b100000000001;
30
	/** @var int */
31
	public const M_DATA           = 0b000000000010;
32
	/** @var int */
33
	public const M_DATA_DARK      = 0b100000000010;
34
	/** @var int */
35
	public const M_FINDER         = 0b000000000100;
36
	/** @var int */
37
	public const M_FINDER_DARK    = 0b100000000100;
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 = 0b100000010000;
44
	/** @var int */
45
	public const M_TIMING         = 0b000000100000;
46
	/** @var int */
47
	public const M_TIMING_DARK    = 0b100000100000;
48
	/** @var int */
49
	public const M_FORMAT         = 0b000001000000;
50
	/** @var int */
51
	public const M_FORMAT_DARK    = 0b100001000000;
52
	/** @var int */
53
	public const M_VERSION        = 0b000010000000;
54
	/** @var int */
55
	public const M_VERSION_DARK   = 0b100010000000;
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     = 0b110000000000;
62
	/** @var int */
63
	public const M_TEST           = 0b011111111111;
64
	/** @var int */
65
	public const M_TEST_DARK      = 0b111111111111;
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
		$matrix = [];
155
156
		foreach($this->matrix as $y => $row){
157
			$matrix[$y] = [];
158
159
			foreach($row as $x => $val){
160
				$matrix[$y][$x] = $boolean === true
161
					? $this->checkType($x, $y, $this::IS_DARK)
162
					: $this->get($x, $y);
163
			}
164
		}
165
166
		return $matrix;
167
	}
168
169
	/**
170
	 * @deprecated 5.0.0 use QRMatrix::getMatrix() instead
171
	 * @see \chillerlan\QRCode\Data\QRMatrix::getMatrix()
172
	 * @codeCoverageIgnore
173
	 */
174
	public function matrix(bool $boolean = null):array{
175
		return $this->getMatrix($boolean);
176
	}
177
178
	/**
179
	 * Returns the current version number
180
	 */
181
	public function getVersion():?Version{
182
		return $this->version;
183
	}
184
185
	/**
186
	 * @deprecated 5.0.0 use QRMatrix::getVersion() instead
187
	 * @see \chillerlan\QRCode\Data\QRMatrix::getVersion()
188
	 * @codeCoverageIgnore
189
	 */
190
	public function version():?Version{
191
		return $this->getVersion();
192
	}
193
194
	/**
195
	 * Returns the current ECC level
196
	 */
197
	public function getEccLevel():?EccLevel{
198
		return $this->eccLevel;
199
	}
200
201
	/**
202
	 * @deprecated 5.0.0 use QRMatrix::getEccLevel() instead
203
	 * @see \chillerlan\QRCode\Data\QRMatrix::getEccLevel()
204
	 * @codeCoverageIgnore
205
	 */
206
	public function eccLevel():?EccLevel{
207
		return $this->getEccLevel();
208
	}
209
210
	/**
211
	 * Returns the current mask pattern
212
	 */
213
	public function getMaskPattern():?MaskPattern{
214
		return $this->maskPattern;
215
	}
216
217
	/**
218
	 * @deprecated 5.0.0 use QRMatrix::getMaskPattern() instead
219
	 * @see \chillerlan\QRCode\Data\QRMatrix::getMaskPattern()
220
	 * @codeCoverageIgnore
221
	 */
222
	public function maskPattern():?MaskPattern{
223
		return $this->getMaskPattern();
224
	}
225
226
	/**
227
	 * Returns the absoulute size of the matrix, including quiet zone (after setting it).
228
	 *
229
	 * size = version * 4 + 17 [ + 2 * quietzone size]
230
	 */
231
	public function getSize():int{
232
		return $this->moduleCount;
233
	}
234
235
	/**
236
	 * @deprecated 5.0.0 use QRMatrix::getSize() instead
237
	 * @see \chillerlan\QRCode\Data\QRMatrix::getSize()
238
	 * @codeCoverageIgnore
239
	 */
240
	public function size():int{
241
		return $this->getSize();
242
	}
243
244
	/**
245
	 * Returns the value of the module at position [$x, $y] or -1 if the coordinate is outside the matrix
246
	 */
247
	public function get(int $x, int $y):int{
248
249
		if(!isset($this->matrix[$y][$x])){
250
			return -1;
251
		}
252
253
		return $this->matrix[$y][$x];
254
	}
255
256
	/**
257
	 * Sets the $M_TYPE value for the module at position [$x, $y]
258
	 *
259
	 *   true  => $M_TYPE | 0x800
260
	 *   false => $M_TYPE
261
	 */
262
	public function set(int $x, int $y, bool $value, int $M_TYPE):self{
263
264
		if(isset($this->matrix[$y][$x])){
265
			$this->matrix[$y][$x] = (($M_TYPE & ~$this::IS_DARK) | (($value) ? $this::IS_DARK : 0));
266
		}
267
268
		return $this;
269
	}
270
271
	/**
272
	 * Fills an area of $width * $height, from the given starting point [$startX, $startY] (top left) with $value for $M_TYPE.
273
	 */
274
	public function setArea(int $startX, int $startY, int $width, int $height, bool $value, int $M_TYPE):self{
275
276
		for($y = $startY; $y < ($startY + $height); $y++){
277
			for($x = $startX; $x < ($startX + $width); $x++){
278
				$this->set($x, $y, $value, $M_TYPE);
279
			}
280
		}
281
282
		return $this;
283
	}
284
285
	/**
286
	 * Checks whether the module at ($x, $y) is of the given $M_TYPE
287
	 *
288
	 *   true => $value & $M_TYPE === $M_TYPE
289
	 */
290
	public function checkType(int $x, int $y, int $M_TYPE):bool{
291
		$val = $this->get($x, $y);
292
293
		if($val === -1){
294
			return false;
295
		}
296
297
		return ($val & $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 = null):int{
333
		$bits = 0;
334
335
		foreach($this::neighbours as $bit => $coord){
336
			[$ix, $iy] = $coord;
337
338
			$ix += $x;
339
			$iy += $y;
340
341
			// $M_TYPE is given, skip if the field is not the same type
342
			if($M_TYPE !== null && !$this->checkType($ix, $iy, $M_TYPE)){
343
				continue;
344
			}
345
346
			if($this->checkType($ix, $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
			$this
380
				->setArea($c[0], $c[1], 7, 7, true, $this::M_FINDER)
381
				->setArea(($c[0] + 1), ($c[1] + 1), 5, 5, false, $this::M_FINDER)
382
				->setArea(($c[0] + 2), ($c[1] + 2), 3, 3, true, $this::M_FINDER_DOT)
383
			;
384
		}
385
386
		return $this;
387
	}
388
389
	/**
390
	 * Draws the separator lines around the finder patterns
391
	 *
392
	 * ISO/IEC 18004:2000 Section 7.3.3
393
	 */
394
	public function setSeparators():self{
395
396
		$h = [
397
			[7, 0],
398
			[($this->moduleCount - 8), 0],
399
			[7, ($this->moduleCount - 8)],
400
		];
401
402
		$v = [
403
			[7, 7],
404
			[($this->moduleCount - 1), 7],
405
			[7, ($this->moduleCount - 8)],
406
		];
407
408
		for($c = 0; $c < 3; $c++){
409
			for($i = 0; $i < 8; $i++){
410
				$this->set($h[$c][0]     , ($h[$c][1] + $i), false, $this::M_SEPARATOR);
411
				$this->set(($v[$c][0] - $i), $v[$c][1]     , false, $this::M_SEPARATOR);
412
			}
413
		}
414
415
		return $this;
416
	}
417
418
419
	/**
420
	 * Draws the 5x5 alignment patterns
421
	 *
422
	 * ISO/IEC 18004:2000 Section 7.3.5
423
	 */
424
	public function setAlignmentPattern():self{
425
		$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

425
		/** @scrutinizer ignore-call */ 
426
  $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...
426
427
		foreach($alignmentPattern as $y){
428
			foreach($alignmentPattern as $x){
429
430
				// skip existing patterns
431
				if($this->matrix[$y][$x] !== $this::M_NULL){
432
					continue;
433
				}
434
435
				$this
436
					->setArea(($x - 2), ($y - 2), 5, 5, true, $this::M_ALIGNMENT)
437
					->setArea(($x - 1), ($y - 1), 3, 3, false, $this::M_ALIGNMENT)
438
					->set($x, $y, true, $this::M_ALIGNMENT)
439
				;
440
441
			}
442
		}
443
444
		return $this;
445
	}
446
447
448
	/**
449
	 * Draws the timing pattern (h/v checkered line between the finder patterns)
450
	 *
451
	 * ISO/IEC 18004:2000 Section 7.3.4
452
	 */
453
	public function setTimingPattern():self{
454
455
		foreach(range(8, ($this->moduleCount - 8 - 1)) as $i){
456
457
			if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){
458
				continue;
459
			}
460
461
			$v = ($i % 2) === 0;
462
463
			$this->set($i, 6, $v, $this::M_TIMING); // h
464
			$this->set(6, $i, $v, $this::M_TIMING); // v
465
		}
466
467
		return $this;
468
	}
469
470
	/**
471
	 * Draws the version information, 2x 3x6 pixel
472
	 *
473
	 * ISO/IEC 18004:2000 Section 8.10
474
	 */
475
	public function setVersionNumber():self{
476
		$bits = $this->version->getVersionPattern();
477
478
		if($bits !== null){
479
480
			for($i = 0; $i < 18; $i++){
481
				$a = (int)($i / 3);
482
				$b = (($i % 3) + ($this->moduleCount - 8 - 3));
483
				$v = (($bits >> $i) & 1) === 1;
484
485
				$this->set($b, $a, $v, $this::M_VERSION); // ne
486
				$this->set($a, $b, $v, $this::M_VERSION); // sw
487
			}
488
489
		}
490
491
		return $this;
492
	}
493
494
	/**
495
	 * Draws the format info along the finder patterns. If no $maskPattern, all format info modules will be set to false.
496
	 *
497
	 * ISO/IEC 18004:2000 Section 8.9
498
	 */
499
	public function setFormatInfo(MaskPattern $maskPattern = null):self{
500
		$this->maskPattern = $maskPattern;
501
502
		$bits = ($this->maskPattern instanceof MaskPattern)
503
			? $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

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

657
		$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

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