Total Complexity | 99 |
Total Lines | 557 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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 |
||
24 | final class QRMatrix{ |
||
25 | |||
26 | /** @var int */ |
||
27 | public const M_NULL = 0b000000000000; |
||
28 | /** @var int */ |
||
29 | public const M_DARKMODULE = 0b000000000001; |
||
30 | /** @var int */ |
||
31 | public const M_DATA = 0b000000000010; |
||
32 | /** @var int */ |
||
33 | public const M_FINDER = 0b000000000100; |
||
34 | /** @var int */ |
||
35 | public const M_SEPARATOR = 0b000000001000; |
||
36 | /** @var int */ |
||
37 | public const M_ALIGNMENT = 0b000000010000; |
||
38 | /** @var int */ |
||
39 | public const M_TIMING = 0b000000100000; |
||
40 | /** @var int */ |
||
41 | public const M_FORMAT = 0b000001000000; |
||
42 | /** @var int */ |
||
43 | public const M_VERSION = 0b000010000000; |
||
44 | /** @var int */ |
||
45 | public const M_QUIETZONE = 0b000100000000; |
||
46 | /** @var int */ |
||
47 | public const M_LOGO = 0b001000000000; |
||
48 | /** @var int */ |
||
49 | public const M_FINDER_DOT = 0b010000000000; |
||
50 | /** @var int */ |
||
51 | public const M_TEST = 0b011111111111; |
||
52 | /** @var int */ |
||
53 | public const IS_DARK = 0b100000000000; |
||
54 | |||
55 | /** |
||
56 | * the used mask pattern, set via QRMatrix::mask() |
||
57 | */ |
||
58 | private ?MaskPattern $maskPattern = null; |
||
59 | |||
60 | /** |
||
61 | * the size (side length) of the matrix, including quiet zone (if created) |
||
62 | */ |
||
63 | private int $moduleCount; |
||
64 | |||
65 | /** |
||
66 | * the actual matrix data array |
||
67 | * |
||
68 | * @var int[][] |
||
69 | */ |
||
70 | private array $matrix; |
||
71 | |||
72 | /** |
||
73 | * the current ECC level |
||
74 | */ |
||
75 | private EccLevel $eccLevel; |
||
76 | |||
77 | /** |
||
78 | * a Version instance |
||
79 | */ |
||
80 | private Version $version; |
||
81 | |||
82 | /** |
||
83 | * QRMatrix constructor. |
||
84 | */ |
||
85 | public function __construct(Version $version, EccLevel $eccLevel){ |
||
86 | $this->version = $version; |
||
87 | $this->eccLevel = $eccLevel; |
||
88 | $this->moduleCount = $this->version->getDimension(); |
||
89 | $this->matrix = array_fill(0, $this->moduleCount, array_fill(0, $this->moduleCount, $this::M_NULL)); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * shortcut to initialize the matrix |
||
94 | */ |
||
95 | public function init(MaskPattern $maskPattern, bool $test = null):self{ |
||
104 | ; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Returns the data matrix, returns a pure boolean representation if $boolean is set to true |
||
109 | * |
||
110 | * @return int[][]|bool[][] |
||
111 | */ |
||
112 | public function matrix(bool $boolean = false):array{ |
||
113 | |||
114 | if(!$boolean){ |
||
115 | return $this->matrix; |
||
116 | } |
||
117 | |||
118 | $matrix = []; |
||
119 | |||
120 | foreach($this->matrix as $y => $row){ |
||
121 | $matrix[$y] = []; |
||
122 | |||
123 | foreach($row as $x => $val){ |
||
124 | $matrix[$y][$x] = ($val & $this::IS_DARK) === $this::IS_DARK; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return $matrix; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Returns the current version number |
||
133 | */ |
||
134 | public function version():Version{ |
||
135 | return $this->version; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Returns the current ECC level |
||
140 | */ |
||
141 | public function eccLevel():EccLevel{ |
||
142 | return $this->eccLevel; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Returns the current mask pattern |
||
147 | */ |
||
148 | public function maskPattern():?MaskPattern{ |
||
149 | return $this->maskPattern; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Returns the absoulute size of the matrix, including quiet zone (after setting it). |
||
154 | * |
||
155 | * size = version * 4 + 17 [ + 2 * quietzone size] |
||
156 | */ |
||
157 | public function size():int{ |
||
158 | return $this->moduleCount; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Returns the value of the module at position [$x, $y] |
||
163 | */ |
||
164 | public function get(int $x, int $y):int{ |
||
165 | return $this->matrix[$y][$x]; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Sets the $M_TYPE value for the module at position [$x, $y] |
||
170 | * |
||
171 | * true => $M_TYPE | 0x800 |
||
172 | * false => $M_TYPE |
||
173 | */ |
||
174 | public function set(int $x, int $y, bool $value, int $M_TYPE):self{ |
||
175 | $this->matrix[$y][$x] = $M_TYPE | ($value ? $this::IS_DARK : 0); |
||
176 | |||
177 | return $this; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Flips the value of the module |
||
182 | */ |
||
183 | public function flip(int $x, int $y):self{ |
||
184 | $this->matrix[$y][$x] ^= $this::IS_DARK; |
||
185 | |||
186 | return $this; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Checks whether a module is of the given $M_TYPE |
||
191 | * |
||
192 | * true => $value & $M_TYPE === $M_TYPE |
||
193 | */ |
||
194 | public function checkType(int $x, int $y, int $M_TYPE):bool{ |
||
195 | return ($this->matrix[$y][$x] & $M_TYPE) === $M_TYPE; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Checks whether a module is true (dark) or false (light) |
||
200 | * |
||
201 | * true => $value & 0x800 === 0x800 |
||
202 | * false => $value & 0x800 === 0 |
||
203 | */ |
||
204 | public function check(int $x, int $y):bool{ |
||
205 | return $this->checkType($x, $y, $this::IS_DARK); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder |
||
210 | */ |
||
211 | public function setDarkModule():self{ |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Draws the 7x7 finder patterns in the corners top left/right and bottom left |
||
219 | * |
||
220 | * ISO/IEC 18004:2000 Section 7.3.2 |
||
221 | */ |
||
222 | public function setFinderPattern():self{ |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Draws the separator lines around the finder patterns |
||
254 | * |
||
255 | * ISO/IEC 18004:2000 Section 7.3.3 |
||
256 | */ |
||
257 | public function setSeparators():self{ |
||
279 | } |
||
280 | |||
281 | |||
282 | /** |
||
283 | * Draws the 5x5 alignment patterns |
||
284 | * |
||
285 | * ISO/IEC 18004:2000 Section 7.3.5 |
||
286 | */ |
||
287 | public function setAlignmentPattern():self{ |
||
288 | $alignmentPattern = $this->version->getAlignmentPattern(); |
||
289 | |||
290 | foreach($alignmentPattern as $y){ |
||
291 | foreach($alignmentPattern as $x){ |
||
292 | |||
293 | // skip existing patterns |
||
294 | if($this->matrix[$y][$x] !== $this::M_NULL){ |
||
295 | continue; |
||
296 | } |
||
297 | |||
298 | for($ry = -2; $ry <= 2; $ry++){ |
||
299 | for($rx = -2; $rx <= 2; $rx++){ |
||
300 | $v = ($ry === 0 && $rx === 0) || $ry === 2 || $ry === -2 || $rx === 2 || $rx === -2; |
||
301 | |||
302 | $this->set($x + $rx, $y + $ry, $v, $this::M_ALIGNMENT); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | } |
||
307 | } |
||
308 | |||
309 | return $this; |
||
310 | } |
||
311 | |||
312 | |||
313 | /** |
||
314 | * Draws the timing pattern (h/v checkered line between the finder patterns) |
||
315 | * |
||
316 | * ISO/IEC 18004:2000 Section 7.3.4 |
||
317 | */ |
||
318 | public function setTimingPattern():self{ |
||
319 | |||
320 | foreach(range(8, $this->moduleCount - 8 - 1) as $i){ |
||
321 | |||
322 | if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){ |
||
323 | continue; |
||
324 | } |
||
325 | |||
326 | $v = $i % 2 === 0; |
||
327 | |||
328 | $this->set($i, 6, $v, $this::M_TIMING); // h |
||
329 | $this->set(6, $i, $v, $this::M_TIMING); // v |
||
330 | } |
||
331 | |||
332 | return $this; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Draws the version information, 2x 3x6 pixel |
||
337 | * |
||
338 | * ISO/IEC 18004:2000 Section 8.10 |
||
339 | */ |
||
340 | public function setVersionNumber(bool $test = null):self{ |
||
341 | $bits = $this->version->getVersionPattern(); |
||
342 | |||
343 | if($bits !== null){ |
||
344 | |||
345 | for($i = 0; $i < 18; $i++){ |
||
346 | $a = (int)($i / 3); |
||
347 | $b = $i % 3 + $this->moduleCount - 8 - 3; |
||
348 | $v = !$test && (($bits >> $i) & 1) === 1; |
||
|
|||
349 | |||
350 | $this->set($b, $a, $v, $this::M_VERSION); // ne |
||
351 | $this->set($a, $b, $v, $this::M_VERSION); // sw |
||
352 | } |
||
353 | |||
354 | } |
||
355 | |||
356 | return $this; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Draws the format info along the finder patterns |
||
361 | * |
||
362 | * ISO/IEC 18004:2000 Section 8.9 |
||
363 | */ |
||
364 | public function setFormatInfo(MaskPattern $maskPattern, bool $test = null):self{ |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Draws the "quiet zone" of $size around the matrix |
||
399 | * |
||
400 | * ISO/IEC 18004:2000 Section 7.3.7 |
||
401 | * |
||
402 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
403 | */ |
||
404 | public function setQuietZone(int $size = null):self{ |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Clears a space of $width * $height in order to add a logo or text. |
||
435 | * |
||
436 | * Additionally, the logo space can be positioned within the QR Code - respecting the main functional patterns - |
||
437 | * using $startX and $startY. If either of these are null, the logo space will be centered in that direction. |
||
438 | * ECC level "H" (30%) is required. |
||
439 | * |
||
440 | * Please note that adding a logo space minimizes the error correction capacity of the QR Code and |
||
441 | * created images may become unreadable, especially when printed with a chance to receive damage. |
||
442 | * Please test thoroughly before using this feature in production. |
||
443 | * |
||
444 | * This method should be called from within an output module (after the matrix has been filled with data). |
||
445 | * Note that there is no restiction on how many times this method could be called on the same matrix instance. |
||
446 | * |
||
447 | * @link https://github.com/chillerlan/php-qrcode/issues/52 |
||
448 | * |
||
449 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
450 | */ |
||
451 | public function setLogoSpace(int $width, int $height, int $startX = null, int $startY = null):self{ |
||
452 | |||
453 | // for logos we operate in ECC H (30%) only |
||
454 | if($this->eccLevel->getLevel() !== EccLevel::H){ |
||
455 | throw new QRCodeDataException('ECC level "H" required to add logo space'); |
||
456 | } |
||
457 | |||
458 | // we need uneven sizes to center the logo space, adjust if needed |
||
459 | if($startX === null && ($width % 2) === 0){ |
||
460 | $width++; |
||
461 | } |
||
462 | |||
463 | if($startY === null && ($height % 2) === 0){ |
||
464 | $height++; |
||
465 | } |
||
466 | |||
467 | // $this->moduleCount includes the quiet zone (if created), we need the QR size here |
||
468 | $length = $this->version->getDimension(); |
||
469 | |||
470 | // throw if the logo space exceeds the maximum error correction capacity |
||
471 | if($width * $height > floor($length * $length * 0.2)){ |
||
472 | throw new QRCodeDataException('logo space exceeds the maximum error correction capacity'); |
||
473 | } |
||
474 | |||
475 | // quiet zone size |
||
476 | $qz = ($this->moduleCount - $length) / 2; |
||
477 | // skip quiet zone and the first 9 rows/columns (finder-, mode-, version- and timing patterns) |
||
478 | $start = $qz + 9; |
||
479 | // skip quiet zone |
||
480 | $end = $this->moduleCount - $qz; |
||
481 | |||
482 | // determine start coordinates |
||
483 | $startX = ($startX !== null ? $startX : ($length - $width) / 2) + $qz; |
||
484 | $startY = ($startY !== null ? $startY : ($length - $height) / 2) + $qz; |
||
485 | |||
486 | // clear the space |
||
487 | foreach($this->matrix as $y => $row){ |
||
488 | foreach($row as $x => $val){ |
||
489 | // out of bounds, skip |
||
490 | if($x < $start || $y < $start ||$x >= $end || $y >= $end){ |
||
491 | continue; |
||
492 | } |
||
493 | // a match |
||
494 | if($x >= $startX && $x < ($startX + $width) && $y >= $startY && $y < ($startY + $height)){ |
||
495 | $this->set($x, $y, false, $this::M_LOGO); |
||
496 | } |
||
497 | } |
||
498 | } |
||
499 | |||
500 | return $this; |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Maps the binary $data array from QRData::maskECC() on the matrix, |
||
505 | * masking the data using $maskPattern (ISO/IEC 18004:2000 Section 8.8) |
||
506 | * |
||
507 | * @see \chillerlan\QRCode\Data\QRData::maskECC() |
||
508 | * |
||
509 | * @param \SplFixedArray<int> $data |
||
510 | * |
||
511 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
512 | */ |
||
513 | public function mapData(SplFixedArray $data):self{ |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * Applies the mask pattern |
||
565 | * |
||
566 | * ISO/IEC 18004:2000 Section 8.8.1 |
||
567 | */ |
||
568 | public function mask(MaskPattern $maskPattern):self{ |
||
581 | } |
||
582 | |||
583 | } |
||
584 |
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.