| Total Complexity | 62 |
| Total Lines | 271 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MaskPattern 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 MaskPattern, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | final class MaskPattern{ |
||
| 25 | |||
| 26 | public const PATTERN_000 = 0b000; |
||
| 27 | public const PATTERN_001 = 0b001; |
||
| 28 | public const PATTERN_010 = 0b010; |
||
| 29 | public const PATTERN_011 = 0b011; |
||
| 30 | public const PATTERN_100 = 0b100; |
||
| 31 | public const PATTERN_101 = 0b101; |
||
| 32 | public const PATTERN_110 = 0b110; |
||
| 33 | public const PATTERN_111 = 0b111; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int[] |
||
| 37 | */ |
||
| 38 | public const PATTERNS = [ |
||
| 39 | self::PATTERN_000, |
||
| 40 | self::PATTERN_001, |
||
| 41 | self::PATTERN_010, |
||
| 42 | self::PATTERN_011, |
||
| 43 | self::PATTERN_100, |
||
| 44 | self::PATTERN_101, |
||
| 45 | self::PATTERN_110, |
||
| 46 | self::PATTERN_111, |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The current mask pattern value (0-7) |
||
| 51 | */ |
||
| 52 | private int $maskPattern; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * MaskPattern constructor. |
||
| 56 | * |
||
| 57 | * @throws \chillerlan\QRCode\QRCodeException |
||
| 58 | */ |
||
| 59 | public function __construct(int $maskPattern){ |
||
| 60 | |||
| 61 | if((0b111 & $maskPattern) !== $maskPattern){ |
||
| 62 | throw new QRCodeException('invalid mask pattern'); |
||
| 63 | } |
||
| 64 | |||
| 65 | $this->maskPattern = $maskPattern; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Returns the current mask pattern |
||
| 70 | */ |
||
| 71 | public function getPattern():int{ |
||
| 72 | return $this->maskPattern; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns a closure that applies the mask for the chosen mask pattern. |
||
| 77 | * |
||
| 78 | * Encapsulates data masks for the data bits in a QR code, per ISO 18004:2006 6.8. Implementations |
||
| 79 | * of this class can un-mask a raw BitMatrix. For simplicity, they will unmask the entire BitMatrix, |
||
| 80 | * including areas used for finder patterns, timing patterns, etc. These areas should be unused |
||
| 81 | * after the point they are unmasked anyway. |
||
| 82 | * |
||
| 83 | * Note that the diagram in section 6.8.1 is misleading since it indicates that i is column position |
||
| 84 | * and j is row position. In fact, as the text says, i is row position and j is column position. |
||
| 85 | * |
||
| 86 | * @see https://www.thonky.com/qr-code-tutorial/mask-patterns |
||
| 87 | * @see https://github.com/zxing/zxing/blob/e9e2bd280bcaeabd59d0f955798384fe6c018a6c/core/src/main/java/com/google/zxing/qrcode/decoder/DataMask.java#L32-L117 |
||
| 88 | */ |
||
| 89 | public function getMask():Closure{ |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Evaluates the matrix of the given data interface and returns a new mask pattern instance for the best result |
||
| 105 | */ |
||
| 106 | public static function getBestPattern(QRData $dataInterface):self{ |
||
| 107 | $penalties = []; |
||
| 108 | |||
| 109 | foreach(self::PATTERNS as $pattern){ |
||
| 110 | $matrix = $dataInterface->writeMatrix(new self($pattern))->matrix(true); |
||
| 111 | $penalty = 0; |
||
| 112 | |||
| 113 | for($level = 1; $level <= 4; $level++){ |
||
| 114 | $penalty += self::{'testRule'.$level}($matrix, count($matrix), count($matrix[0])); |
||
| 115 | } |
||
| 116 | |||
| 117 | $penalties[$pattern] = (int)$penalty; |
||
| 118 | } |
||
| 119 | |||
| 120 | return new self(array_search(min($penalties), $penalties, true)); |
||
|
|
|||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Apply mask penalty rule 1 and return the penalty. Find repetitive cells with the same color and |
||
| 125 | * give penalty to them. Example: 00000 or 11111. |
||
| 126 | */ |
||
| 127 | public static function testRule1(array $matrix, int $height, int $width):int{ |
||
| 128 | return self::applyRule1($matrix, $height, $width, true) + self::applyRule1($matrix, $height, $width, false); |
||
| 129 | } |
||
| 130 | |||
| 131 | private static function applyRule1(array $matrix, int $height, int $width, bool $isHorizontal):int{ |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Apply mask penalty rule 2 and return the penalty. Find 2x2 blocks with the same color and give |
||
| 166 | * penalty to them. This is actually equivalent to the spec's rule, which is to find MxN blocks and give a |
||
| 167 | * penalty proportional to (M-1)x(N-1), because this is the number of 2x2 blocks inside such a block. |
||
| 168 | */ |
||
| 169 | public static function testRule2(array $matrix, int $height, int $width):int{ |
||
| 170 | $penalty = 0; |
||
| 171 | |||
| 172 | foreach($matrix as $y => $row){ |
||
| 173 | |||
| 174 | if($y > $height - 2){ |
||
| 175 | break; |
||
| 176 | } |
||
| 177 | |||
| 178 | foreach($row as $x => $val){ |
||
| 179 | |||
| 180 | if($x > $width - 2){ |
||
| 181 | break; |
||
| 182 | } |
||
| 183 | |||
| 184 | if( |
||
| 185 | $val === $row[$x + 1] |
||
| 186 | && $val === $matrix[$y + 1][$x] |
||
| 187 | && $val === $matrix[$y + 1][$x + 1] |
||
| 188 | ){ |
||
| 189 | $penalty++; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return 3 * $penalty; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Apply mask penalty rule 3 and return the penalty. Find consecutive runs of 1:1:3:1:1:4 |
||
| 199 | * starting with black, or 4:1:1:3:1:1 starting with white, and give penalty to them. If we |
||
| 200 | * find patterns like 000010111010000, we give penalty once. |
||
| 201 | */ |
||
| 202 | public static function testRule3(array $matrix, int $height, int $width):int{ |
||
| 203 | $penalties = 0; |
||
| 204 | |||
| 205 | foreach($matrix as $y => $row){ |
||
| 206 | foreach($row as $x => $val){ |
||
| 207 | |||
| 208 | if( |
||
| 209 | $x + 6 < $width |
||
| 210 | && $val |
||
| 211 | && !$row[$x + 1] |
||
| 212 | && $row[$x + 2] |
||
| 213 | && $row[$x + 3] |
||
| 214 | && $row[$x + 4] |
||
| 215 | && !$row[$x + 5] |
||
| 216 | && $row[$x + 6] |
||
| 217 | && ( |
||
| 218 | self::isWhiteHorizontal($row, $width, $x - 4, $x) |
||
| 219 | || self::isWhiteHorizontal($row, $width, $x + 7, $x + 11) |
||
| 220 | ) |
||
| 221 | ){ |
||
| 222 | $penalties++; |
||
| 223 | } |
||
| 224 | |||
| 225 | if( |
||
| 226 | $y + 6 < $height |
||
| 227 | && $val |
||
| 228 | && !$matrix[$y + 1][$x] |
||
| 229 | && $matrix[$y + 2][$x] |
||
| 230 | && $matrix[$y + 3][$x] |
||
| 231 | && $matrix[$y + 4][$x] |
||
| 232 | && !$matrix[$y + 5][$x] |
||
| 233 | && $matrix[$y + 6][$x] |
||
| 234 | && ( |
||
| 235 | self::isWhiteVertical($matrix, $height, $x, $y - 4, $y) |
||
| 236 | || self::isWhiteVertical($matrix, $height, $x, $y + 7, $y + 11) |
||
| 237 | ) |
||
| 238 | ){ |
||
| 239 | $penalties++; |
||
| 240 | } |
||
| 241 | |||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | return $penalties * 40; |
||
| 246 | } |
||
| 247 | |||
| 248 | private static function isWhiteHorizontal(array $row, int $width, int $from, int $to):bool{ |
||
| 249 | |||
| 250 | if($from < 0 || $width < $to){ |
||
| 251 | return false; |
||
| 252 | } |
||
| 253 | |||
| 254 | for($x = $from; $x < $to; $x++){ |
||
| 255 | if($row[$x]){ |
||
| 256 | return false; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | return true; |
||
| 261 | } |
||
| 262 | |||
| 263 | private static function isWhiteVertical(array $matrix, int $height, int $x, int $from, int $to):bool{ |
||
| 264 | |||
| 265 | if($from < 0 || $height < $to){ |
||
| 266 | return false; |
||
| 267 | } |
||
| 268 | |||
| 269 | for($y = $from; $y < $to; $y++){ |
||
| 270 | if($matrix[$y][$x]){ |
||
| 271 | return false; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | return true; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Apply mask penalty rule 4 and return the penalty. Calculate the ratio of dark cells and give |
||
| 280 | * penalty if the ratio is far from 50%. It gives 10 penalty for 5% distance. |
||
| 281 | */ |
||
| 282 | public static function testRule4(array $matrix, int $height, int $width):int{ |
||
| 295 | } |
||
| 296 | |||
| 297 | } |
||
| 298 |