| Total Complexity | 71 |
| Total Lines | 324 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like qrmask 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 qrmask, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class qrmask |
||
| 34 | { |
||
| 35 | public $runLength = []; |
||
| 36 | |||
| 37 | //---------------------------------------------------------------------- |
||
| 38 | public function __construct() |
||
| 39 | { |
||
| 40 | $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0); |
||
| 41 | } |
||
| 42 | |||
| 43 | //---------------------------------------------------------------------- |
||
| 44 | public function writeFormatInformation($width, &$frame, $mask, $level) |
||
| 45 | { |
||
| 46 | $blacks = 0; |
||
| 47 | $format = QRspec::getFormatInfo($mask, $level); |
||
| 48 | |||
| 49 | for ($i = 0; $i < 8; $i++) { |
||
| 50 | if ($format & 1) { |
||
| 51 | $blacks += 2; |
||
| 52 | $v = 0x85; |
||
| 53 | } else { |
||
| 54 | $v = 0x84; |
||
| 55 | } |
||
| 56 | |||
| 57 | $frame[8][$width - 1 - $i] = chr($v); |
||
| 58 | if ($i < 6) { |
||
| 59 | $frame[$i][8] = chr($v); |
||
| 60 | } else { |
||
| 61 | $frame[$i + 1][8] = chr($v); |
||
| 62 | } |
||
| 63 | $format = $format >> 1; |
||
| 64 | } |
||
| 65 | |||
| 66 | for ($i = 0; $i < 7; $i++) { |
||
| 67 | if ($format & 1) { |
||
| 68 | $blacks += 2; |
||
| 69 | $v = 0x85; |
||
| 70 | } else { |
||
| 71 | $v = 0x84; |
||
| 72 | } |
||
| 73 | |||
| 74 | $frame[$width - 7 + $i][8] = chr($v); |
||
| 75 | if ($i == 0) { |
||
| 76 | $frame[8][7] = chr($v); |
||
| 77 | } else { |
||
| 78 | $frame[8][6 - $i] = chr($v); |
||
| 79 | } |
||
| 80 | |||
| 81 | $format = $format >> 1; |
||
| 82 | } |
||
| 83 | |||
| 84 | return $blacks; |
||
| 85 | } |
||
| 86 | |||
| 87 | //---------------------------------------------------------------------- |
||
| 88 | public function mask0($x, $y) |
||
| 89 | { |
||
| 90 | return ($x + $y) & 1; |
||
| 91 | } |
||
| 92 | |||
| 93 | public function mask1($x, $y) |
||
| 94 | { |
||
| 95 | return $y & 1; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function mask2($x, $y) |
||
| 99 | { |
||
| 100 | return $x % 3; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function mask3($x, $y) |
||
| 104 | { |
||
| 105 | return ($x + $y) % 3; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function mask4($x, $y) |
||
| 109 | { |
||
| 110 | return (((int) ($y / 2)) + ((int) ($x / 3))) & 1; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function mask5($x, $y) |
||
| 114 | { |
||
| 115 | return (($x * $y) & 1) + ($x * $y) % 3; |
||
| 116 | } |
||
| 117 | |||
| 118 | public function mask6($x, $y) |
||
| 119 | { |
||
| 120 | return ((($x * $y) & 1) + ($x * $y) % 3) & 1; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function mask7($x, $y) |
||
| 124 | { |
||
| 125 | return ((($x * $y) % 3) + (($x + $y) & 1)) & 1; |
||
| 126 | } |
||
| 127 | |||
| 128 | //---------------------------------------------------------------------- |
||
| 129 | private function generateMaskNo($maskNo, $width, $frame) |
||
| 130 | { |
||
| 131 | $bitMask = array_fill(0, $width, array_fill(0, $width, 0)); |
||
| 132 | |||
| 133 | for ($y = 0; $y < $width; $y++) { |
||
| 134 | for ($x = 0; $x < $width; $x++) { |
||
| 135 | if (ord($frame[$y][$x]) & 0x80) { |
||
| 136 | $bitMask[$y][$x] = 0; |
||
| 137 | } else { |
||
| 138 | $maskFunc = call_user_func([$this, 'mask'.$maskNo], $x, $y); |
||
| 139 | $bitMask[$y][$x] = ($maskFunc == 0) ? 1 : 0; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | return $bitMask; |
||
| 145 | } |
||
| 146 | |||
| 147 | //---------------------------------------------------------------------- |
||
| 148 | public static function serial($bitFrame) |
||
| 149 | { |
||
| 150 | $codeArr = []; |
||
| 151 | |||
| 152 | foreach ($bitFrame as $line) { |
||
| 153 | $codeArr[] = implode('', $line); |
||
| 154 | } |
||
| 155 | |||
| 156 | return gzcompress(implode("\n", $codeArr), 9); |
||
| 157 | } |
||
| 158 | |||
| 159 | //---------------------------------------------------------------------- |
||
| 160 | public static function unserial($code) |
||
| 161 | { |
||
| 162 | $codeArr = []; |
||
| 163 | |||
| 164 | $codeLines = explode("\n", gzuncompress($code)); |
||
| 165 | foreach ($codeLines as $line) { |
||
| 166 | $codeArr[] = str_split($line); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $codeArr; |
||
| 170 | } |
||
| 171 | |||
| 172 | //---------------------------------------------------------------------- |
||
| 173 | public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false) |
||
| 174 | { |
||
| 175 | $b = 0; |
||
| 176 | $bitMask = []; |
||
| 177 | |||
| 178 | $fileName = QR_CACHE_DIR.'mask_'.$maskNo.DIRECTORY_SEPARATOR.'mask_'.$width.'_'.$maskNo.'.dat'; |
||
| 179 | |||
| 180 | if (QR_CACHEABLE) { |
||
| 181 | if (file_exists($fileName)) { |
||
| 182 | $bitMask = self::unserial(file_get_contents($fileName)); |
||
| 183 | } else { |
||
| 184 | $bitMask = $this->generateMaskNo($maskNo, $width, $s); |
||
| 185 | if (!file_exists(QR_CACHE_DIR.'mask_'.$maskNo)) { |
||
| 186 | mkdir(QR_CACHE_DIR.'mask_'.$maskNo); |
||
| 187 | } |
||
| 188 | file_put_contents($fileName, self::serial($bitMask)); |
||
| 189 | } |
||
| 190 | } else { |
||
| 191 | $bitMask = $this->generateMaskNo($maskNo, $width, $s); |
||
| 192 | } |
||
| 193 | |||
| 194 | if ($maskGenOnly) { |
||
| 195 | return; |
||
| 196 | } |
||
| 197 | |||
| 198 | $d = $s; |
||
| 199 | |||
| 200 | for ($y = 0; $y < $width; $y++) { |
||
| 201 | for ($x = 0; $x < $width; $x++) { |
||
| 202 | if ($bitMask[$y][$x] == 1) { |
||
| 203 | $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int) $bitMask[$y][$x]); |
||
| 204 | } |
||
| 205 | $b += (int) (ord($d[$y][$x]) & 1); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | return $b; |
||
| 210 | } |
||
| 211 | |||
| 212 | //---------------------------------------------------------------------- |
||
| 213 | public function makeMask($width, $frame, $maskNo, $level) |
||
| 214 | { |
||
| 215 | $masked = array_fill(0, $width, str_repeat("\0", $width)); |
||
| 216 | $this->makeMaskNo($maskNo, $width, $frame, $masked); |
||
| 217 | $this->writeFormatInformation($width, $masked, $maskNo, $level); |
||
| 218 | |||
| 219 | return $masked; |
||
| 220 | } |
||
| 221 | |||
| 222 | //---------------------------------------------------------------------- |
||
| 223 | public function calcN1N3($length) |
||
| 224 | { |
||
| 225 | $demerit = 0; |
||
| 226 | |||
| 227 | for ($i = 0; $i < $length; $i++) { |
||
| 228 | if ($this->runLength[$i] >= 5) { |
||
| 229 | $demerit += (N1 + ($this->runLength[$i] - 5)); |
||
| 230 | } |
||
| 231 | if ($i & 1) { |
||
| 232 | if (($i >= 3) && ($i < ($length - 2)) && ($this->runLength[$i] % 3 == 0)) { |
||
| 233 | $fact = (int) ($this->runLength[$i] / 3); |
||
| 234 | if (($this->runLength[$i - 2] == $fact) && |
||
| 235 | ($this->runLength[$i - 1] == $fact) && |
||
| 236 | ($this->runLength[$i + 1] == $fact) && |
||
| 237 | ($this->runLength[$i + 2] == $fact)) { |
||
| 238 | if (($this->runLength[$i - 3] < 0) || ($this->runLength[$i - 3] >= (4 * $fact))) { |
||
| 239 | $demerit += N3; |
||
| 240 | } elseif ((($i + 3) >= $length) || ($this->runLength[$i + 3] >= (4 * $fact))) { |
||
| 241 | $demerit += N3; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | return $demerit; |
||
| 249 | } |
||
| 250 | |||
| 251 | //---------------------------------------------------------------------- |
||
| 252 | public function evaluateSymbol($width, $frame) |
||
| 316 | } |
||
| 317 | |||
| 318 | //---------------------------------------------------------------------- |
||
| 319 | public function mask($width, $frame, $level) |
||
| 320 | { |
||
| 321 | $minDemerit = PHP_INT_MAX; |
||
| 322 | $bestMaskNum = 0; |
||
| 323 | $bestMask = []; |
||
| 324 | |||
| 325 | $checked_masks = [0, 1, 2, 3, 4, 5, 6, 7]; |
||
| 326 | |||
| 357 | } |
||
| 358 | |||
| 359 | //---------------------------------------------------------------------- |
||
| 360 | } |
||
| 361 |