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 |
||
17 | class ExperimentResults |
||
18 | { |
||
19 | /** |
||
20 | * The significance level at which you would like to declare winning and |
||
21 | * losing variations. A lower number minimizes the time needed to declare |
||
22 | * a winning or losing variation, but increases the risk that your results |
||
23 | * aren't true winners and losers. |
||
24 | * @var number |
||
25 | */ |
||
26 | private $confidenceThreshold; |
||
27 | |||
28 | /** |
||
29 | * The latest time to count events in results |
||
30 | * @var string |
||
31 | */ |
||
32 | private $endTime; |
||
33 | |||
34 | /** |
||
35 | * The unique identifier for the Experiment. |
||
36 | * @var type |
||
37 | */ |
||
38 | private $experimentId; |
||
39 | |||
40 | /** |
||
41 | * The breakdown of experiment results by metric |
||
42 | * @var array[ExperimentMetricResult] |
||
43 | */ |
||
44 | private $metrics; |
||
45 | |||
46 | /** |
||
47 | * The total number of users exposed to a different experience |
||
48 | * @var ExperimentVariationReach |
||
49 | */ |
||
50 | private $reach; |
||
51 | |||
52 | /** |
||
53 | * The earliest time to count events in results |
||
54 | * @var string |
||
55 | */ |
||
56 | private $startTime; |
||
57 | |||
58 | /** |
||
59 | * |
||
60 | * @var StatsConfig |
||
61 | */ |
||
62 | private $statsConfig; |
||
63 | |||
64 | /** |
||
65 | * Constructor. |
||
66 | */ |
||
67 | 3 | public function __construct($options = array()) |
|
68 | { |
||
69 | 3 | foreach ($options as $name=>$value) { |
|
70 | switch ($name) { |
||
71 | 3 | case 'confidence_threshold': $this->setConfidenceThreshold($value); break; |
|
72 | 3 | case 'end_time': $this->setEndTime($value); break; |
|
73 | 3 | case 'experiment_id': $this->setExperimentId($value); break; |
|
74 | 3 | View Code Duplication | case 'metrics': { |
75 | 3 | $metrics = array(); |
|
76 | 3 | foreach ($value as $metricInfo) { |
|
77 | 3 | $metrics[] = new ExperimentMetricResults($metricInfo); |
|
78 | } |
||
79 | 3 | $this->setMetrics($metrics); |
|
80 | 3 | break; |
|
81 | } |
||
82 | 3 | case 'reach': $this->setReach(new ExperimentVariationReach($value)); break; |
|
83 | 3 | case 'start_time': $this->setStartTime($value); break; |
|
84 | case 'stats_config': $this->setStatsConfig(new StatsConfig($value)); break; |
||
85 | default: |
||
86 | 3 | throw new Exception('Unknown option found in the ExperimentResults entity: ' . $name); |
|
87 | } |
||
88 | } |
||
89 | 3 | } |
|
90 | |||
91 | /** |
||
92 | * Returns this object as array. |
||
93 | */ |
||
94 | 1 | public function toArray() |
|
95 | { |
||
96 | $options = array( |
||
97 | 1 | 'confidence_threshold' => $this->getConfidenceThreshold(), |
|
98 | 1 | 'end_time' => $this->getEndTime(), |
|
99 | 1 | 'experiment_id' => $this->getExperimentId(), |
|
100 | 'metrics' => array(), |
||
101 | 1 | 'reach' => $this->getReach()?$this->getReach()->toArray():null, |
|
102 | 1 | 'start_time' => $this->getStartTime(), |
|
103 | 1 | 'stats_config' => $this->getStatsConfig()?$this->getStatsConfig()->toArray():null, |
|
104 | ); |
||
105 | |||
106 | 1 | foreach ($this->getMetrics() as $metric) { |
|
107 | 1 | $options['metrics'][] = $metric->toArray(); |
|
108 | } |
||
109 | |||
110 | // Remove options with empty values |
||
111 | 1 | $cleanedOptions = array(); |
|
112 | 1 | foreach ($options as $name=>$value) { |
|
113 | 1 | if ($value!==null) |
|
114 | 1 | $cleanedOptions[$name] = $value; |
|
115 | } |
||
116 | |||
117 | 1 | return $cleanedOptions; |
|
118 | } |
||
119 | |||
120 | 3 | public function getConfidenceThreshold() |
|
121 | { |
||
122 | 3 | return $this->confidenceThreshold; |
|
123 | } |
||
124 | |||
125 | 3 | public function setConfidenceThreshold($confidenceThreshold) |
|
129 | |||
130 | 1 | public function getEndTime() |
|
131 | { |
||
132 | 1 | return $this->endTime; |
|
133 | } |
||
134 | |||
135 | 3 | public function setEndTime($endTime) |
|
139 | |||
140 | 1 | public function getExperimentId() |
|
141 | { |
||
142 | 1 | return $this->experimentId; |
|
143 | } |
||
144 | |||
145 | 3 | public function setExperimentId($experimentId) |
|
149 | |||
150 | 1 | public function getMetrics() |
|
151 | { |
||
152 | 1 | return $this->metrics; |
|
153 | } |
||
154 | |||
155 | 3 | public function setMetrics($metrics) |
|
156 | { |
||
157 | 3 | $this->metrics = $metrics; |
|
158 | 3 | } |
|
159 | |||
160 | 1 | public function getReach() |
|
161 | { |
||
162 | 1 | return $this->reach; |
|
163 | } |
||
164 | |||
165 | 3 | public function setReach($reach) |
|
166 | { |
||
167 | 3 | $this->reach = $reach; |
|
168 | 3 | } |
|
169 | |||
170 | 2 | public function getStartTime() |
|
171 | { |
||
172 | 2 | return $this->startTime; |
|
173 | } |
||
174 | |||
175 | 3 | public function setStartTime($startTime) |
|
176 | { |
||
177 | 3 | $this->startTime = $startTime; |
|
178 | 3 | } |
|
179 | |||
180 | 1 | public function getStatsConfig() |
|
181 | { |
||
182 | 1 | return $this->statsConfig; |
|
183 | } |
||
184 | |||
185 | public function setStatsConfig($statsConfig) |
||
189 | } |
||
190 | |||
191 | |||
192 | |||
193 | |||
194 | |||
195 | |||
196 | |||
197 | |||
198 | |||
199 | |||
200 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.