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. 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 MaskPatternTester, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class MaskPatternTester{ |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var \chillerlan\QRCode\Data\QRMatrix |
||
| 22 | */ |
||
| 23 | protected $matrix; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | protected $moduleCount; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param \chillerlan\QRCode\Data\QRMatrix $matrix |
||
| 32 | */ |
||
| 33 | public function setMatrix(QRMatrix $matrix){ |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Returns the penalty for the given mask pattern |
||
| 40 | * |
||
| 41 | * @see \chillerlan\QRCode\QRCode::getBestMaskPattern() |
||
| 42 | * |
||
| 43 | * @return float |
||
| 44 | */ |
||
| 45 | public function testPattern():float{ |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Checks for each group of five or more same-colored modules in a row (or column) |
||
| 57 | * |
||
| 58 | * @return float |
||
| 59 | */ |
||
| 60 | protected function testLevel1():float{ |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Checks for each 2x2 area of same-colored modules in the matrix |
||
| 98 | * |
||
| 99 | * @return float |
||
| 100 | */ |
||
| 101 | protected function testLevel2():float{ |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Checks if there are patterns that look similar to the finder patterns |
||
| 146 | * |
||
| 147 | * @return float |
||
| 148 | */ |
||
| 149 | protected function testLevel3():float{ |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Checks if more than half of the modules are dark or light, with a larger penalty for a larger difference |
||
| 197 | * |
||
| 198 | * @return float |
||
| 199 | */ |
||
| 200 | protected function testLevel4():float { |
||
| 213 | |||
| 214 | |||
| 215 | } |
||
| 216 |