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()) |
|
167 | |||
168 | /** |
||
169 | * Returns this object as array. |
||
170 | */ |
||
171 | 3 | public function toArray() |
|
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.