Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
20 | class MaskPatternTester{ |
||
21 | |||
22 | /** |
||
23 | * @var \chillerlan\QRCode\Data\QRMatrix |
||
24 | */ |
||
25 | protected $matrix; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $moduleCount; |
||
31 | |||
32 | /** |
||
33 | * Receives the matrix an sets the module count |
||
34 | * |
||
35 | * @see \chillerlan\QRCode\QROptions::$maskPattern |
||
36 | * @see \chillerlan\QRCode\Data\QRMatrix::$maskPattern |
||
37 | * @see \chillerlan\QRCode\QRCode::getBestMaskPattern() |
||
38 | * |
||
39 | * @param \chillerlan\QRCode\Data\QRMatrix $matrix |
||
40 | */ |
||
41 | public function __construct(QRMatrix $matrix){ |
||
42 | $this->matrix = $matrix; |
||
43 | $this->moduleCount = $this->matrix->size(); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Returns the penalty for the given mask pattern |
||
48 | * |
||
49 | * @see \chillerlan\QRCode\QROptions::$maskPattern |
||
50 | * @see \chillerlan\QRCode\Data\QRMatrix::$maskPattern |
||
51 | * @see \chillerlan\QRCode\QRCode::getBestMaskPattern() |
||
52 | * |
||
53 | * @return int |
||
54 | */ |
||
55 | public function testPattern():int{ |
||
64 | |||
65 | /** |
||
66 | * Checks for each group of five or more same-colored modules in a row (or column) |
||
67 | * |
||
68 | * @return float |
||
69 | */ |
||
70 | protected function testLevel1():float{ |
||
105 | |||
106 | /** |
||
107 | * Checks for each 2x2 area of same-colored modules in the matrix |
||
108 | * |
||
109 | * @return float |
||
110 | */ |
||
111 | protected function testLevel2():float{ |
||
153 | |||
154 | /** |
||
155 | * Checks if there are patterns that look similar to the finder patterns |
||
156 | * |
||
157 | * @return float |
||
158 | */ |
||
159 | protected function testLevel3():float{ |
||
198 | |||
199 | /** |
||
200 | * Checks if more than half of the modules are dark or light, with a larger penalty for a larger difference |
||
201 | * |
||
202 | * @return float |
||
203 | */ |
||
204 | protected function testLevel4():float { |
||
217 | |||
218 | } |
||
219 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.