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:
1 | <?php |
||
8 | class Difference |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | private $bitmap; |
||
14 | |||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | private $width; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | private $height; |
||
24 | |||
25 | /** |
||
26 | * @param Image $image |
||
27 | */ |
||
28 | 1 | public function __construct(Image $image) |
|
34 | |||
35 | /** |
||
36 | * @return array |
||
37 | */ |
||
38 | 1 | public function getBitmap() |
|
42 | |||
43 | /** |
||
44 | * @return int |
||
45 | */ |
||
46 | public function getWidth() |
||
50 | |||
51 | /** |
||
52 | * @return int |
||
53 | */ |
||
54 | public function getHeight() |
||
58 | |||
59 | /** |
||
60 | * New Difference with scaled differences. |
||
61 | * |
||
62 | * @param float $factor |
||
63 | * |
||
64 | * @return Difference |
||
65 | */ |
||
66 | 1 | View Code Duplication | public function withScale($factor) |
77 | |||
78 | /** |
||
79 | * Maximum difference for all bitmap pixels. |
||
80 | * |
||
81 | * @return int |
||
82 | */ |
||
83 | 1 | private function maximum() |
|
91 | |||
92 | /** |
||
93 | * New Difference from reduced standard deviation. |
||
94 | * |
||
95 | * @return Difference |
||
96 | */ |
||
97 | 1 | View Code Duplication | public function withReducedStandardDeviation() |
108 | |||
109 | /** |
||
110 | * Standard deviation for all bitmap pixels. |
||
111 | * |
||
112 | * @return float |
||
113 | */ |
||
114 | 1 | private function standardDeviation() |
|
123 | |||
124 | /** |
||
125 | * Average difference for all bitmap pixels. |
||
126 | * |
||
127 | * @return int |
||
128 | */ |
||
129 | 1 | private function average() |
|
137 | |||
138 | /** |
||
139 | * @param string $property |
||
140 | * @param mixed $value |
||
141 | * |
||
142 | * @return Difference |
||
143 | */ |
||
144 | 1 | private function cloneWith($property, $value) |
|
151 | |||
152 | /** |
||
153 | * Boundary for all significant pixels. |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | 1 | public function boundary() |
|
198 | |||
199 | /** |
||
200 | * Percentage of different pixels in the bitmap. |
||
201 | * |
||
202 | * @return float |
||
203 | */ |
||
204 | 1 | public function percentage() |
|
212 | } |
||
213 |
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.