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