Total Complexity | 98 |
Total Lines | 716 |
Duplicated Lines | 0 % |
Changes | 18 | ||
Bugs | 0 | Features | 3 |
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 IS_DARK = 0b100000000000; |
||
26 | /** @var int */ |
||
27 | public const M_NULL = 0b000000000000; |
||
28 | /** @var int */ |
||
29 | public const M_DARKMODULE = 0b100000000001; |
||
30 | /** @var int */ |
||
31 | public const M_DATA = 0b000000000010; |
||
32 | /** @var int */ |
||
33 | public const M_DATA_DARK = 0b100000000010; |
||
34 | /** @var int */ |
||
35 | public const M_FINDER = 0b000000000100; |
||
36 | /** @var int */ |
||
37 | public const M_FINDER_DARK = 0b100000000100; |
||
38 | /** @var int */ |
||
39 | public const M_SEPARATOR = 0b000000001000; |
||
40 | /** @var int */ |
||
41 | public const M_ALIGNMENT = 0b000000010000; |
||
42 | /** @var int */ |
||
43 | public const M_ALIGNMENT_DARK = 0b100000010000; |
||
44 | /** @var int */ |
||
45 | public const M_TIMING = 0b000000100000; |
||
46 | /** @var int */ |
||
47 | public const M_TIMING_DARK = 0b100000100000; |
||
48 | /** @var int */ |
||
49 | public const M_FORMAT = 0b000001000000; |
||
50 | /** @var int */ |
||
51 | public const M_FORMAT_DARK = 0b100001000000; |
||
52 | /** @var int */ |
||
53 | public const M_VERSION = 0b000010000000; |
||
54 | /** @var int */ |
||
55 | public const M_VERSION_DARK = 0b100010000000; |
||
56 | /** @var int */ |
||
57 | public const M_QUIETZONE = 0b000100000000; |
||
58 | /** @var int */ |
||
59 | public const M_QUIETZONE_DARK = 0b100100000000; |
||
60 | /** @var int */ |
||
61 | public const M_LOGO = 0b001000000000; |
||
62 | /** @var int */ |
||
63 | public const M_LOGO_DARK = 0b101000000000; |
||
64 | /** @var int */ |
||
65 | public const M_FINDER_DOT = 0b110000000000; |
||
66 | /** @var int */ |
||
67 | public const M_TEST = 0b011111111111; |
||
68 | /** @var int */ |
||
69 | public const M_TEST_DARK = 0b111111111111; |
||
70 | |||
71 | /** |
||
72 | * Map of flag => coord |
||
73 | * |
||
74 | * @see \chillerlan\QRCode\Data\QRMatrix::checkNeighbours() |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected const neighbours = [ |
||
79 | 0b00000001 => [-1, -1], |
||
80 | 0b00000010 => [ 0, -1], |
||
81 | 0b00000100 => [ 1, -1], |
||
82 | 0b00001000 => [ 1, 0], |
||
83 | 0b00010000 => [ 1, 1], |
||
84 | 0b00100000 => [ 0, 1], |
||
85 | 0b01000000 => [-1, 1], |
||
86 | 0b10000000 => [-1, 0], |
||
87 | ]; |
||
88 | |||
89 | /** |
||
90 | * the matrix version - always set in QRMatrix, may be null in BitMatrix |
||
91 | */ |
||
92 | protected ?Version $version = null; |
||
93 | |||
94 | /** |
||
95 | * the current ECC level - always set in QRMatrix, may be null in BitMatrix |
||
96 | */ |
||
97 | protected ?EccLevel $eccLevel = null; |
||
98 | |||
99 | /** |
||
100 | * the mask pattern that was used in the most recent operation, set via: |
||
101 | * |
||
102 | * - QRMatrix::setFormatInfo() |
||
103 | * - QRMatrix::mask() |
||
104 | * - BitMatrix::readFormatInformation() |
||
105 | */ |
||
106 | protected ?MaskPattern $maskPattern = null; |
||
107 | |||
108 | /** |
||
109 | * the size (side length) of the matrix, including quiet zone (if created) |
||
110 | */ |
||
111 | protected int $moduleCount; |
||
112 | |||
113 | /** |
||
114 | * the actual matrix data array |
||
115 | * |
||
116 | * @var int[][] |
||
117 | */ |
||
118 | protected array $matrix; |
||
119 | |||
120 | /** |
||
121 | * QRMatrix constructor. |
||
122 | */ |
||
123 | public function __construct(Version $version, EccLevel $eccLevel){ |
||
124 | $this->version = $version; |
||
125 | $this->eccLevel = $eccLevel; |
||
126 | $this->moduleCount = $this->version->getDimension(); |
||
127 | $this->matrix = $this->createMatrix($this->moduleCount, $this::M_NULL); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Creates a 2-dimensional array (square) of the given $size |
||
132 | */ |
||
133 | protected function createMatrix(int $size, int $value):array{ |
||
134 | return array_fill(0, $size, array_fill(0, $size, $value)); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * shortcut to initialize the functional patterns |
||
139 | */ |
||
140 | public function initFunctionalPatterns():self{ |
||
149 | ; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Returns the data matrix, returns a pure boolean representation if $boolean is set to true |
||
154 | * |
||
155 | * @return int[][]|bool[][] |
||
156 | */ |
||
157 | public function getMatrix(bool $boolean = null):array{ |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @deprecated 5.0.0 use QRMatrix::getMatrix() instead |
||
178 | * @see \chillerlan\QRCode\Data\QRMatrix::getMatrix() |
||
179 | * @codeCoverageIgnore |
||
180 | */ |
||
181 | public function matrix(bool $boolean = null):array{ |
||
182 | return $this->getMatrix($boolean); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Returns the current version number |
||
187 | */ |
||
188 | public function getVersion():?Version{ |
||
189 | return $this->version; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @deprecated 5.0.0 use QRMatrix::getVersion() instead |
||
194 | * @see \chillerlan\QRCode\Data\QRMatrix::getVersion() |
||
195 | * @codeCoverageIgnore |
||
196 | */ |
||
197 | public function version():?Version{ |
||
198 | return $this->getVersion(); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Returns the current ECC level |
||
203 | */ |
||
204 | public function getEccLevel():?EccLevel{ |
||
205 | return $this->eccLevel; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @deprecated 5.0.0 use QRMatrix::getEccLevel() instead |
||
210 | * @see \chillerlan\QRCode\Data\QRMatrix::getEccLevel() |
||
211 | * @codeCoverageIgnore |
||
212 | */ |
||
213 | public function eccLevel():?EccLevel{ |
||
214 | return $this->getEccLevel(); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Returns the current mask pattern |
||
219 | */ |
||
220 | public function getMaskPattern():?MaskPattern{ |
||
221 | return $this->maskPattern; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @deprecated 5.0.0 use QRMatrix::getMaskPattern() instead |
||
226 | * @see \chillerlan\QRCode\Data\QRMatrix::getMaskPattern() |
||
227 | * @codeCoverageIgnore |
||
228 | */ |
||
229 | public function maskPattern():?MaskPattern{ |
||
230 | return $this->getMaskPattern(); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Returns the absoulute size of the matrix, including quiet zone (after setting it). |
||
235 | * |
||
236 | * size = version * 4 + 17 [ + 2 * quietzone size] |
||
237 | */ |
||
238 | public function getSize():int{ |
||
239 | return $this->moduleCount; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @deprecated 5.0.0 use QRMatrix::getSize() instead |
||
244 | * @see \chillerlan\QRCode\Data\QRMatrix::getSize() |
||
245 | * @codeCoverageIgnore |
||
246 | */ |
||
247 | public function size():int{ |
||
248 | return $this->getSize(); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Returns the value of the module at position [$x, $y] or -1 if the coordinate is outside the matrix |
||
253 | */ |
||
254 | public function get(int $x, int $y):int{ |
||
255 | |||
256 | if(!isset($this->matrix[$y][$x])){ |
||
257 | return -1; |
||
258 | } |
||
259 | |||
260 | return $this->matrix[$y][$x]; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Sets the $M_TYPE value for the module at position [$x, $y] |
||
265 | * |
||
266 | * true => $M_TYPE | 0x800 |
||
267 | * false => $M_TYPE |
||
268 | */ |
||
269 | public function set(int $x, int $y, bool $value, int $M_TYPE):self{ |
||
270 | |||
271 | if(isset($this->matrix[$y][$x])){ |
||
272 | $this->matrix[$y][$x] = (($M_TYPE & ~$this::IS_DARK) | (($value) ? $this::IS_DARK : 0)); |
||
273 | } |
||
274 | |||
275 | return $this; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Fills an area of $width * $height, from the given starting point [$startX, $startY] (top left) with $value for $M_TYPE. |
||
280 | */ |
||
281 | public function setArea(int $startX, int $startY, int $width, int $height, bool $value, int $M_TYPE):self{ |
||
282 | |||
283 | for($y = $startY; $y < ($startY + $height); $y++){ |
||
284 | for($x = $startX; $x < ($startX + $width); $x++){ |
||
285 | $this->set($x, $y, $value, $M_TYPE); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | return $this; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Checks whether the module at ($x, $y) is of the given $M_TYPE |
||
294 | * |
||
295 | * true => $value & $M_TYPE === $M_TYPE |
||
296 | */ |
||
297 | public function checkType(int $x, int $y, int $M_TYPE):bool{ |
||
298 | $val = $this->get($x, $y); |
||
299 | |||
300 | if($val === -1){ |
||
301 | return false; |
||
302 | } |
||
303 | |||
304 | return ($val & $M_TYPE) === $M_TYPE; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * checks whether the module at ($x, $y) is in the given array of $M_TYPES, |
||
309 | * returns true if a match is found, otherwise false. |
||
310 | */ |
||
311 | public function checkTypeIn(int $x, int $y, array $M_TYPES):bool{ |
||
312 | |||
313 | foreach($M_TYPES as $type){ |
||
314 | if($this->checkType($x, $y, $type)){ |
||
315 | return true; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | return false; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Checks whether the module at ($x, $y) is true (dark) or false (light) |
||
324 | */ |
||
325 | public function check(int $x, int $y):bool{ |
||
326 | return $this->checkType($x, $y, $this::IS_DARK); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Checks the status of the neighbouring modules for the module at ($x, $y) and returns a bitmask with the results. |
||
331 | * |
||
332 | * The 8 flags of the bitmask represent the status of each of the neighbouring fields, |
||
333 | * starting with the lowest bit for top left, going clockwise: |
||
334 | * |
||
335 | * 1 2 3 |
||
336 | * 8 # 4 |
||
337 | * 7 6 5 |
||
338 | */ |
||
339 | public function checkNeighbours(int $x, int $y, int $M_TYPE = null):int{ |
||
340 | $bits = 0; |
||
341 | |||
342 | foreach($this::neighbours as $bit => $coord){ |
||
343 | [$ix, $iy] = $coord; |
||
344 | |||
345 | $ix += $x; |
||
346 | $iy += $y; |
||
347 | |||
348 | // $M_TYPE is given, skip if the field is not the same type |
||
349 | if($M_TYPE !== null && !$this->checkType($ix, $iy, $M_TYPE)){ |
||
350 | continue; |
||
351 | } |
||
352 | |||
353 | if($this->checkType($ix, $iy, $this::IS_DARK)){ |
||
354 | $bits |= $bit; |
||
355 | } |
||
356 | } |
||
357 | |||
358 | return $bits; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder |
||
363 | * |
||
364 | * 4 * version + 9 or moduleCount - 8 |
||
365 | */ |
||
366 | public function setDarkModule():self{ |
||
367 | $this->set(8, ($this->moduleCount - 8), true, $this::M_DARKMODULE); |
||
368 | |||
369 | return $this; |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Draws the 7x7 finder patterns in the corners top left/right and bottom left |
||
374 | * |
||
375 | * ISO/IEC 18004:2000 Section 7.3.2 |
||
376 | */ |
||
377 | public function setFinderPattern():self{ |
||
378 | |||
379 | $pos = [ |
||
380 | [0, 0], // top left |
||
381 | [($this->moduleCount - 7), 0], // top right |
||
382 | [0, ($this->moduleCount - 7)], // bottom left |
||
383 | ]; |
||
384 | |||
385 | foreach($pos as $c){ |
||
386 | $this |
||
387 | ->setArea($c[0], $c[1], 7, 7, true, $this::M_FINDER) |
||
388 | ->setArea(($c[0] + 1), ($c[1] + 1), 5, 5, false, $this::M_FINDER) |
||
389 | ->setArea(($c[0] + 2), ($c[1] + 2), 3, 3, true, $this::M_FINDER_DOT) |
||
390 | ; |
||
391 | } |
||
392 | |||
393 | return $this; |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Draws the separator lines around the finder patterns |
||
398 | * |
||
399 | * ISO/IEC 18004:2000 Section 7.3.3 |
||
400 | */ |
||
401 | public function setSeparators():self{ |
||
423 | } |
||
424 | |||
425 | |||
426 | /** |
||
427 | * Draws the 5x5 alignment patterns |
||
428 | * |
||
429 | * ISO/IEC 18004:2000 Section 7.3.5 |
||
430 | */ |
||
431 | public function setAlignmentPattern():self{ |
||
432 | $alignmentPattern = $this->version->getAlignmentPattern(); |
||
|
|||
433 | |||
434 | foreach($alignmentPattern as $y){ |
||
435 | foreach($alignmentPattern as $x){ |
||
436 | |||
437 | // skip existing patterns |
||
438 | if($this->matrix[$y][$x] !== $this::M_NULL){ |
||
439 | continue; |
||
440 | } |
||
441 | |||
442 | $this |
||
443 | ->setArea(($x - 2), ($y - 2), 5, 5, true, $this::M_ALIGNMENT) |
||
444 | ->setArea(($x - 1), ($y - 1), 3, 3, false, $this::M_ALIGNMENT) |
||
445 | ->set($x, $y, true, $this::M_ALIGNMENT) |
||
446 | ; |
||
447 | |||
448 | } |
||
449 | } |
||
450 | |||
451 | return $this; |
||
452 | } |
||
453 | |||
454 | |||
455 | /** |
||
456 | * Draws the timing pattern (h/v checkered line between the finder patterns) |
||
457 | * |
||
458 | * ISO/IEC 18004:2000 Section 7.3.4 |
||
459 | */ |
||
460 | public function setTimingPattern():self{ |
||
461 | |||
462 | for($i = 8; $i < ($this->moduleCount - 8); $i++){ |
||
463 | |||
464 | if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){ |
||
465 | continue; |
||
466 | } |
||
467 | |||
468 | $v = ($i % 2) === 0; |
||
469 | |||
470 | $this->set($i, 6, $v, $this::M_TIMING); // h |
||
471 | $this->set(6, $i, $v, $this::M_TIMING); // v |
||
472 | } |
||
473 | |||
474 | return $this; |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * Draws the version information, 2x 3x6 pixel |
||
479 | * |
||
480 | * ISO/IEC 18004:2000 Section 8.10 |
||
481 | */ |
||
482 | public function setVersionNumber():self{ |
||
483 | $bits = $this->version->getVersionPattern(); |
||
484 | |||
485 | if($bits !== null){ |
||
486 | |||
487 | for($i = 0; $i < 18; $i++){ |
||
488 | $a = (int)($i / 3); |
||
489 | $b = (($i % 3) + ($this->moduleCount - 8 - 3)); |
||
490 | $v = (($bits >> $i) & 1) === 1; |
||
491 | |||
492 | $this->set($b, $a, $v, $this::M_VERSION); // ne |
||
493 | $this->set($a, $b, $v, $this::M_VERSION); // sw |
||
494 | } |
||
495 | |||
496 | } |
||
497 | |||
498 | return $this; |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * Draws the format info along the finder patterns. If no $maskPattern, all format info modules will be set to false. |
||
503 | * |
||
504 | * ISO/IEC 18004:2000 Section 8.9 |
||
505 | */ |
||
506 | public function setFormatInfo(MaskPattern $maskPattern = null):self{ |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * Draws the "quiet zone" of $size around the matrix |
||
543 | * |
||
544 | * ISO/IEC 18004:2000 Section 7.3.7 |
||
545 | * |
||
546 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
547 | */ |
||
548 | public function setQuietZone(int $quietZoneSize):self{ |
||
549 | |||
570 | } |
||
571 | |||
572 | /** |
||
573 | * Rotates the matrix by 90 degrees clock wise |
||
574 | */ |
||
575 | public function rotate90():self{ |
||
576 | /** @phan-suppress-next-line PhanParamTooFewInternalUnpack */ |
||
577 | $this->matrix = array_map((fn(int ...$a):array => array_reverse($a)), ...$this->matrix); |
||
578 | |||
579 | return $this; |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * Clears a space of $width * $height in order to add a logo or text. |
||
584 | * If no $height is given, the space will be assumed a square of $width. |
||
585 | * |
||
586 | * Additionally, the logo space can be positioned within the QR Code - respecting the main functional patterns - |
||
587 | * using $startX and $startY. If either of these are null, the logo space will be centered in that direction. |
||
588 | * ECC level "H" (30%) is required. |
||
589 | * |
||
590 | * The coordinates of $startX and $startY do not include the quiet zone: |
||
591 | * [0, 0] is always the top left module of the top left finder pattern, negative values go into the quiet zone top and left. |
||
592 | * |
||
593 | * Please note that adding a logo space minimizes the error correction capacity of the QR Code and |
||
594 | * created images may become unreadable, especially when printed with a chance to receive damage. |
||
595 | * Please test thoroughly before using this feature in production. |
||
596 | * |
||
597 | * This method should be called from within an output module (after the matrix has been filled with data). |
||
598 | * Note that there is no restiction on how many times this method could be called on the same matrix instance. |
||
599 | * |
||
600 | * @link https://github.com/chillerlan/php-qrcode/issues/52 |
||
601 | * |
||
602 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
603 | */ |
||
604 | public function setLogoSpace(int $width, int $height = null, int $startX = null, int $startY = null):self{ |
||
605 | |||
606 | // for logos, we operate in ECC H (30%) only |
||
607 | if($this->eccLevel->getLevel() !== EccLevel::H){ |
||
608 | throw new QRCodeDataException('ECC level "H" required to add logo space'); |
||
609 | } |
||
610 | |||
611 | if($height === null){ |
||
612 | $height = $width; |
||
613 | } |
||
614 | |||
615 | // if width and height happen to be negative or 0 (default value), just return - nothing to do |
||
616 | if($width <= 0 || $height <= 0){ |
||
617 | return $this; // @codeCoverageIgnore |
||
618 | } |
||
619 | |||
620 | // $this->moduleCount includes the quiet zone (if created), we need the QR size here |
||
621 | $length = $this->version->getDimension(); |
||
622 | |||
623 | // throw if the size is exceeds the qrcode size |
||
624 | if($width > $length || $height > $length){ |
||
625 | throw new QRCodeDataException('logo dimensions exceed matrix size'); |
||
626 | } |
||
627 | |||
628 | // we need uneven sizes to center the logo space, adjust if needed |
||
629 | if($startX === null && ($width % 2) === 0){ |
||
630 | $width++; |
||
631 | } |
||
632 | |||
633 | if($startY === null && ($height % 2) === 0){ |
||
634 | $height++; |
||
635 | } |
||
636 | |||
637 | // throw if the logo space exceeds the maximum error correction capacity |
||
638 | if(($width * $height) > floor($length * $length * 0.2)){ |
||
639 | throw new QRCodeDataException('logo space exceeds the maximum error correction capacity'); |
||
640 | } |
||
641 | |||
642 | // quiet zone size |
||
643 | $qz = (($this->moduleCount - $length) / 2); |
||
644 | // skip quiet zone and the first 9 rows/columns (finder-, mode-, version- and timing patterns) |
||
645 | $start = ($qz + 9); |
||
646 | // skip quiet zone |
||
647 | $end = ($this->moduleCount - $qz); |
||
648 | |||
649 | // determine start coordinates |
||
650 | $startX = ((($startX !== null) ? $startX : ($length - $width) / 2) + $qz); |
||
651 | $startY = ((($startY !== null) ? $startY : ($length - $height) / 2) + $qz); |
||
652 | $endX = ($startX + $width); |
||
653 | $endY = ($startY + $height); |
||
654 | |||
655 | // clear the space |
||
656 | for($y = $startY; $y < $endY; $y++){ |
||
657 | for($x = $startX; $x < $endX; $x++){ |
||
658 | // out of bounds, skip |
||
659 | if($x < $start || $y < $start ||$x >= $end || $y >= $end){ |
||
660 | continue; |
||
661 | } |
||
662 | |||
663 | $this->set($x, $y, false, $this::M_LOGO); |
||
664 | } |
||
665 | } |
||
666 | |||
667 | return $this; |
||
668 | } |
||
669 | |||
670 | /** |
||
671 | * Maps the interleaved binary $data on the matrix |
||
672 | */ |
||
673 | public function writeCodewords(BitBuffer $bitBuffer):self{ |
||
713 | } |
||
714 | |||
715 | /** |
||
716 | * Applies/reverses the mask pattern |
||
717 | * |
||
718 | * ISO/IEC 18004:2000 Section 8.8.1 |
||
719 | */ |
||
720 | public function mask(MaskPattern $maskPattern):self{ |
||
738 | } |
||
739 | |||
740 | } |
||
741 |
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.