Passed
Push — v5 ( 902486...182ebf )
by smiley
02:36
created

MaskPattern::getMask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 11
rs 9.9332
c 1
b 0
f 0
1
<?php
2
/**
3
 * Class MaskPattern
4
 *
5
 * @filesource   MaskPattern.php
6
 * @created      19.01.2021
7
 * @package      chillerlan\QRCode\Common
8
 * @author       smiley <[email protected]>
9
 * @copyright    2021 smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Common;
14
15
use chillerlan\QRCode\QRCodeException;
16
use Closure;
17
18
/**
19
 *
20
 */
21
class MaskPattern{
22
23
	public const PATTERN_000 = 0b000;
24
	public const PATTERN_001 = 0b001;
25
	public const PATTERN_010 = 0b010;
26
	public const PATTERN_011 = 0b011;
27
	public const PATTERN_100 = 0b100;
28
	public const PATTERN_101 = 0b101;
29
	public const PATTERN_110 = 0b110;
30
	public const PATTERN_111 = 0b111;
31
32
	public const PATTERNS = [
33
		self::PATTERN_000,
34
		self::PATTERN_001,
35
		self::PATTERN_010,
36
		self::PATTERN_011,
37
		self::PATTERN_100,
38
		self::PATTERN_101,
39
		self::PATTERN_110,
40
		self::PATTERN_111,
41
	];
42
43
	private int $maskPattern;
44
45
	/**
46
	 * MaskPattern constructor.
47
	 *
48
	 * ISO/IEC 18004:2000 Section 8.8.1
49
	 *
50
	 * @throws \chillerlan\QRCode\QRCodeException
51
	 */
52
	public function __construct(int $maskPattern){
53
54
		if((0b111 & $maskPattern) !== $maskPattern){
55
			throw new QRCodeException('invalid mask pattern'); // @codeCoverageIgnore
56
		}
57
58
		$this->maskPattern = $maskPattern;
59
	}
60
61
	public function getPattern():int{
62
		return $this->maskPattern;
63
	}
64
65
	/**
66
	 * ISO/IEC 18004:2000 Section 8.8.1
67
	 *
68
	 * Note that some versions of the QR code standard have had errors in the section about mask patterns.
69
	 * The information below has been corrected. (https://www.thonky.com/qr-code-tutorial/mask-patterns)
70
	 */
71
	public function getMask():Closure{
72
		return [
73
			self::PATTERN_000 => fn($x, $y):int => ($x + $y) % 2,
74
			self::PATTERN_001 => fn($x, $y):int => $y % 2,
75
			self::PATTERN_010 => fn($x, $y):int => $x % 3,
0 ignored issues
show
Unused Code introduced by
The parameter $y is not used and could be removed. ( Ignorable by Annotation )

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

75
			self::PATTERN_010 => fn($x, /** @scrutinizer ignore-unused */ $y):int => $x % 3,

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

Loading history...
76
			self::PATTERN_011 => fn($x, $y):int => ($x + $y) % 3,
77
			self::PATTERN_100 => fn($x, $y):int => ((int)($y / 2) + (int)($x / 3)) % 2,
78
			self::PATTERN_101 => fn($x, $y):int => (($x * $y) % 2) + (($x * $y) % 3),
79
			self::PATTERN_110 => fn($x, $y):int => ((($x * $y) % 2) + (($x * $y) % 3)) % 2,
80
			self::PATTERN_111 => fn($x, $y):int => ((($x * $y) % 3) + (($x + $y) % 2)) % 2,
81
		][$this->maskPattern];
82
	}
83
84
}
85