Skip to content

Commit a7e9642

Browse files
committed
🚿 extract several properties in Decoder
1 parent 24c7a14 commit a7e9642

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

src/Decoder/Decoder.php

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace chillerlan\QRCode\Decoder;
1313

1414
use Throwable;
15-
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, Mode, ReedSolomonDecoder, Version};
15+
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, FormatInformation, Mode, ReedSolomonDecoder, Version};
1616
use chillerlan\QRCode\Data\{AlphaNum, Byte, ECI, Kanji, Number};
1717
use chillerlan\QRCode\Detector\Detector;
1818
use function count, array_fill, mb_convert_encoding, mb_detect_encoding;
@@ -27,6 +27,10 @@ final class Decoder{
2727

2828
# private const GB2312_SUBSET = 1;
2929

30+
private ?Version $version = null;
31+
private ?FormatInformation $formatInfo = null;
32+
private EccLevel $eccLevel;
33+
3034
/**
3135
* Decodes a QR Code represented as a BitMatrix.
3236
* A 1 or "true" is taken to mean a black module.
@@ -70,22 +74,18 @@ public function decode(LuminanceSourceInterface $source):DecoderResult{
7074
*/
7175
private function decodeMatrix(BitMatrix $bitMatrix):DecoderResult{
7276
// Read raw codewords
73-
$rawCodewords = $bitMatrix->readCodewords();
74-
$version = $bitMatrix->getVersion();
75-
$formatInfo = $bitMatrix->getFormatInfo();
77+
$rawCodewords = $bitMatrix->readCodewords();
78+
$this->version = $bitMatrix->getVersion();
79+
$this->formatInfo = $bitMatrix->getFormatInfo();
7680

77-
// technically this shouldn't happen as the respective read meathods would throw first
78-
if($version === null || $formatInfo === null){
79-
throw new QRCodeDecoderException('unable to read version or ecc level');
81+
if($this->version === null || $this->formatInfo === null){
82+
throw new QRCodeDecoderException('unable to read version or format info'); // @codeCoverageIgnore
8083
}
8184

82-
$eccLevel = $formatInfo->getErrorCorrectionLevel();
83-
84-
// Separate into data blocks
85-
$dataBlocks = $this->getDataBlocks($rawCodewords, $version, $eccLevel);
86-
87-
$resultBytes = [];
88-
$resultOffset = 0;
85+
$this->eccLevel = $this->formatInfo->getErrorCorrectionLevel();
86+
$dataBlocks = $this->getDataBlocks($rawCodewords);
87+
$resultBytes = [];
88+
$resultOffset = 0;
8989

9090
// Error-correct and copy data blocks together into a stream of bytes
9191
foreach($dataBlocks as $dataBlock){
@@ -99,30 +99,24 @@ private function decodeMatrix(BitMatrix $bitMatrix):DecoderResult{
9999
}
100100

101101
// Decode the contents of that stream of bytes
102-
return $this->decodeBitStream($resultBytes, $version, $eccLevel);
102+
return $this->decodeBitStream($resultBytes);
103103
}
104104

105105
/**
106106
* When QR Codes use multiple data blocks, they are actually interleaved.
107107
* That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This
108108
* method will separate the data into original blocks.
109109
*
110-
* @param array $rawCodewords bytes as read directly from the QR Code
111-
* @param \chillerlan\QRCode\Common\Version $version version of the QR Code
112-
* @param \chillerlan\QRCode\Common\EccLevel $eccLevel error-correction level of the QR Code
110+
* @param array $rawCodewords bytes as read directly from the QR Code
113111
*
114112
* @return array DataBlocks containing original bytes, "de-interleaved" from representation in the QR Code
115113
* @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
116114
*/
117-
private function getDataBlocks(array $rawCodewords, Version $version, EccLevel $eccLevel):array{
118-
119-
if(count($rawCodewords) !== $version->getTotalCodewords()){
120-
throw new QRCodeDecoderException('$rawCodewords differ from total codewords for version');
121-
}
115+
private function getDataBlocks(array $rawCodewords):array{
122116

123117
// Figure out the number and size of data blocks used by this version and
124118
// error correction level
125-
[$numEccCodewords, $eccBlocks] = $version->getRSBlocks($eccLevel);
119+
[$numEccCodewords, $eccBlocks] = $this->version->getRSBlocks($this->eccLevel);
126120

127121
// Now establish DataBlocks of the appropriate size and number of data codewords
128122
$result = [];//new DataBlock[$totalBlocks];
@@ -210,11 +204,11 @@ private function correctErrors(array $codewordBytes, int $numDataCodewords):arra
210204
/**
211205
* @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
212206
*/
213-
private function decodeBitStream(array $bytes, Version $version, EccLevel $ecLevel):DecoderResult{
207+
private function decodeBitStream(array $bytes):DecoderResult{
214208
$bits = new BitBuffer($bytes);
215209
$symbolSequence = -1;
216210
$parityData = -1;
217-
$versionNumber = $version->getVersionNumber();
211+
$versionNumber = $this->version->getVersionNumber();
218212

219213
$result = '';
220214
$eciCharset = null;
@@ -319,8 +313,9 @@ private function decodeBitStream(array $bytes, Version $version, EccLevel $ecLev
319313
return new DecoderResult([
320314
'rawBytes' => $bytes,
321315
'data' => $result,
322-
'version' => $version,
323-
'eccLevel' => $ecLevel,
316+
'version' => $this->version,
317+
'eccLevel' => $this->eccLevel,
318+
'maskPattern' => $this->formatInfo->getMaskPattern(),
324319
'structuredAppendParity' => $parityData,
325320
'structuredAppendSequence' => $symbolSequence
326321
]);

0 commit comments

Comments
 (0)