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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
23 | class QRMatrix{ |
||
24 | |||
25 | public const M_NULL = 0x00; |
||
26 | public const M_DARKMODULE = 0x02; |
||
27 | public const M_DATA = 0x04; |
||
28 | public const M_FINDER = 0x06; |
||
29 | public const M_SEPARATOR = 0x08; |
||
30 | public const M_ALIGNMENT = 0x0a; |
||
31 | public const M_TIMING = 0x0c; |
||
32 | public const M_FORMAT = 0x0e; |
||
33 | public const M_VERSION = 0x10; |
||
34 | public const M_QUIETZONE = 0x12; |
||
35 | public const M_LOGO = 0x14; |
||
36 | public const M_FINDER_DOT = 0x16; |
||
37 | |||
38 | public const M_TEST = 0xff; |
||
39 | |||
40 | /** |
||
41 | * @link http://www.thonky.com/qr-code-tutorial/alignment-pattern-locations |
||
42 | * |
||
43 | * version -> pattern |
||
44 | */ |
||
45 | protected const alignmentPattern = [ |
||
46 | 1 => [], |
||
47 | 2 => [6, 18], |
||
48 | 3 => [6, 22], |
||
49 | 4 => [6, 26], |
||
50 | 5 => [6, 30], |
||
51 | 6 => [6, 34], |
||
52 | 7 => [6, 22, 38], |
||
53 | 8 => [6, 24, 42], |
||
54 | 9 => [6, 26, 46], |
||
55 | 10 => [6, 28, 50], |
||
56 | 11 => [6, 30, 54], |
||
57 | 12 => [6, 32, 58], |
||
58 | 13 => [6, 34, 62], |
||
59 | 14 => [6, 26, 46, 66], |
||
60 | 15 => [6, 26, 48, 70], |
||
61 | 16 => [6, 26, 50, 74], |
||
62 | 17 => [6, 30, 54, 78], |
||
63 | 18 => [6, 30, 56, 82], |
||
64 | 19 => [6, 30, 58, 86], |
||
65 | 20 => [6, 34, 62, 90], |
||
66 | 21 => [6, 28, 50, 72, 94], |
||
67 | 22 => [6, 26, 50, 74, 98], |
||
68 | 23 => [6, 30, 54, 78, 102], |
||
69 | 24 => [6, 28, 54, 80, 106], |
||
70 | 25 => [6, 32, 58, 84, 110], |
||
71 | 26 => [6, 30, 58, 86, 114], |
||
72 | 27 => [6, 34, 62, 90, 118], |
||
73 | 28 => [6, 26, 50, 74, 98, 122], |
||
74 | 29 => [6, 30, 54, 78, 102, 126], |
||
75 | 30 => [6, 26, 52, 78, 104, 130], |
||
76 | 31 => [6, 30, 56, 82, 108, 134], |
||
77 | 32 => [6, 34, 60, 86, 112, 138], |
||
78 | 33 => [6, 30, 58, 86, 114, 142], |
||
79 | 34 => [6, 34, 62, 90, 118, 146], |
||
80 | 35 => [6, 30, 54, 78, 102, 126, 150], |
||
81 | 36 => [6, 24, 50, 76, 102, 128, 154], |
||
82 | 37 => [6, 28, 54, 80, 106, 132, 158], |
||
83 | 38 => [6, 32, 58, 84, 110, 136, 162], |
||
84 | 39 => [6, 26, 54, 82, 110, 138, 166], |
||
85 | 40 => [6, 30, 58, 86, 114, 142, 170], |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * @link http://www.thonky.com/qr-code-tutorial/format-version-tables |
||
90 | * |
||
91 | * no version pattern for QR Codes < 7 |
||
92 | */ |
||
93 | protected const versionPattern = [ |
||
94 | 7 => 0b000111110010010100, |
||
95 | 8 => 0b001000010110111100, |
||
96 | 9 => 0b001001101010011001, |
||
97 | 10 => 0b001010010011010011, |
||
98 | 11 => 0b001011101111110110, |
||
99 | 12 => 0b001100011101100010, |
||
100 | 13 => 0b001101100001000111, |
||
101 | 14 => 0b001110011000001101, |
||
102 | 15 => 0b001111100100101000, |
||
103 | 16 => 0b010000101101111000, |
||
104 | 17 => 0b010001010001011101, |
||
105 | 18 => 0b010010101000010111, |
||
106 | 19 => 0b010011010100110010, |
||
107 | 20 => 0b010100100110100110, |
||
108 | 21 => 0b010101011010000011, |
||
109 | 22 => 0b010110100011001001, |
||
110 | 23 => 0b010111011111101100, |
||
111 | 24 => 0b011000111011000100, |
||
112 | 25 => 0b011001000111100001, |
||
113 | 26 => 0b011010111110101011, |
||
114 | 27 => 0b011011000010001110, |
||
115 | 28 => 0b011100110000011010, |
||
116 | 29 => 0b011101001100111111, |
||
117 | 30 => 0b011110110101110101, |
||
118 | 31 => 0b011111001001010000, |
||
119 | 32 => 0b100000100111010101, |
||
120 | 33 => 0b100001011011110000, |
||
121 | 34 => 0b100010100010111010, |
||
122 | 35 => 0b100011011110011111, |
||
123 | 36 => 0b100100101100001011, |
||
124 | 37 => 0b100101010000101110, |
||
125 | 38 => 0b100110101001100100, |
||
126 | 39 => 0b100111010101000001, |
||
127 | 40 => 0b101000110001101001, |
||
128 | ]; |
||
129 | |||
130 | // ECC level -> mask pattern |
||
131 | protected const formatPattern = [ |
||
132 | [ // L |
||
133 | 0b111011111000100, |
||
134 | 0b111001011110011, |
||
135 | 0b111110110101010, |
||
136 | 0b111100010011101, |
||
137 | 0b110011000101111, |
||
138 | 0b110001100011000, |
||
139 | 0b110110001000001, |
||
140 | 0b110100101110110, |
||
141 | ], |
||
142 | [ // M |
||
143 | 0b101010000010010, |
||
144 | 0b101000100100101, |
||
145 | 0b101111001111100, |
||
146 | 0b101101101001011, |
||
147 | 0b100010111111001, |
||
148 | 0b100000011001110, |
||
149 | 0b100111110010111, |
||
150 | 0b100101010100000, |
||
151 | ], |
||
152 | [ // Q |
||
153 | 0b011010101011111, |
||
154 | 0b011000001101000, |
||
155 | 0b011111100110001, |
||
156 | 0b011101000000110, |
||
157 | 0b010010010110100, |
||
158 | 0b010000110000011, |
||
159 | 0b010111011011010, |
||
160 | 0b010101111101101, |
||
161 | ], |
||
162 | [ // H |
||
163 | 0b001011010001001, |
||
164 | 0b001001110111110, |
||
165 | 0b001110011100111, |
||
166 | 0b001100111010000, |
||
167 | 0b000011101100010, |
||
168 | 0b000001001010101, |
||
169 | 0b000110100001100, |
||
170 | 0b000100000111011, |
||
171 | ], |
||
172 | ]; |
||
173 | |||
174 | /** |
||
175 | * @var int |
||
176 | */ |
||
177 | protected $version; |
||
178 | |||
179 | /** |
||
180 | * @var int |
||
181 | */ |
||
182 | protected $eclevel; |
||
183 | |||
184 | /** |
||
185 | * @var int |
||
186 | */ |
||
187 | protected $maskPattern = QRCode::MASK_PATTERN_AUTO; |
||
188 | |||
189 | /** |
||
190 | * @var int |
||
191 | */ |
||
192 | protected $moduleCount; |
||
193 | |||
194 | /** |
||
195 | * @var mixed[] |
||
196 | */ |
||
197 | protected $matrix; |
||
198 | |||
199 | /** |
||
200 | * QRMatrix constructor. |
||
201 | * |
||
202 | * @param int $version |
||
203 | * @param int $eclevel |
||
204 | * |
||
205 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
206 | */ |
||
207 | public function __construct(int $version, int $eclevel){ |
||
222 | |||
223 | /** |
||
224 | * Returns the data matrix, returns a pure boolean representation if $boolean is set to true |
||
225 | * |
||
226 | * @return int[][]|bool[][] |
||
227 | */ |
||
228 | public function matrix(bool $boolean = false):array{ |
||
246 | |||
247 | /** |
||
248 | * @return int |
||
249 | */ |
||
250 | public function version():int{ |
||
253 | |||
254 | /** |
||
255 | * @return int |
||
256 | */ |
||
257 | public function eccLevel():int{ |
||
260 | |||
261 | /** |
||
262 | * @return int |
||
263 | */ |
||
264 | public function maskPattern():int{ |
||
267 | |||
268 | /** |
||
269 | * Returns the absoulute size of the matrix, including quiet zone (after setting it). |
||
270 | * |
||
271 | * size = version * 4 + 17 [ + 2 * quietzone size] |
||
272 | * |
||
273 | * @return int |
||
274 | */ |
||
275 | public function size():int{ |
||
278 | |||
279 | /** |
||
280 | * Returns the value of the module at position [$x, $y] |
||
281 | * |
||
282 | * @param int $x |
||
283 | * @param int $y |
||
284 | * |
||
285 | * @return int |
||
286 | */ |
||
287 | public function get(int $x, int $y):int{ |
||
290 | |||
291 | /** |
||
292 | * Sets the $M_TYPE value for the module at position [$x, $y] |
||
293 | * |
||
294 | * true => $M_TYPE << 8 |
||
295 | * false => $M_TYPE |
||
296 | * |
||
297 | * @param int $x |
||
298 | * @param int $y |
||
299 | * @param int $M_TYPE |
||
300 | * @param bool $value |
||
301 | * |
||
302 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
303 | */ |
||
304 | public function set(int $x, int $y, bool $value, int $M_TYPE):QRMatrix{ |
||
309 | |||
310 | /** |
||
311 | * Checks whether a module is true (dark) or false (light) |
||
312 | * |
||
313 | * true => $value >> 8 === $M_TYPE |
||
314 | * $value >> 8 > 0 |
||
315 | * |
||
316 | * false => $value === $M_TYPE |
||
317 | * $value >> 8 === 0 |
||
318 | * |
||
319 | * @param int $x |
||
320 | * @param int $y |
||
321 | * |
||
322 | * @return bool |
||
323 | */ |
||
324 | public function check(int $x, int $y):bool{ |
||
327 | |||
328 | |||
329 | /** |
||
330 | * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder |
||
331 | * |
||
332 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
333 | */ |
||
334 | public function setDarkModule():QRMatrix{ |
||
339 | |||
340 | /** |
||
341 | * Draws the 7x7 finder patterns in the corners top left/right and bottom left |
||
342 | * |
||
343 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
344 | */ |
||
345 | public function setFinderPattern():QRMatrix{ |
||
374 | |||
375 | /** |
||
376 | * Draws the separator lines around the finder patterns |
||
377 | * |
||
378 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
379 | */ |
||
380 | public function setSeparators():QRMatrix{ |
||
403 | |||
404 | |||
405 | /** |
||
406 | * Draws the 5x5 alignment patterns |
||
407 | * |
||
408 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
409 | */ |
||
410 | public function setAlignmentPattern():QRMatrix{ |
||
433 | |||
434 | |||
435 | /** |
||
436 | * Draws the timing pattern (h/v checkered line between the finder patterns) |
||
437 | * |
||
438 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
439 | */ |
||
440 | public function setTimingPattern():QRMatrix{ |
||
456 | |||
457 | /** |
||
458 | * Draws the version information, 2x 3x6 pixel |
||
459 | * |
||
460 | * @param bool|null $test |
||
461 | * |
||
462 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
463 | */ |
||
464 | public function setVersionNumber(bool $test = null):QRMatrix{ |
||
482 | |||
483 | /** |
||
484 | * Draws the format info along the finder patterns |
||
485 | * |
||
486 | * @param int $maskPattern |
||
487 | * @param bool|null $test |
||
488 | * |
||
489 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
490 | */ |
||
491 | public function setFormatInfo(int $maskPattern, bool $test = null):QRMatrix{ |
||
523 | |||
524 | /** |
||
525 | * Draws the "quiet zone" of $size around the matrix |
||
526 | * |
||
527 | * @param int|null $size |
||
528 | * |
||
529 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
530 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
531 | */ |
||
532 | public function setQuietZone(int $size = null):QRMatrix{ |
||
560 | |||
561 | /** |
||
562 | * Clears a space of $width * $height in order to add a logo or text. |
||
563 | * |
||
564 | * Additionally, the logo space can be positioned within the QR Code - respecting the main functional patterns - |
||
565 | * using $startX and $startY. If either of these are null, the logo space will be centered in that direction. |
||
566 | * ECC level "H" (30%) is required. |
||
567 | * |
||
568 | * Please note that adding a logo space minimizes the error correction capacity of the QR Code and |
||
569 | * created images may become unreadable, especially when printed with a chance to receive damage. |
||
570 | * Please test thoroughly before using this feature in production. |
||
571 | * |
||
572 | * This method should be called from within an output module (after the matrix has been filled with data). |
||
573 | * Note that there is no restiction on how many times this method could be called on the same matrix instance. |
||
574 | * |
||
575 | * @link https://github.com/chillerlan/php-qrcode/issues/52 |
||
576 | * |
||
577 | * @param int $width |
||
578 | * @param int $height |
||
579 | * @param int|null $startX |
||
580 | * @param int|null $startY |
||
581 | * |
||
582 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
583 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
584 | */ |
||
585 | public function setLogoSpace(int $width, int $height, int $startX = null, int $startY = null):QRMatrix{ |
||
586 | |||
587 | // for logos we operate in ECC H (30%) only |
||
588 | if($this->eclevel !== 0b10){ |
||
589 | throw new QRCodeDataException('ECC level "H" required to add logo space'); |
||
590 | } |
||
591 | |||
592 | // we need uneven sizes to center the logo space, adjust if needed |
||
593 | if($startX === null && ($width % 2) === 0){ |
||
594 | $width++; |
||
595 | } |
||
596 | |||
597 | if($startY === null && ($height % 2) === 0){ |
||
598 | $height++; |
||
599 | } |
||
600 | |||
601 | // $this->moduleCount includes the quiet zone (if created), we need the QR size here |
||
602 | $length = $this->version * 4 + 17; |
||
603 | |||
604 | // throw if the logo space exceeds the maximum error correction capacity |
||
605 | if($width * $height > floor($length * $length * 0.2)){ |
||
606 | throw new QRCodeDataException('logo space exceeds the maximum error correction capacity'); |
||
607 | } |
||
608 | |||
609 | // quiet zone size |
||
610 | $qz = ($this->moduleCount - $length) / 2; |
||
611 | // skip quiet zone and the first 9 rows/columns (finder-, mode-, version- and timing patterns) |
||
612 | $start = $qz + 9; |
||
613 | // skip quiet zone |
||
614 | $end = $this->moduleCount - $qz; |
||
615 | |||
616 | // determine start coordinates |
||
617 | $startX = ($startX !== null ? $startX : ($length - $width) / 2) + $qz; |
||
618 | $startY = ($startY !== null ? $startY : ($length - $height) / 2) + $qz; |
||
619 | |||
620 | // clear the space |
||
621 | foreach($this->matrix as $y => $row){ |
||
622 | foreach($row as $x => $val){ |
||
623 | // out of bounds, skip |
||
624 | if($x < $start || $y < $start ||$x >= $end || $y >= $end){ |
||
625 | continue; |
||
626 | } |
||
627 | // a match |
||
628 | if($x >= $startX && $x < ($startX + $width) && $y >= $startY && $y < ($startY + $height)){ |
||
629 | $this->set($x, $y, false, $this::M_LOGO); |
||
630 | } |
||
631 | } |
||
632 | } |
||
633 | |||
634 | return $this; |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * Maps the binary $data array from QRDataInterface::maskECC() on the matrix, using $maskPattern |
||
639 | * |
||
640 | * @see \chillerlan\QRCode\Data\QRDataAbstract::maskECC() |
||
641 | * |
||
642 | * @param int[] $data |
||
643 | * @param int $maskPattern |
||
644 | * |
||
645 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
646 | */ |
||
647 | public function mapData(array $data, int $maskPattern):QRMatrix{ |
||
699 | |||
700 | /** |
||
701 | * ISO/IEC 18004:2000 Section 8.8.1 |
||
702 | * |
||
703 | * Note that some versions of the QR code standard have had errors in the section about mask patterns. |
||
704 | * The information below has been corrected. (https://www.thonky.com/qr-code-tutorial/mask-patterns) |
||
705 | * |
||
706 | * @see \chillerlan\QRCode\QRMatrix::mapData() |
||
707 | * |
||
708 | * @internal |
||
709 | * |
||
710 | * @param int $maskPattern |
||
711 | * |
||
712 | * @return \Closure |
||
713 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
714 | */ |
||
715 | protected function getMask(int $maskPattern):Closure{ |
||
732 | |||
733 | } |
||
734 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.