Total Complexity | 109 |
Total Lines | 592 |
Duplicated Lines | 0 % |
Changes | 19 | ||
Bugs | 0 | Features | 1 |
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{ |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Returns the current version number |
||
132 | */ |
||
133 | public function version():?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{ |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Returns the value of the module at position [$x, $y] or -1 if the coordinate is outside of the matrix |
||
162 | */ |
||
163 | public function get(int $x, int $y):int{ |
||
164 | |||
165 | if(!isset($this->matrix[$y][$x])){ |
||
166 | return -1; |
||
167 | } |
||
168 | |||
169 | return $this->matrix[$y][$x]; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Sets the $M_TYPE value for the module at position [$x, $y] |
||
174 | * |
||
175 | * true => $M_TYPE | 0x800 |
||
176 | * false => $M_TYPE |
||
177 | */ |
||
178 | public function set(int $x, int $y, bool $value, int $M_TYPE):self{ |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Flips the value of the module |
||
189 | */ |
||
190 | public function flip(int $x, int $y):self{ |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Checks whether a module is of the given $M_TYPE |
||
201 | * |
||
202 | * true => $value & $M_TYPE === $M_TYPE |
||
203 | */ |
||
204 | public function checkType(int $x, int $y, int $M_TYPE):bool{ |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * checks whether the module at ($x, $y) is in the given array of $M_TYPES, |
||
215 | * returns true if no matches are found, otherwise false. |
||
216 | */ |
||
217 | public function checkTypeIn(int $x, int $y, array $M_TYPES):bool{ |
||
218 | |||
219 | return !$this->checkTypeNotIn($x, $y, $M_TYPES); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * checks whether the module at ($x, $y) is not in the given array of $M_TYPES, |
||
224 | * returns true if no matches are found, otherwise false. |
||
225 | */ |
||
226 | public function checkTypeNotIn(int $x, int $y, array $M_TYPES):bool{ |
||
227 | |||
228 | foreach($M_TYPES as $type){ |
||
229 | if($this->checkType($x, $y, $type)){ |
||
230 | return false; |
||
231 | } |
||
232 | } |
||
233 | |||
234 | return true; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Checks whether a module is true (dark) or false (light) |
||
239 | * |
||
240 | * true => $value & 0x800 === 0x800 |
||
241 | * false => $value & 0x800 === 0 |
||
242 | */ |
||
243 | public function check(int $x, int $y):bool{ |
||
244 | return $this->checkType($x, $y, $this::IS_DARK); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder |
||
249 | * |
||
250 | * 4 * version + 9 or moduleCount - 8 |
||
251 | */ |
||
252 | public function setDarkModule():self{ |
||
253 | $this->set(8, $this->moduleCount - 8, true, $this::M_DARKMODULE); |
||
254 | |||
255 | return $this; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Draws the 7x7 finder patterns in the corners top left/right and bottom left |
||
260 | * |
||
261 | * ISO/IEC 18004:2000 Section 7.3.2 |
||
262 | */ |
||
263 | public function setFinderPattern():self{ |
||
264 | |||
265 | $pos = [ |
||
266 | [0, 0], // top left |
||
267 | [$this->moduleCount - 7, 0], // bottom left |
||
268 | [0, $this->moduleCount - 7], // top right |
||
269 | ]; |
||
270 | |||
271 | foreach($pos as $c){ |
||
272 | for($y = 0; $y < 7; $y++){ |
||
273 | for($x = 0; $x < 7; $x++){ |
||
274 | // outer (dark) 7*7 square |
||
275 | if($x === 0 || $x === 6 || $y === 0 || $y === 6){ |
||
276 | $this->set($c[0] + $y, $c[1] + $x, true, $this::M_FINDER); |
||
277 | } |
||
278 | // inner (light) 5*5 square |
||
279 | elseif($x === 1 || $x === 5 || $y === 1 || $y === 5){ |
||
280 | $this->set($c[0] + $y, $c[1] + $x, false, $this::M_FINDER); |
||
281 | } |
||
282 | // 3*3 dot |
||
283 | else{ |
||
284 | $this->set($c[0] + $y, $c[1] + $x, true, $this::M_FINDER_DOT); |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | |||
290 | return $this; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Draws the separator lines around the finder patterns |
||
295 | * |
||
296 | * ISO/IEC 18004:2000 Section 7.3.3 |
||
297 | */ |
||
298 | public function setSeparators():self{ |
||
320 | } |
||
321 | |||
322 | |||
323 | /** |
||
324 | * Draws the 5x5 alignment patterns |
||
325 | * |
||
326 | * ISO/IEC 18004:2000 Section 7.3.5 |
||
327 | */ |
||
328 | public function setAlignmentPattern():self{ |
||
329 | $alignmentPattern = $this->version->getAlignmentPattern(); |
||
|
|||
330 | |||
331 | foreach($alignmentPattern as $y){ |
||
332 | foreach($alignmentPattern as $x){ |
||
333 | |||
334 | // skip existing patterns |
||
335 | if($this->matrix[$y][$x] !== $this::M_NULL){ |
||
336 | continue; |
||
337 | } |
||
338 | |||
339 | for($ry = -2; $ry <= 2; $ry++){ |
||
340 | for($rx = -2; $rx <= 2; $rx++){ |
||
341 | $v = ($ry === 0 && $rx === 0) || $ry === 2 || $ry === -2 || $rx === 2 || $rx === -2; |
||
342 | |||
343 | $this->set($x + $rx, $y + $ry, $v, $this::M_ALIGNMENT); |
||
344 | } |
||
345 | } |
||
346 | |||
347 | } |
||
348 | } |
||
349 | |||
350 | return $this; |
||
351 | } |
||
352 | |||
353 | |||
354 | /** |
||
355 | * Draws the timing pattern (h/v checkered line between the finder patterns) |
||
356 | * |
||
357 | * ISO/IEC 18004:2000 Section 7.3.4 |
||
358 | */ |
||
359 | public function setTimingPattern():self{ |
||
360 | |||
361 | foreach(range(8, $this->moduleCount - 8 - 1) as $i){ |
||
362 | |||
363 | if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){ |
||
364 | continue; |
||
365 | } |
||
366 | |||
367 | $v = $i % 2 === 0; |
||
368 | |||
369 | $this->set($i, 6, $v, $this::M_TIMING); // h |
||
370 | $this->set(6, $i, $v, $this::M_TIMING); // v |
||
371 | } |
||
372 | |||
373 | return $this; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Draws the version information, 2x 3x6 pixel |
||
378 | * |
||
379 | * ISO/IEC 18004:2000 Section 8.10 |
||
380 | */ |
||
381 | public function setVersionNumber():self{ |
||
382 | $bits = $this->version->getVersionPattern(); |
||
383 | |||
384 | if($bits !== null){ |
||
385 | |||
386 | for($i = 0; $i < 18; $i++){ |
||
387 | $a = (int)($i / 3); |
||
388 | $b = $i % 3 + $this->moduleCount - 8 - 3; |
||
389 | $v = (($bits >> $i) & 1) === 1; |
||
390 | |||
391 | $this->set($b, $a, $v, $this::M_VERSION); // ne |
||
392 | $this->set($a, $b, $v, $this::M_VERSION); // sw |
||
393 | } |
||
394 | |||
395 | } |
||
396 | |||
397 | return $this; |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Draws the format info along the finder patterns |
||
402 | * |
||
403 | * ISO/IEC 18004:2000 Section 8.9 |
||
404 | */ |
||
405 | public function setFormatInfo():self{ |
||
406 | $bits = $this->eccLevel->getformatPattern($this->maskPattern); |
||
407 | |||
408 | for($i = 0; $i < 15; $i++){ |
||
409 | $v = (($bits >> $i) & 1) === 1; |
||
410 | |||
411 | if($i < 6){ |
||
412 | $this->set(8, $i, $v, $this::M_FORMAT); |
||
413 | } |
||
414 | elseif($i < 8){ |
||
415 | $this->set(8, $i + 1, $v, $this::M_FORMAT); |
||
416 | } |
||
417 | else{ |
||
418 | $this->set(8, $this->moduleCount - 15 + $i, $v, $this::M_FORMAT); |
||
419 | } |
||
420 | |||
421 | if($i < 8){ |
||
422 | $this->set($this->moduleCount - $i - 1, 8, $v, $this::M_FORMAT); |
||
423 | } |
||
424 | elseif($i < 9){ |
||
425 | $this->set(15 - $i, 8, $v, $this::M_FORMAT); |
||
426 | } |
||
427 | else{ |
||
428 | $this->set(15 - $i - 1, 8, $v, $this::M_FORMAT); |
||
429 | } |
||
430 | |||
431 | } |
||
432 | |||
433 | return $this; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Draws the "quiet zone" of $size around the matrix |
||
438 | * |
||
439 | * ISO/IEC 18004:2000 Section 7.3.7 |
||
440 | * |
||
441 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
442 | */ |
||
443 | public function setQuietZone(int $size = null):self{ |
||
444 | |||
445 | if($this->matrix[$this->moduleCount - 1][$this->moduleCount - 1] === $this::M_NULL){ |
||
446 | throw new QRCodeDataException('use only after writing data'); |
||
447 | } |
||
448 | |||
449 | $size = $size !== null |
||
450 | ? max(0, min($size, floor($this->moduleCount / 2))) |
||
451 | : 4; |
||
452 | |||
453 | for($y = 0; $y < $this->moduleCount; $y++){ |
||
454 | for($i = 0; $i < $size; $i++){ |
||
455 | array_unshift($this->matrix[$y], $this::M_QUIETZONE); |
||
456 | $this->matrix[$y][] = $this::M_QUIETZONE; |
||
457 | } |
||
458 | } |
||
459 | |||
460 | $this->moduleCount += ($size * 2); |
||
461 | |||
462 | $r = array_fill(0, $this->moduleCount, $this::M_QUIETZONE); |
||
463 | |||
464 | for($i = 0; $i < $size; $i++){ |
||
465 | array_unshift($this->matrix, $r); |
||
466 | $this->matrix[] = $r; |
||
467 | } |
||
468 | |||
469 | return $this; |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Clears a space of $width * $height in order to add a logo or text. |
||
474 | * |
||
475 | * Additionally, the logo space can be positioned within the QR Code - respecting the main functional patterns - |
||
476 | * using $startX and $startY. If either of these are null, the logo space will be centered in that direction. |
||
477 | * ECC level "H" (30%) is required. |
||
478 | * |
||
479 | * Please note that adding a logo space minimizes the error correction capacity of the QR Code and |
||
480 | * created images may become unreadable, especially when printed with a chance to receive damage. |
||
481 | * Please test thoroughly before using this feature in production. |
||
482 | * |
||
483 | * This method should be called from within an output module (after the matrix has been filled with data). |
||
484 | * Note that there is no restiction on how many times this method could be called on the same matrix instance. |
||
485 | * |
||
486 | * @link https://github.com/chillerlan/php-qrcode/issues/52 |
||
487 | * |
||
488 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
489 | */ |
||
490 | public function setLogoSpace(int $width, int $height, int $startX = null, int $startY = null):self{ |
||
491 | |||
492 | // for logos we operate in ECC H (30%) only |
||
493 | if($this->eccLevel->getLevel() !== EccLevel::H){ |
||
494 | throw new QRCodeDataException('ECC level "H" required to add logo space'); |
||
495 | } |
||
496 | |||
497 | // if width and height happen to be exactly 0 (default value), just return - nothing to do |
||
498 | if($width === 0 || $height === 0){ |
||
499 | return $this; |
||
500 | } |
||
501 | |||
502 | // $this->moduleCount includes the quiet zone (if created), we need the QR size here |
||
503 | $length = $this->version->getDimension(); |
||
504 | |||
505 | // throw if the size is negative or exceeds the qrcode size |
||
506 | if($width < 0 || $height < 0 || $width > $length || $height > $length){ |
||
507 | throw new QRCodeDataException('invalid logo dimensions'); |
||
508 | } |
||
509 | |||
510 | // we need uneven sizes to center the logo space, adjust if needed |
||
511 | if($startX === null && ($width % 2) === 0){ |
||
512 | $width++; |
||
513 | } |
||
514 | |||
515 | if($startY === null && ($height % 2) === 0){ |
||
516 | $height++; |
||
517 | } |
||
518 | |||
519 | // throw if the logo space exceeds the maximum error correction capacity |
||
520 | if($width * $height > floor($length * $length * 0.2)){ |
||
521 | throw new QRCodeDataException('logo space exceeds the maximum error correction capacity'); |
||
522 | } |
||
523 | |||
524 | // quiet zone size |
||
525 | $qz = ($this->moduleCount - $length) / 2; |
||
526 | // skip quiet zone and the first 9 rows/columns (finder-, mode-, version- and timing patterns) |
||
527 | $start = $qz + 9; |
||
528 | // skip quiet zone |
||
529 | $end = $this->moduleCount - $qz; |
||
530 | |||
531 | // determine start coordinates |
||
532 | $startX = ($startX !== null ? $startX : ($length - $width) / 2) + $qz; |
||
533 | $startY = ($startY !== null ? $startY : ($length - $height) / 2) + $qz; |
||
534 | |||
535 | // clear the space |
||
536 | foreach($this->matrix as $y => $row){ |
||
537 | foreach($row as $x => $val){ |
||
538 | // out of bounds, skip |
||
539 | if($x < $start || $y < $start ||$x >= $end || $y >= $end){ |
||
540 | continue; |
||
541 | } |
||
542 | // a match |
||
543 | if($x >= $startX && $x < ($startX + $width) && $y >= $startY && $y < ($startY + $height)){ |
||
544 | $this->set($x, $y, false, $this::M_LOGO); |
||
545 | } |
||
546 | } |
||
547 | } |
||
548 | |||
549 | return $this; |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * Maps the interleaved binary $data on the matrix |
||
554 | */ |
||
555 | public function writeCodewords(BitBuffer $bitBuffer):self{ |
||
595 | } |
||
596 | |||
597 | /** |
||
598 | * Applies/reverses the mask pattern |
||
599 | * |
||
600 | * ISO/IEC 18004:2000 Section 8.8.1 |
||
601 | */ |
||
602 | public function mask():self{ |
||
614 | } |
||
615 | |||
616 | } |
||
617 |
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.