Total Complexity | 54 |
Total Lines | 348 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like BitMatrixParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BitMatrixParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | final class BitMatrixParser{ |
||
22 | |||
23 | private BitMatrix $bitMatrix; |
||
24 | private ?Version $parsedVersion = null; |
||
25 | private ?FormatInformation $parsedFormatInfo = null; |
||
26 | private bool $mirror = false; |
||
27 | |||
28 | /** |
||
29 | * @param \chillerlan\QRCode\Decoder\BitMatrix $bitMatrix |
||
30 | * |
||
31 | * @throws \RuntimeException if dimension is not >= 21 and 1 mod 4 |
||
32 | */ |
||
33 | public function __construct(BitMatrix $bitMatrix){ |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Prepare the parser for a mirrored operation. |
||
45 | * This flag has effect only on the {@link #readFormatInformation()} and the |
||
46 | * {@link #readVersion()}. Before proceeding with {@link #readCodewords()} the |
||
47 | * {@link #mirror()} method should be called. |
||
48 | * |
||
49 | * @param bool $mirror Whether to read version and format information mirrored. |
||
50 | */ |
||
51 | public function setMirror(bool $mirror):void{ |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Mirror the bit matrix in order to attempt a second reading. |
||
59 | */ |
||
60 | public function mirror():void{ |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * |
||
66 | */ |
||
67 | private function copyBit(int $i, int $j, int $versionBits):int{ |
||
68 | |||
69 | $bit = $this->mirror |
||
70 | ? $this->bitMatrix->get($j, $i) |
||
71 | : $this->bitMatrix->get($i, $j); |
||
72 | |||
73 | return $bit ? ($versionBits << 1) | 0x1 : $versionBits << 1; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * <p>Reads the bits in the {@link BitMatrix} representing the finder pattern in the |
||
78 | * correct order in order to reconstruct the codewords bytes contained within the |
||
79 | * QR Code.</p> |
||
80 | * |
||
81 | * @return array bytes encoded within the QR Code |
||
82 | * @throws \RuntimeException if the exact number of bytes expected is not read |
||
83 | */ |
||
84 | public function readCodewords():array{ |
||
85 | $formatInfo = $this->readFormatInformation(); |
||
86 | $version = $this->readVersion(); |
||
87 | |||
88 | // Get the data mask for the format used in this QR Code. This will exclude |
||
89 | // some bits from reading as we wind through the bit matrix. |
||
90 | $dimension = $this->bitMatrix->getDimension(); |
||
91 | $this->bitMatrix->unmask($dimension, $formatInfo->getDataMask()); |
||
92 | $functionPattern = $this->bitMatrix->buildFunctionPattern($version); |
||
93 | |||
94 | $readingUp = true; |
||
95 | $result = []; |
||
96 | $resultOffset = 0; |
||
97 | $currentByte = 0; |
||
98 | $bitsRead = 0; |
||
99 | // Read columns in pairs, from right to left |
||
100 | for($j = $dimension - 1; $j > 0; $j -= 2){ |
||
101 | |||
102 | if($j === 6){ |
||
103 | // Skip whole column with vertical alignment pattern; |
||
104 | // saves time and makes the other code proceed more cleanly |
||
105 | $j--; |
||
106 | } |
||
107 | // Read alternatingly from bottom to top then top to bottom |
||
108 | for($count = 0; $count < $dimension; $count++){ |
||
109 | $i = $readingUp ? $dimension - 1 - $count : $count; |
||
110 | |||
111 | for($col = 0; $col < 2; $col++){ |
||
112 | // Ignore bits covered by the function pattern |
||
113 | if(!$functionPattern->get($j - $col, $i)){ |
||
114 | // Read a bit |
||
115 | $bitsRead++; |
||
116 | $currentByte <<= 1; |
||
117 | |||
118 | if($this->bitMatrix->get($j - $col, $i)){ |
||
119 | $currentByte |= 1; |
||
120 | } |
||
121 | // If we've made a whole byte, save it off |
||
122 | if($bitsRead === 8){ |
||
123 | $result[$resultOffset++] = $currentByte; //(byte) |
||
124 | $bitsRead = 0; |
||
125 | $currentByte = 0; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | $readingUp = !$readingUp; // switch directions |
||
|
|||
132 | } |
||
133 | |||
134 | if($resultOffset !== $version->getTotalCodewords()){ |
||
135 | throw new RuntimeException('offset differs from total codewords for version'); |
||
136 | } |
||
137 | |||
138 | return $result; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * <p>Reads format information from one of its two locations within the QR Code.</p> |
||
143 | * |
||
144 | * @return \chillerlan\QRCode\Common\FormatInformation encapsulating the QR Code's format info |
||
145 | * @throws \RuntimeException if both format information locations cannot be parsed as |
||
146 | * the valid encoding of format information |
||
147 | */ |
||
148 | public function readFormatInformation():FormatInformation{ |
||
149 | |||
150 | if($this->parsedFormatInfo !== null){ |
||
151 | return $this->parsedFormatInfo; |
||
152 | } |
||
153 | |||
154 | // Read top-left format info bits |
||
155 | $formatInfoBits1 = 0; |
||
156 | |||
157 | for($i = 0; $i < 6; $i++){ |
||
158 | $formatInfoBits1 = $this->copyBit($i, 8, $formatInfoBits1); |
||
159 | } |
||
160 | |||
161 | // .. and skip a bit in the timing pattern ... |
||
162 | $formatInfoBits1 = $this->copyBit(7, 8, $formatInfoBits1); |
||
163 | $formatInfoBits1 = $this->copyBit(8, 8, $formatInfoBits1); |
||
164 | $formatInfoBits1 = $this->copyBit(8, 7, $formatInfoBits1); |
||
165 | // .. and skip a bit in the timing pattern ... |
||
166 | for($j = 5; $j >= 0; $j--){ |
||
167 | $formatInfoBits1 = $this->copyBit(8, $j, $formatInfoBits1); |
||
168 | } |
||
169 | |||
170 | // Read the top-right/bottom-left pattern too |
||
171 | $dimension = $this->bitMatrix->getDimension(); |
||
172 | $formatInfoBits2 = 0; |
||
173 | $jMin = $dimension - 7; |
||
174 | |||
175 | for($j = $dimension - 1; $j >= $jMin; $j--){ |
||
176 | $formatInfoBits2 = $this->copyBit(8, $j, $formatInfoBits2); |
||
177 | } |
||
178 | |||
179 | for($i = $dimension - 8; $i < $dimension; $i++){ |
||
180 | $formatInfoBits2 = $this->copyBit($i, 8, $formatInfoBits2); |
||
181 | } |
||
182 | |||
183 | $this->parsedFormatInfo = $this->doDecodeFormatInformation($formatInfoBits1, $formatInfoBits2); |
||
184 | |||
185 | if($this->parsedFormatInfo !== null){ |
||
186 | return $this->parsedFormatInfo; |
||
187 | } |
||
188 | |||
189 | // Should return null, but, some QR codes apparently do not mask this info. |
||
190 | // Try again by actually masking the pattern first. |
||
191 | $this->parsedFormatInfo = $this->doDecodeFormatInformation( |
||
192 | $formatInfoBits1 ^ FormatInformation::MASK_QR, |
||
193 | $formatInfoBits2 ^ FormatInformation::MASK_QR |
||
194 | ); |
||
195 | |||
196 | if($this->parsedFormatInfo !== null){ |
||
197 | return $this->parsedFormatInfo; |
||
198 | } |
||
199 | |||
200 | throw new RuntimeException('failed to read format info'); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param int $maskedFormatInfo1 format info indicator, with mask still applied |
||
205 | * @param int $maskedFormatInfo2 second copy of same info; both are checked at the same time |
||
206 | * to establish best match |
||
207 | * |
||
208 | * @return \chillerlan\QRCode\Common\FormatInformation|null information about the format it specifies, or null |
||
209 | * if doesn't seem to match any known pattern |
||
210 | */ |
||
211 | private function doDecodeFormatInformation(int $maskedFormatInfo1, int $maskedFormatInfo2):?FormatInformation{ |
||
212 | // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing |
||
213 | $bestDifference = PHP_INT_MAX; |
||
214 | $bestFormatInfo = 0; |
||
215 | |||
216 | foreach(FormatInformation::DECODE_LOOKUP as $decodeInfo){ |
||
217 | [$maskedBits, $dataBits] = $decodeInfo; |
||
218 | |||
219 | if($maskedFormatInfo1 === $dataBits || $maskedFormatInfo2 === $dataBits){ |
||
220 | // Found an exact match |
||
221 | return new FormatInformation($maskedBits); |
||
222 | } |
||
223 | |||
224 | $bitsDifference = self::numBitsDiffering($maskedFormatInfo1, $dataBits); |
||
225 | |||
226 | if($bitsDifference < $bestDifference){ |
||
227 | $bestFormatInfo = $maskedBits; |
||
228 | $bestDifference = $bitsDifference; |
||
229 | } |
||
230 | |||
231 | if($maskedFormatInfo1 !== $maskedFormatInfo2){ |
||
232 | // also try the other option |
||
233 | $bitsDifference = self::numBitsDiffering($maskedFormatInfo2, $dataBits); |
||
234 | |||
235 | if($bitsDifference < $bestDifference){ |
||
236 | $bestFormatInfo = $maskedBits; |
||
237 | $bestDifference = $bitsDifference; |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits differing means we found a match |
||
242 | if($bestDifference <= 3){ |
||
243 | return new FormatInformation($bestFormatInfo); |
||
244 | } |
||
245 | |||
246 | return null; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * <p>Reads version information from one of its two locations within the QR Code.</p> |
||
251 | * |
||
252 | * @return \chillerlan\QRCode\Common\Version encapsulating the QR Code's version |
||
253 | * @throws \RuntimeException if both version information locations cannot be parsed as |
||
254 | * the valid encoding of version information |
||
255 | */ |
||
256 | public function readVersion():Version{ |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param int $versionBits |
||
305 | * |
||
306 | * @return \chillerlan\QRCode\Common\Version|null |
||
307 | */ |
||
308 | private function decodeVersionInformation(int $versionBits):?Version{ |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * |
||
343 | */ |
||
344 | public static function uRShift(int $a, int $b):int{ |
||
345 | |||
346 | if($b === 0){ |
||
347 | return $a; |
||
348 | } |
||
349 | |||
350 | return ($a >> $b) & ~((1 << (8 * PHP_INT_SIZE - 1)) >> ($b - 1)); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * |
||
355 | */ |
||
356 | private static function numBitsDiffering(int $a, int $b):int{ |
||
369 | } |
||
370 | |||
372 |