Passed
Push — main ( 39352f...6b2a52 )
by smiley
02:13
created

Decoder::decodeBitStream()   C

Complexity

Conditions 13
Paths 4

Size

Total Lines 63
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 13
eloc 40
c 6
b 0
f 0
nc 4
nop 1
dl 0
loc 63
rs 6.6166

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 Decoder
4
 *
5
 * @created      17.01.2021
6
 * @author       ZXing Authors
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2021 Smiley
9
 * @license      Apache-2.0
10
 */
11
12
namespace chillerlan\QRCode\Decoder;
13
14
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, ECICharset, MaskPattern, Mode, ReedSolomonDecoder, Version};
15
use chillerlan\QRCode\Data\{AlphaNum, Byte, ECI, Hanzi, Kanji, Number};
16
use chillerlan\QRCode\Detector\Detector;
17
use Throwable;
18
use function chr, mb_convert_encoding, mb_detect_encoding, mb_internal_encoding, str_replace;
19
20
/**
21
 * The main class which implements QR Code decoding -- as opposed to locating and extracting
22
 * the QR Code from an image.
23
 *
24
 * @author Sean Owen
25
 */
26
final class Decoder{
27
28
	private ?Version     $version = null;
29
	private ?EccLevel    $eccLevel = null;
30
	private ?MaskPattern $maskPattern = null;
31
	private BitBuffer    $bitBuffer;
32
	private ?ECICharset  $eciCharset = null;
33
34
	/**
35
	 * Decodes a QR Code represented as a BitMatrix.
36
	 * A 1 or "true" is taken to mean a black module.
37
	 *
38
	 * @throws \Throwable|\chillerlan\QRCode\Decoder\QRCodeDecoderException
39
	 */
40
	public function decode(LuminanceSourceInterface $source):DecoderResult{
41
		$matrix = (new Detector($source))->detect();
42
43
		try{
44
			// clone the BitMatrix to avoid errors in case we run into mirroring
45
			return $this->decodeMatrix(clone $matrix);
46
		}
47
		catch(Throwable $e){
48
49
			try{
50
				/*
51
				 * Prepare for a mirrored reading.
52
				 *
53
				 * Since we're here, this means we have successfully detected some kind
54
				 * of version and format information when mirrored. This is a good sign,
55
				 * that the QR code may be mirrored, and we should try once more with a
56
				 * mirrored content.
57
				 */
58
				return $this->decodeMatrix($matrix->setMirror(true)->mirror());
59
			}
60
			catch(Throwable $f){
61
				// Throw the exception from the original reading
62
				throw $e;
63
			}
64
65
		}
66
67
	}
68
69
	/**
70
	 * @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
71
	 */
72
	private function decodeMatrix(BitMatrix $matrix):DecoderResult{
73
		// Read raw codewords
74
		$rawCodewords      = $matrix->readCodewords();
75
		$this->version     = $matrix->version();
76
		$this->eccLevel    = $matrix->eccLevel();
77
		$this->maskPattern = $matrix->maskPattern();
78
79
		if($this->version === null || $this->eccLevel === null || $this->maskPattern === null){
80
			throw new QRCodeDecoderException('unable to read version or format info'); // @codeCoverageIgnore
81
		}
82
83
		$resultBytes = (new ReedSolomonDecoder($this->version, $this->eccLevel))->decode($rawCodewords);
84
85
		return $this->decodeBitStream($resultBytes);
86
	}
87
88
	/**
89
	 * Decode the contents of that stream of bytes
90
	 *
91
	 * @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
92
	 */
93
	private function decodeBitStream(BitBuffer $bitBuffer):DecoderResult{
94
		$this->bitBuffer  = $bitBuffer;
95
		$this->eciCharset = null;
96
		$versionNumber    = $this->version->getVersionNumber();
0 ignored issues
show
Bug introduced by
The method getVersionNumber() 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

96
		/** @scrutinizer ignore-call */ 
97
  $versionNumber    = $this->version->getVersionNumber();

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...
97
		$symbolSequence   = -1;
98
		$parityData       = -1;
99
		$fc1InEffect      = false;
100
		$result           = '';
101
102
		// While still another segment to read...
103
		while($this->bitBuffer->available() >= 4){
104
			$datamode = $this->bitBuffer->read(4); // mode is encoded by 4 bits
105
106
			// OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
107
			if($datamode === Mode::TERMINATOR){
108
				break;
109
			}
110
			elseif($datamode === Mode::ECI){
111
				$this->eciCharset = ECI::parseValue($this->bitBuffer);
112
			}
113
			elseif($datamode === Mode::FNC1_FIRST || $datamode === Mode::FNC1_SECOND){
114
				// We do little with FNC1 except alter the parsed result a bit according to the spec
115
				$fc1InEffect = true;
116
			}
117
			elseif($datamode === Mode::STRCTURED_APPEND){
118
119
				if($this->bitBuffer->available() < 16){
120
					throw new QRCodeDecoderException('structured append: not enough bits left');
121
				}
122
				// sequence number and parity is added later to the result metadata
123
				// Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
124
				$symbolSequence = $this->bitBuffer->read(8);
125
				$parityData     = $this->bitBuffer->read(8);
126
			}
127
			elseif($datamode === Mode::NUMBER){
128
				$result .= Number::decodeSegment($this->bitBuffer, $versionNumber);
129
			}
130
			elseif($datamode === Mode::ALPHANUM){
131
				$result .= $this->decodeAlphanumSegment($versionNumber, $fc1InEffect);
132
			}
133
			elseif($datamode === Mode::BYTE){
134
				$result .= $this->decodeByteSegment($versionNumber);
135
			}
136
			elseif($datamode === Mode::KANJI){
137
				$result .= Kanji::decodeSegment($this->bitBuffer, $versionNumber);
138
			}
139
			elseif($datamode === Mode::HANZI){
140
				$result .= Hanzi::decodeSegment($this->bitBuffer, $versionNumber);
141
			}
142
			else{
143
				throw new QRCodeDecoderException('invalid data mode');
144
			}
145
146
		}
147
148
		return new DecoderResult([
149
			'rawBytes'                 => $this->bitBuffer,
150
			'data'                     => $result,
151
			'version'                  => $this->version,
152
			'eccLevel'                 => $this->eccLevel,
153
			'maskPattern'              => $this->maskPattern,
154
			'structuredAppendParity'   => $parityData,
155
			'structuredAppendSequence' => $symbolSequence,
156
		]);
157
	}
158
159
	/**
160
	 *
161
	 */
162
	private function decodeAlphanumSegment(int $versionNumber, bool $fc1InEffect):string{
163
		$str = AlphaNum::decodeSegment($this->bitBuffer, $versionNumber);
164
165
		// See section 6.4.8.1, 6.4.8.2
166
		if($fc1InEffect){ // ???
167
			// We need to massage the result a bit if in an FNC1 mode:
168
			$str = str_replace(chr(0x1d), '%', $str);
169
			$str = str_replace('%%', '%', $str);
170
		}
171
172
		return $str;
173
	}
174
175
	/**
176
	 * @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
177
	 */
178
	private function decodeByteSegment(int $versionNumber):string{
179
		$str = Byte::decodeSegment($this->bitBuffer, $versionNumber);
180
181
		if($this->eciCharset === null){
182
			return $str;
183
		}
184
185
		$encoding = $this->eciCharset->getName();
186
187
		if($encoding === null){
188
			// The spec isn't clear on this mode; see
189
			// section 6.4.5: t does not say which encoding to assuming
190
			// upon decoding. I have seen ISO-8859-1 used as well as
191
			// Shift_JIS -- without anything like an ECI designator to
192
			// give a hint.
193
			$encoding = mb_detect_encoding($str, ['ISO-8859-1', 'Windows-1252', 'SJIS', 'UTF-8'], true);
194
195
			if($encoding === false){
196
				throw new QRCodeDecoderException('could not determine encoding in ECI mode');
197
			}
198
		}
199
200
		$this->eciCharset = null;
201
202
		return mb_convert_encoding($str, mb_internal_encoding(), $encoding);
0 ignored issues
show
Bug introduced by
It seems like mb_internal_encoding() can also be of type true; however, parameter $to_encoding of mb_convert_encoding() does only seem to accept string, 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

202
		return mb_convert_encoding($str, /** @scrutinizer ignore-type */ mb_internal_encoding(), $encoding);
Loading history...
203
	}
204
205
}
206