Total Complexity | 104 |
Total Lines | 566 |
Duplicated Lines | 0 % |
Changes | 16 | ||
Bugs | 0 | Features | 0 |
Complex classes like QRMatrix 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 QRMatrix, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class QRMatrix{ |
||
23 | |||
24 | /** @var int */ |
||
25 | public const M_NULL = 0b000000000000; |
||
26 | /** @var int */ |
||
27 | public const M_DARKMODULE = 0b000000000001; |
||
28 | /** @var int */ |
||
29 | public const M_DATA = 0b000000000010; |
||
30 | /** @var int */ |
||
31 | public const M_FINDER = 0b000000000100; |
||
32 | /** @var int */ |
||
33 | public const M_SEPARATOR = 0b000000001000; |
||
34 | /** @var int */ |
||
35 | public const M_ALIGNMENT = 0b000000010000; |
||
36 | /** @var int */ |
||
37 | public const M_TIMING = 0b000000100000; |
||
38 | /** @var int */ |
||
39 | public const M_FORMAT = 0b000001000000; |
||
40 | /** @var int */ |
||
41 | public const M_VERSION = 0b000010000000; |
||
42 | /** @var int */ |
||
43 | public const M_QUIETZONE = 0b000100000000; |
||
44 | /** @var int */ |
||
45 | public const M_LOGO = 0b001000000000; |
||
46 | /** @var int */ |
||
47 | public const M_FINDER_DOT = 0b010000000000; |
||
48 | /** @var int */ |
||
49 | public const M_TEST = 0b011111111111; |
||
50 | /** @var int */ |
||
51 | public const IS_DARK = 0b100000000000; |
||
52 | |||
53 | /** |
||
54 | * the used mask pattern, set via QRMatrix::mask() |
||
55 | */ |
||
56 | protected ?MaskPattern $maskPattern = null; |
||
57 | |||
58 | /** |
||
59 | * the current ECC level |
||
60 | */ |
||
61 | protected ?EccLevel $eccLevel = null; |
||
62 | |||
63 | /** |
||
64 | * a Version instance |
||
65 | */ |
||
66 | protected ?Version $version = null; |
||
67 | |||
68 | /** |
||
69 | * the size (side length) of the matrix, including quiet zone (if created) |
||
70 | */ |
||
71 | protected int $moduleCount; |
||
72 | |||
73 | /** |
||
74 | * the actual matrix data array |
||
75 | * |
||
76 | * @var int[][] |
||
77 | */ |
||
78 | protected array $matrix; |
||
79 | |||
80 | /** |
||
81 | * QRMatrix constructor. |
||
82 | */ |
||
83 | public function __construct(Version $version, EccLevel $eccLevel, MaskPattern $maskPattern){ |
||
84 | $this->version = $version; |
||
85 | $this->eccLevel = $eccLevel; |
||
86 | $this->maskPattern = $maskPattern; |
||
87 | $this->moduleCount = $this->version->getDimension(); |
||
88 | $this->matrix = array_fill(0, $this->moduleCount, array_fill(0, $this->moduleCount, $this::M_NULL)); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * shortcut to initialize the functional patterns |
||
93 | */ |
||
94 | public function initFunctionalPatterns():self{ |
||
103 | ; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Returns the data matrix, returns a pure boolean representation if $boolean is set to true |
||
108 | * |
||
109 | * @return int[][]|bool[][] |
||
110 | */ |
||
111 | public function matrix(bool $boolean = false):array{ |
||
112 | |||
113 | if(!$boolean){ |
||
114 | return $this->matrix; |
||
115 | } |
||
116 | |||
117 | $matrix = []; |
||
118 | |||
119 | foreach($this->matrix as $y => $row){ |
||
120 | $matrix[$y] = []; |
||
121 | |||
122 | foreach($row as $x => $val){ |
||
123 | $matrix[$y][$x] = ($val & $this::IS_DARK) === $this::IS_DARK; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | return $matrix; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Returns the current version number |
||
132 | */ |
||
133 | public function version():?Version{ |
||
134 | return $this->version; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Returns the current ECC level |
||
139 | */ |
||
140 | public function eccLevel():?EccLevel{ |
||
141 | return $this->eccLevel; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Returns the current mask pattern |
||
146 | */ |
||
147 | public function maskPattern():?MaskPattern{ |
||
148 | return $this->maskPattern; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Returns the absoulute size of the matrix, including quiet zone (after setting it). |
||
153 | * |
||
154 | * size = version * 4 + 17 [ + 2 * quietzone size] |
||
155 | */ |
||
156 | public function size():int{ |
||
157 | return $this->moduleCount; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Returns the value of the module at position [$x, $y] |
||
162 | */ |
||
163 | public function get(int $x, int $y):int{ |
||
164 | return $this->matrix[$y][$x]; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Sets the $M_TYPE value for the module at position [$x, $y] |
||
169 | * |
||
170 | * true => $M_TYPE | 0x800 |
||
171 | * false => $M_TYPE |
||
172 | */ |
||
173 | public function set(int $x, int $y, bool $value, int $M_TYPE):self{ |
||
174 | $this->matrix[$y][$x] = $M_TYPE | ($value ? $this::IS_DARK : 0); |
||
175 | |||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Flips the value of the module |
||
181 | */ |
||
182 | public function flip(int $x, int $y):self{ |
||
183 | $this->matrix[$y][$x] ^= $this::IS_DARK; |
||
184 | |||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Checks whether a module is of the given $M_TYPE |
||
190 | * |
||
191 | * true => $value & $M_TYPE === $M_TYPE |
||
192 | */ |
||
193 | public function checkType(int $x, int $y, int $M_TYPE):bool{ |
||
194 | return ($this->matrix[$y][$x] & $M_TYPE) === $M_TYPE; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * checks whether a module matches one of the given $M_TYPES |
||
199 | */ |
||
200 | public function checkTypes(int $x, int $y, array $M_TYPES):bool{ |
||
201 | |||
202 | foreach($M_TYPES as $type){ |
||
203 | if($this->checkType($x, $y, $type)){ |
||
204 | return true; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | return false; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Checks whether a module is true (dark) or false (light) |
||
213 | * |
||
214 | * true => $value & 0x800 === 0x800 |
||
215 | * false => $value & 0x800 === 0 |
||
216 | */ |
||
217 | public function check(int $x, int $y):bool{ |
||
218 | return $this->checkType($x, $y, $this::IS_DARK); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder |
||
223 | * |
||
224 | * 4 * version + 9 or moduleCount - 8 |
||
225 | */ |
||
226 | public function setDarkModule():self{ |
||
227 | $this->set(8, $this->moduleCount - 8, true, $this::M_DARKMODULE); |
||
228 | |||
229 | return $this; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Draws the 7x7 finder patterns in the corners top left/right and bottom left |
||
234 | * |
||
235 | * ISO/IEC 18004:2000 Section 7.3.2 |
||
236 | */ |
||
237 | public function setFinderPattern():self{ |
||
238 | |||
239 | $pos = [ |
||
240 | [0, 0], // top left |
||
241 | [$this->moduleCount - 7, 0], // bottom left |
||
242 | [0, $this->moduleCount - 7], // top right |
||
243 | ]; |
||
244 | |||
245 | foreach($pos as $c){ |
||
246 | for($y = 0; $y < 7; $y++){ |
||
247 | for($x = 0; $x < 7; $x++){ |
||
248 | // outer (dark) 7*7 square |
||
249 | if($x === 0 || $x === 6 || $y === 0 || $y === 6){ |
||
250 | $this->set($c[0] + $y, $c[1] + $x, true, $this::M_FINDER); |
||
251 | } |
||
252 | // inner (light) 5*5 square |
||
253 | elseif($x === 1 || $x === 5 || $y === 1 || $y === 5){ |
||
254 | $this->set($c[0] + $y, $c[1] + $x, false, $this::M_FINDER); |
||
255 | } |
||
256 | // 3*3 dot |
||
257 | else{ |
||
258 | $this->set($c[0] + $y, $c[1] + $x, true, $this::M_FINDER_DOT); |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | } |
||
263 | |||
264 | return $this; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Draws the separator lines around the finder patterns |
||
269 | * |
||
270 | * ISO/IEC 18004:2000 Section 7.3.3 |
||
271 | */ |
||
272 | public function setSeparators():self{ |
||
294 | } |
||
295 | |||
296 | |||
297 | /** |
||
298 | * Draws the 5x5 alignment patterns |
||
299 | * |
||
300 | * ISO/IEC 18004:2000 Section 7.3.5 |
||
301 | */ |
||
302 | public function setAlignmentPattern():self{ |
||
303 | $alignmentPattern = $this->version->getAlignmentPattern(); |
||
|
|||
304 | |||
305 | foreach($alignmentPattern as $y){ |
||
306 | foreach($alignmentPattern as $x){ |
||
307 | |||
308 | // skip existing patterns |
||
309 | if($this->matrix[$y][$x] !== $this::M_NULL){ |
||
310 | continue; |
||
311 | } |
||
312 | |||
313 | for($ry = -2; $ry <= 2; $ry++){ |
||
314 | for($rx = -2; $rx <= 2; $rx++){ |
||
315 | $v = ($ry === 0 && $rx === 0) || $ry === 2 || $ry === -2 || $rx === 2 || $rx === -2; |
||
316 | |||
317 | $this->set($x + $rx, $y + $ry, $v, $this::M_ALIGNMENT); |
||
318 | } |
||
319 | } |
||
320 | |||
321 | } |
||
322 | } |
||
323 | |||
324 | return $this; |
||
325 | } |
||
326 | |||
327 | |||
328 | /** |
||
329 | * Draws the timing pattern (h/v checkered line between the finder patterns) |
||
330 | * |
||
331 | * ISO/IEC 18004:2000 Section 7.3.4 |
||
332 | */ |
||
333 | public function setTimingPattern():self{ |
||
334 | |||
335 | foreach(range(8, $this->moduleCount - 8 - 1) as $i){ |
||
336 | |||
337 | if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){ |
||
338 | continue; |
||
339 | } |
||
340 | |||
341 | $v = $i % 2 === 0; |
||
342 | |||
343 | $this->set($i, 6, $v, $this::M_TIMING); // h |
||
344 | $this->set(6, $i, $v, $this::M_TIMING); // v |
||
345 | } |
||
346 | |||
347 | return $this; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Draws the version information, 2x 3x6 pixel |
||
352 | * |
||
353 | * ISO/IEC 18004:2000 Section 8.10 |
||
354 | */ |
||
355 | public function setVersionNumber():self{ |
||
356 | $bits = $this->version->getVersionPattern(); |
||
357 | |||
358 | if($bits !== null){ |
||
359 | |||
360 | for($i = 0; $i < 18; $i++){ |
||
361 | $a = (int)($i / 3); |
||
362 | $b = $i % 3 + $this->moduleCount - 8 - 3; |
||
363 | $v = (($bits >> $i) & 1) === 1; |
||
364 | |||
365 | $this->set($b, $a, $v, $this::M_VERSION); // ne |
||
366 | $this->set($a, $b, $v, $this::M_VERSION); // sw |
||
367 | } |
||
368 | |||
369 | } |
||
370 | |||
371 | return $this; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Draws the format info along the finder patterns |
||
376 | * |
||
377 | * ISO/IEC 18004:2000 Section 8.9 |
||
378 | */ |
||
379 | public function setFormatInfo():self{ |
||
380 | $bits = $this->eccLevel->getformatPattern($this->maskPattern); |
||
381 | |||
382 | for($i = 0; $i < 15; $i++){ |
||
383 | $v = (($bits >> $i) & 1) === 1; |
||
384 | |||
385 | if($i < 6){ |
||
386 | $this->set(8, $i, $v, $this::M_FORMAT); |
||
387 | } |
||
388 | elseif($i < 8){ |
||
389 | $this->set(8, $i + 1, $v, $this::M_FORMAT); |
||
390 | } |
||
391 | else{ |
||
392 | $this->set(8, $this->moduleCount - 15 + $i, $v, $this::M_FORMAT); |
||
393 | } |
||
394 | |||
395 | if($i < 8){ |
||
396 | $this->set($this->moduleCount - $i - 1, 8, $v, $this::M_FORMAT); |
||
397 | } |
||
398 | elseif($i < 9){ |
||
399 | $this->set(15 - $i, 8, $v, $this::M_FORMAT); |
||
400 | } |
||
401 | else{ |
||
402 | $this->set(15 - $i - 1, 8, $v, $this::M_FORMAT); |
||
403 | } |
||
404 | |||
405 | } |
||
406 | |||
407 | return $this; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Draws the "quiet zone" of $size around the matrix |
||
412 | * |
||
413 | * ISO/IEC 18004:2000 Section 7.3.7 |
||
414 | * |
||
415 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
416 | */ |
||
417 | public function setQuietZone(int $size = null):self{ |
||
418 | |||
419 | if($this->matrix[$this->moduleCount - 1][$this->moduleCount - 1] === $this::M_NULL){ |
||
420 | throw new QRCodeDataException('use only after writing data'); |
||
421 | } |
||
422 | |||
423 | $size = $size !== null |
||
424 | ? max(0, min($size, floor($this->moduleCount / 2))) |
||
425 | : 4; |
||
426 | |||
427 | for($y = 0; $y < $this->moduleCount; $y++){ |
||
428 | for($i = 0; $i < $size; $i++){ |
||
429 | array_unshift($this->matrix[$y], $this::M_QUIETZONE); |
||
430 | $this->matrix[$y][] = $this::M_QUIETZONE; |
||
431 | } |
||
432 | } |
||
433 | |||
434 | $this->moduleCount += ($size * 2); |
||
435 | |||
436 | $r = array_fill(0, $this->moduleCount, $this::M_QUIETZONE); |
||
437 | |||
438 | for($i = 0; $i < $size; $i++){ |
||
439 | array_unshift($this->matrix, $r); |
||
440 | $this->matrix[] = $r; |
||
441 | } |
||
442 | |||
443 | return $this; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Clears a space of $width * $height in order to add a logo or text. |
||
448 | * |
||
449 | * Additionally, the logo space can be positioned within the QR Code - respecting the main functional patterns - |
||
450 | * using $startX and $startY. If either of these are null, the logo space will be centered in that direction. |
||
451 | * ECC level "H" (30%) is required. |
||
452 | * |
||
453 | * Please note that adding a logo space minimizes the error correction capacity of the QR Code and |
||
454 | * created images may become unreadable, especially when printed with a chance to receive damage. |
||
455 | * Please test thoroughly before using this feature in production. |
||
456 | * |
||
457 | * This method should be called from within an output module (after the matrix has been filled with data). |
||
458 | * Note that there is no restiction on how many times this method could be called on the same matrix instance. |
||
459 | * |
||
460 | * @link https://github.com/chillerlan/php-qrcode/issues/52 |
||
461 | * |
||
462 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
463 | */ |
||
464 | public function setLogoSpace(int $width, int $height, int $startX = null, int $startY = null):self{ |
||
465 | |||
466 | // for logos we operate in ECC H (30%) only |
||
467 | if($this->eccLevel->getLevel() !== EccLevel::H){ |
||
468 | throw new QRCodeDataException('ECC level "H" required to add logo space'); |
||
469 | } |
||
470 | |||
471 | // if width and height happen to be exactly 0 (default value), just return - nothing to do |
||
472 | if($width === 0 || $height === 0){ |
||
473 | return $this; |
||
474 | } |
||
475 | |||
476 | // $this->moduleCount includes the quiet zone (if created), we need the QR size here |
||
477 | $length = $this->version->getDimension(); |
||
478 | |||
479 | // throw if the size is negative or exceeds the qrcode size |
||
480 | if($width < 0 || $height < 0 || $width > $length || $height > $length){ |
||
481 | throw new QRCodeDataException('invalid logo dimensions'); |
||
482 | } |
||
483 | |||
484 | // we need uneven sizes to center the logo space, adjust if needed |
||
485 | if($startX === null && ($width % 2) === 0){ |
||
486 | $width++; |
||
487 | } |
||
488 | |||
489 | if($startY === null && ($height % 2) === 0){ |
||
490 | $height++; |
||
491 | } |
||
492 | |||
493 | // throw if the logo space exceeds the maximum error correction capacity |
||
494 | if($width * $height > floor($length * $length * 0.2)){ |
||
495 | throw new QRCodeDataException('logo space exceeds the maximum error correction capacity'); |
||
496 | } |
||
497 | |||
498 | // quiet zone size |
||
499 | $qz = ($this->moduleCount - $length) / 2; |
||
500 | // skip quiet zone and the first 9 rows/columns (finder-, mode-, version- and timing patterns) |
||
501 | $start = $qz + 9; |
||
502 | // skip quiet zone |
||
503 | $end = $this->moduleCount - $qz; |
||
504 | |||
505 | // determine start coordinates |
||
506 | $startX = ($startX !== null ? $startX : ($length - $width) / 2) + $qz; |
||
507 | $startY = ($startY !== null ? $startY : ($length - $height) / 2) + $qz; |
||
508 | |||
509 | // clear the space |
||
510 | foreach($this->matrix as $y => $row){ |
||
511 | foreach($row as $x => $val){ |
||
512 | // out of bounds, skip |
||
513 | if($x < $start || $y < $start ||$x >= $end || $y >= $end){ |
||
514 | continue; |
||
515 | } |
||
516 | // a match |
||
517 | if($x >= $startX && $x < ($startX + $width) && $y >= $startY && $y < ($startY + $height)){ |
||
518 | $this->set($x, $y, false, $this::M_LOGO); |
||
519 | } |
||
520 | } |
||
521 | } |
||
522 | |||
523 | return $this; |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * Maps the interleaved binary $data on the matrix |
||
528 | */ |
||
529 | public function writeCodewords(BitBuffer $bitBuffer):self{ |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Applies/reverses the mask pattern |
||
573 | * |
||
574 | * ISO/IEC 18004:2000 Section 8.8.1 |
||
575 | */ |
||
576 | public function mask():self{ |
||
577 | $mask = $this->maskPattern->getMask(); |
||
588 | } |
||
589 | |||
590 | } |
||
591 |
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.