| Total Complexity | 50 |
| Total Lines | 178 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MaskPatternTester 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 MaskPatternTester, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class MaskPatternTester{ |
||
| 23 | |||
| 24 | protected QRMatrix $matrix; |
||
| 25 | |||
| 26 | protected int $moduleCount; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Receives the matrix an sets the module count |
||
| 30 | * |
||
| 31 | * @see \chillerlan\QRCode\QROptions::$maskPattern |
||
| 32 | * @see \chillerlan\QRCode\Data\QRMatrix::$maskPattern |
||
| 33 | * @see \chillerlan\QRCode\QRCode::getBestMaskPattern() |
||
| 34 | */ |
||
| 35 | public function __construct(QRMatrix $matrix){ |
||
| 36 | $this->matrix = $matrix; |
||
| 37 | $this->moduleCount = $this->matrix->size(); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Returns the penalty for the given mask pattern |
||
| 42 | * |
||
| 43 | * @see \chillerlan\QRCode\QROptions::$maskPattern |
||
| 44 | * @see \chillerlan\QRCode\Data\QRMatrix::$maskPattern |
||
| 45 | * @see \chillerlan\QRCode\QRCode::getBestMaskPattern() |
||
| 46 | */ |
||
| 47 | public function testPattern():int{ |
||
| 48 | $penalty = 0; |
||
| 49 | |||
| 50 | for($level = 1; $level <= 4; $level++){ |
||
| 51 | $penalty += call_user_func([$this, 'testLevel'.$level]); |
||
| 52 | } |
||
| 53 | |||
| 54 | return (int)$penalty; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Checks for each group of five or more same-colored modules in a row (or column) |
||
| 59 | */ |
||
| 60 | protected function testLevel1():float{ |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Checks for each 2x2 area of same-colored modules in the matrix |
||
| 98 | */ |
||
| 99 | protected function testLevel2():float{ |
||
| 100 | $penalty = 0; |
||
| 101 | |||
| 102 | foreach($this->matrix->matrix() as $y => $row){ |
||
| 103 | |||
| 104 | if($y > $this->moduleCount - 2){ |
||
| 105 | break; |
||
| 106 | } |
||
| 107 | |||
| 108 | foreach($row as $x => $val){ |
||
| 109 | |||
| 110 | if($x > $this->moduleCount - 2){ |
||
| 111 | break; |
||
| 112 | } |
||
| 113 | |||
| 114 | $count = 0; |
||
| 115 | |||
| 116 | if($val >> 8 > 0){ |
||
| 117 | $count++; |
||
| 118 | } |
||
| 119 | |||
| 120 | if($this->matrix->check($y, $x + 1)){ |
||
| 121 | $count++; |
||
| 122 | } |
||
| 123 | |||
| 124 | if($this->matrix->check($y + 1, $x)){ |
||
| 125 | $count++; |
||
| 126 | } |
||
| 127 | |||
| 128 | if($this->matrix->check($y + 1, $x + 1)){ |
||
| 129 | $count++; |
||
| 130 | } |
||
| 131 | |||
| 132 | if($count === 0 || $count === 4){ |
||
| 133 | $penalty += 3; |
||
| 134 | } |
||
| 135 | |||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | return $penalty; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Checks if there are patterns that look similar to the finder patterns |
||
| 144 | */ |
||
| 145 | protected function testLevel3():float{ |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Checks if more than half of the modules are dark or light, with a larger penalty for a larger difference |
||
| 187 | */ |
||
| 188 | protected function testLevel4():float{ |
||
| 200 | } |
||
| 201 | |||
| 202 | } |
||
| 203 |