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 |
||
| 20 | class QRMatrix{ |
||
| 21 | |||
| 22 | const M_NULL = 0x00; |
||
| 23 | const M_DARKMODULE = 0x02; |
||
| 24 | const M_DATA = 0x04; |
||
| 25 | const M_FINDER = 0x06; |
||
| 26 | const M_SEPARATOR = 0x08; |
||
| 27 | const M_ALIGNMENT = 0x0a; |
||
| 28 | const M_TIMING = 0x0c; |
||
| 29 | const M_FORMAT = 0x0e; |
||
| 30 | const M_VERSION = 0x10; |
||
| 31 | const M_QUIETZONE = 0x12; |
||
| 32 | const M_LOGO = 0x14; // @todo |
||
| 33 | |||
| 34 | const M_TEST = 0xff; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @link http://www.thonky.com/qr-code-tutorial/alignment-pattern-locations |
||
| 38 | */ |
||
| 39 | const alignmentPattern = [ 1 => // start at 1 |
||
| 40 | [], |
||
| 41 | [6, 18], |
||
| 42 | [6, 22], |
||
| 43 | [6, 26], |
||
| 44 | [6, 30], |
||
| 45 | [6, 34], |
||
| 46 | [6, 22, 38], |
||
| 47 | [6, 24, 42], |
||
| 48 | [6, 26, 46], |
||
| 49 | [6, 28, 50], |
||
| 50 | [6, 30, 54], |
||
| 51 | [6, 32, 58], |
||
| 52 | [6, 34, 62], |
||
| 53 | [6, 26, 46, 66], |
||
| 54 | [6, 26, 48, 70], |
||
| 55 | [6, 26, 50, 74], |
||
| 56 | [6, 30, 54, 78], |
||
| 57 | [6, 30, 56, 82], |
||
| 58 | [6, 30, 58, 86], |
||
| 59 | [6, 34, 62, 90], |
||
| 60 | [6, 28, 50, 72, 94], |
||
| 61 | [6, 26, 50, 74, 98], |
||
| 62 | [6, 30, 54, 78, 102], |
||
| 63 | [6, 28, 54, 80, 106], |
||
| 64 | [6, 32, 58, 84, 110], |
||
| 65 | [6, 30, 58, 86, 114], |
||
| 66 | [6, 34, 62, 90, 118], |
||
| 67 | [6, 26, 50, 74, 98, 122], |
||
| 68 | [6, 30, 54, 78, 102, 126], |
||
| 69 | [6, 26, 52, 78, 104, 130], |
||
| 70 | [6, 30, 56, 82, 108, 134], |
||
| 71 | [6, 34, 60, 86, 112, 138], |
||
| 72 | [6, 30, 58, 86, 114, 142], |
||
| 73 | [6, 34, 62, 90, 118, 146], |
||
| 74 | [6, 30, 54, 78, 102, 126, 150], |
||
| 75 | [6, 24, 50, 76, 102, 128, 154], |
||
| 76 | [6, 28, 54, 80, 106, 132, 158], |
||
| 77 | [6, 32, 58, 84, 110, 136, 162], |
||
| 78 | [6, 26, 54, 82, 110, 138, 166], |
||
| 79 | [6, 30, 58, 86, 114, 142, 170], |
||
| 80 | ]; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @link http://www.thonky.com/qr-code-tutorial/format-version-tables |
||
| 84 | */ |
||
| 85 | const versionPattern = [ 7 => // no version pattern for QR Codes < 7 |
||
| 86 | 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, // 7-10 |
||
| 87 | 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d, 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, |
||
| 88 | 0x15683, 0x168c9, 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75, |
||
| 89 | 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64, 0x27541, 0x28c69, |
||
| 90 | ]; |
||
| 91 | |||
| 92 | const formatPattern = [ |
||
| 93 | [0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976], // L |
||
| 94 | [0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0], // M |
||
| 95 | [0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed], // Q |
||
| 96 | [0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b], // H |
||
| 97 | ]; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var int |
||
| 101 | */ |
||
| 102 | protected $version; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var int |
||
| 106 | */ |
||
| 107 | protected $eclevel; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var int |
||
| 111 | */ |
||
| 112 | protected $maskPattern = -1; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var int |
||
| 116 | */ |
||
| 117 | protected $moduleCount; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var mixed[] |
||
| 121 | */ |
||
| 122 | protected $matrix; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * QRMatrix constructor. |
||
| 126 | * |
||
| 127 | * @param int $version |
||
| 128 | * @param int $eclevel |
||
| 129 | * |
||
| 130 | * @throws \chillerlan\QRCode\Data\QRCodeDataException |
||
| 131 | */ |
||
| 132 | public function __construct(int $version, int $eclevel){ |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns the data array |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function matrix():array { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return int |
||
| 159 | */ |
||
| 160 | public function version():int { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return int |
||
| 166 | */ |
||
| 167 | public function eccLevel():int { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return int |
||
| 173 | */ |
||
| 174 | public function maskPattern():int { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Returns the absoulute size of the matrix, including quiet zone (after setting it). |
||
| 180 | * |
||
| 181 | * size = version * 4 + 17 [ + 2 * quietzone size] |
||
| 182 | * |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | public function size():int{ |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the value of the module at position [$x, $y] |
||
| 191 | * |
||
| 192 | * @param int $x |
||
| 193 | * @param int $y |
||
| 194 | * |
||
| 195 | * @return int |
||
| 196 | */ |
||
| 197 | public function get(int $x, int $y):int{ |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Sets the $M_TYPE value for the module at position [$x, $y] |
||
| 203 | * |
||
| 204 | * true => $M_TYPE << 8 |
||
| 205 | * false => $M_TYPE |
||
| 206 | * |
||
| 207 | * @param int $x |
||
| 208 | * @param int $y |
||
| 209 | * @param int $M_TYPE |
||
| 210 | * @param bool $value |
||
| 211 | * |
||
| 212 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
| 213 | */ |
||
| 214 | public function set(int $x, int $y, bool $value, int $M_TYPE):QRMatrix{ |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Checks whether a module is true (dark) or false (light) |
||
| 222 | * |
||
| 223 | * true => $value >> 8 === $M_TYPE |
||
| 224 | * $value >> 8 > 0 |
||
| 225 | * |
||
| 226 | * false => $value === $M_TYPE |
||
| 227 | * $value >> 8 === 0 |
||
| 228 | * |
||
| 229 | * @param int $x |
||
| 230 | * @param int $y |
||
| 231 | * |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | public function check(int $x, int $y):bool{ |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder |
||
| 241 | * |
||
| 242 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
| 243 | */ |
||
| 244 | public function setDarkModule():QRMatrix{ |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Draws the 7x7 finder patterns in the corners top left/right and bottom left |
||
| 252 | * |
||
| 253 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
| 254 | */ |
||
| 255 | public function setFinderPattern():QRMatrix{ |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Draws the separator lines around the finder patterns |
||
| 281 | * |
||
| 282 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
| 283 | */ |
||
| 284 | public function setSeparators():QRMatrix{ |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Draws the 5x5 alignment patterns |
||
| 313 | * |
||
| 314 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
| 315 | */ |
||
| 316 | public function setAlignmentPattern():QRMatrix{ |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * Draws the timing pattern (h/v checkered line between the finder patterns) |
||
| 344 | * |
||
| 345 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
| 346 | */ |
||
| 347 | public function setTimingPattern():QRMatrix{ |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Draws the version information, 2x 3x6 pixel |
||
| 367 | * |
||
| 368 | * @param bool|null $test |
||
| 369 | * |
||
| 370 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
| 371 | */ |
||
| 372 | public function setVersionNumber(bool $test = null):QRMatrix{ |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Draws the format info along the finder patterns |
||
| 396 | * |
||
| 397 | * @param int $maskPattern |
||
| 398 | * @param bool|null $test |
||
| 399 | * |
||
| 400 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
| 401 | */ |
||
| 402 | public function setFormatInfo(int $maskPattern, bool $test = null):QRMatrix{ |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Draws the "quiet zone" of $size around the matrix |
||
| 439 | * |
||
| 440 | * @param int|null $size |
||
| 441 | * |
||
| 442 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
| 443 | */ |
||
| 444 | public function setQuietZone(int $size = null):QRMatrix{ |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Maps the binary $data array from QRDataInterface::maskECC() on the matrix, using $maskPattern |
||
| 473 | * |
||
| 474 | * @see \chillerlan\QRCode\Data\QRDataAbstract::maskECC() |
||
| 475 | * |
||
| 476 | * @param int[] $data |
||
| 477 | * @param int $maskPattern |
||
| 478 | * |
||
| 479 | * @return \chillerlan\QRCode\Data\QRMatrix |
||
| 480 | */ |
||
| 481 | public function mapData(array $data, int $maskPattern):QRMatrix{ |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @see \chillerlan\QRCode\QRMatrix::mapData() |
||
| 535 | * |
||
| 536 | * @internal |
||
| 537 | * |
||
| 538 | * @param int $x |
||
| 539 | * @param int $y |
||
| 540 | * @param int $maskPattern |
||
| 541 | * |
||
| 542 | * @return int |
||
| 543 | */ |
||
| 544 | protected function getMask(int $x, int $y, int $maskPattern):int { |
||
| 562 | |||
| 563 | } |
||
| 564 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.