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 Experiment 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 Experiment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Experiment |
||
18 | { |
||
19 | /** |
||
20 | * The project the Experiment is in |
||
21 | * @var integer |
||
22 | */ |
||
23 | private $projectId; |
||
24 | |||
25 | /** |
||
26 | * List of IDs of all audiences the Experiment is targeted at |
||
27 | * @var array[integer] |
||
28 | */ |
||
29 | private $audienceIds; |
||
30 | |||
31 | /** |
||
32 | * The ID for the Campaign that the Experiment is in |
||
33 | * @var integer |
||
34 | */ |
||
35 | private $campaignId; |
||
36 | |||
37 | /** |
||
38 | * Experiment-level changes that will run before all Variations. Typically |
||
39 | * this is custom CSS or custom JavaScript. |
||
40 | * @var array[Change] |
||
41 | */ |
||
42 | private $changes; |
||
43 | |||
44 | /** |
||
45 | * The time the experiment was initially created |
||
46 | * @var string |
||
47 | */ |
||
48 | private $created; |
||
49 | |||
50 | /** |
||
51 | * The description or hypothesis for an Experiment |
||
52 | * @var string |
||
53 | */ |
||
54 | private $description; |
||
55 | |||
56 | /** |
||
57 | * Percentage expressed as a number from 0-10000 to hold back from being |
||
58 | * included in the experiment |
||
59 | * @var integer |
||
60 | */ |
||
61 | private $holdback; |
||
62 | |||
63 | /** |
||
64 | * Unique string identifier for this experiment within the project. |
||
65 | * @var string |
||
66 | */ |
||
67 | private $key; |
||
68 | |||
69 | /** |
||
70 | * The last time the experiment was modified |
||
71 | * @var string |
||
72 | */ |
||
73 | private $lastModified; |
||
74 | |||
75 | /** |
||
76 | * An ordered list of metrics to track for the experiment |
||
77 | * @var array[Metric] |
||
78 | */ |
||
79 | private $metrics; |
||
80 | |||
81 | /** |
||
82 | * Name of the Experiment |
||
83 | * @var string |
||
84 | */ |
||
85 | private $name; |
||
86 | |||
87 | /** |
||
88 | * The last time the experiment was modified |
||
89 | * @var Schedule |
||
90 | */ |
||
91 | private $schedule; |
||
92 | |||
93 | /** |
||
94 | * Current state of the experiment. Can be 'active', 'paused' or 'archived'. |
||
95 | * @var string |
||
96 | */ |
||
97 | private $status; |
||
98 | |||
99 | /** |
||
100 | * A list of Variations that each define an experience to show in the context |
||
101 | * of the Experiment for the purpose of comparison against each other |
||
102 | * @var array[Variation] |
||
103 | */ |
||
104 | private $variations = array(); |
||
105 | |||
106 | /** |
||
107 | * The unique identifier for the Experiment |
||
108 | * @var integer |
||
109 | */ |
||
110 | private $id; |
||
111 | |||
112 | /** |
||
113 | * Whether or not the Experiment is a classic Experiment |
||
114 | * @var boolean |
||
115 | */ |
||
116 | private $isClassic; |
||
117 | |||
118 | /** |
||
119 | * Constructor. |
||
120 | */ |
||
121 | 7 | public function __construct($options = array()) |
|
122 | { |
||
123 | 7 | foreach ($options as $name=>$value) { |
|
124 | switch ($name) { |
||
125 | 6 | case 'project_id': $this->setProjectId($value); break; |
|
126 | 6 | case 'audience_ids': $this->setAudienceIds($value); break; |
|
127 | 6 | case 'campaign_id': $this->setCampaignId($value); break; |
|
128 | 6 | View Code Duplication | case 'changes': { |
129 | 6 | $changes = array(); |
|
130 | 6 | foreach ($value as $changeInfo) { |
|
131 | 6 | $changes[] = new Change($changeInfo); |
|
132 | } |
||
133 | 6 | $this->setChanges($changes); |
|
134 | 6 | break; |
|
135 | } |
||
136 | 6 | case 'created': $this->setCreated($value); break; |
|
137 | 6 | case 'description': $this->setDescription($value); break; |
|
138 | 6 | case 'holdback': $this->setHoldback($value); break; |
|
139 | 6 | case 'key': $this->setKey($value); break; |
|
140 | 6 | case 'last_modified': $this->setLastModified($value); break; |
|
141 | 6 | case 'metrics': { |
|
142 | 6 | $metrics = array(); |
|
143 | 6 | foreach ($value as $metricInfo) { |
|
144 | 6 | $metrics[] = new Metric($metricInfo); |
|
145 | } |
||
146 | 6 | $this->setMetrics($metrics); |
|
147 | 6 | break; |
|
148 | } |
||
149 | 6 | case 'name': $this->setName($value); break; |
|
150 | 6 | case 'schedule': $this->setSchedule(new Schedule($value)); break; |
|
151 | 6 | case 'status': $this->setStatus($value); break; |
|
152 | 6 | View Code Duplication | case 'variations': { |
153 | 6 | $variations = array(); |
|
154 | 6 | foreach ($value as $variationInfo) { |
|
155 | 6 | $variations[] = new Variation($variationInfo); |
|
156 | } |
||
157 | 6 | $this->setVariations($variations); |
|
158 | 6 | break; |
|
159 | } |
||
160 | 4 | case 'id': $this->setId($value); break; |
|
161 | 4 | case 'is_classic': $this->setIsClassic($value); break; |
|
162 | default: |
||
163 | 6 | throw new \Exception('Unknown option: ' . $name); |
|
164 | } |
||
165 | } |
||
166 | 7 | } |
|
167 | |||
168 | /** |
||
169 | * Returns this object as array. |
||
170 | */ |
||
171 | 3 | public function toArray() |
|
172 | { |
||
173 | $options = array( |
||
174 | 3 | 'project_id' => $this->getProjectId(), |
|
175 | 3 | 'audience_ids' => $this->getAudienceIds(), |
|
176 | 3 | 'campaign_id' => $this->getCampaignId(), |
|
177 | 'changes' => array(), |
||
178 | 3 | 'created' => $this->getCreated(), |
|
179 | 3 | 'description' => $this->getDescription(), |
|
180 | 3 | 'holdback' => $this->getHoldback(), |
|
181 | 3 | 'key' => $this->getKey(), |
|
182 | 3 | 'last_modified' => $this->getLastModified(), |
|
183 | 'metrics' => array(), |
||
184 | 3 | 'name' => $this->getName(), |
|
185 | 3 | 'schedule' => $this->getSchedule()?$this->getSchedule()->toArray():null, |
|
186 | 3 | 'status' => $this->getStatus(), |
|
187 | 'variations' => array(), |
||
188 | 3 | 'id' => $this->getId(), |
|
189 | 3 | 'is_classic' => $this->getIsClassic(), |
|
190 | ); |
||
191 | |||
192 | 3 | foreach ($this->getChanges() as $change) { |
|
193 | 3 | $options['changes'][] = $change->toArray(); |
|
194 | } |
||
195 | |||
196 | 3 | foreach ($this->getMetrics() as $metric) { |
|
197 | 3 | $options['metrics'][] = $metric->toArray(); |
|
198 | } |
||
199 | |||
200 | 3 | foreach ($this->getVariations() as $variation) { |
|
201 | 3 | $options['variations'][] = $variation->toArray(); |
|
202 | } |
||
203 | |||
204 | // Remove options with empty values |
||
205 | 3 | $cleanedOptions = array(); |
|
206 | 3 | foreach ($options as $name=>$value) { |
|
207 | 3 | if ($value!==null) |
|
208 | 3 | $cleanedOptions[$name] = $value; |
|
209 | } |
||
210 | |||
211 | 3 | return $cleanedOptions; |
|
212 | } |
||
213 | |||
214 | 5 | public function getProjectId() |
|
218 | |||
219 | 7 | public function setProjectId($projectId) |
|
223 | |||
224 | 4 | public function getAudienceIds() |
|
228 | |||
229 | 7 | public function setAudienceIds($audienceIds) |
|
233 | |||
234 | 4 | public function getCampaignId() |
|
238 | |||
239 | 7 | public function setCampaignId($campaignId) |
|
243 | |||
244 | 4 | public function getChanges() |
|
248 | |||
249 | 7 | public function setChanges($changes) |
|
253 | |||
254 | 4 | public function getCreated() |
|
258 | |||
259 | 5 | public function setCreated($created) |
|
263 | |||
264 | 4 | public function getDescription() |
|
268 | |||
269 | 7 | public function setDescription($description) |
|
273 | |||
274 | 4 | public function getHoldback() |
|
278 | |||
279 | 7 | public function setHoldback($holdback) |
|
283 | |||
284 | 4 | public function getKey() |
|
288 | |||
289 | 7 | public function setKey($key) |
|
293 | |||
294 | 4 | public function getLastModified() |
|
298 | |||
299 | 5 | public function setLastModified($lastModified) |
|
303 | |||
304 | 4 | public function getMetrics() |
|
308 | |||
309 | 7 | public function setMetrics($metrics) |
|
313 | |||
314 | 7 | public function getName() |
|
318 | |||
319 | 7 | public function setName($name) |
|
323 | |||
324 | 4 | public function getSchedule() |
|
328 | |||
329 | 7 | public function setSchedule($schedule) |
|
333 | |||
334 | 4 | public function getStatus() |
|
338 | |||
339 | 7 | public function setStatus($status) |
|
343 | |||
344 | 4 | public function getVariations() |
|
348 | |||
349 | 7 | public function setVariations($variations) |
|
353 | |||
354 | 4 | public function getId() |
|
358 | |||
359 | 5 | public function setId($id) |
|
363 | |||
364 | 4 | public function getIsClassic() |
|
368 | |||
369 | 5 | public function setIsClassic($isClassic) |
|
373 | } |
||
374 | |||
375 |
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.