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 |
||
15 | class CampaignResults |
||
16 | { |
||
17 | /** |
||
18 | * The unique identifier for the Campaign |
||
19 | * @var integer |
||
20 | */ |
||
21 | private $campaignId; |
||
22 | |||
23 | /** |
||
24 | * The significance level at which you would like to declare winning and |
||
25 | * losing variations. A lower number minimizes the time needed to declare a |
||
26 | * winning or losing variation, but increases the risk that your results |
||
27 | * aren't true winners and losers. |
||
28 | * @var number |
||
29 | */ |
||
30 | private $confidenceThreshold; |
||
31 | |||
32 | /** |
||
33 | * The latest time to count events in results |
||
34 | * @var string |
||
35 | */ |
||
36 | private $endTime; |
||
37 | |||
38 | /** |
||
39 | * The breakdown of campaign results by metric. |
||
40 | * @var array[CampaignMetricResults] |
||
41 | */ |
||
42 | private $metrics; |
||
43 | |||
44 | /** |
||
45 | * The earliest time to count events in results |
||
46 | * @var string |
||
47 | */ |
||
48 | private $startTime; |
||
49 | |||
50 | /** |
||
51 | * Constructor. |
||
52 | */ |
||
53 | 3 | View Code Duplication | public function __construct($options = array()) |
74 | |||
75 | /** |
||
76 | * Returns this object as array. |
||
77 | */ |
||
78 | 2 | public function toArray() |
|
101 | |||
102 | 1 | public function getCampaignId() |
|
106 | |||
107 | 3 | public function setCampaignId($campaignId) |
|
111 | |||
112 | 3 | public function getConfidenceThreshold() |
|
116 | |||
117 | 3 | public function setConfidenceThreshold($confidenceThreshold) |
|
121 | |||
122 | 1 | public function getEndTime() |
|
126 | |||
127 | 3 | public function setEndTime($endTime) |
|
131 | |||
132 | 1 | public function getMetrics() |
|
136 | |||
137 | 3 | public function setMetrics($metrics) |
|
141 | |||
142 | 3 | public function getStartTime() |
|
146 | |||
147 | 3 | public function setStartTime($startTime) |
|
151 | } |
||
152 | |||
156 |
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.