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 ExperimentMetricResults 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 ExperimentMetricResults, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class ExperimentMetricResults |
||
16 | { |
||
17 | /** |
||
18 | * The aggregation function for the numerator of the metric. 'unique' measures |
||
19 | * the number of unique visitors/sessions that include the specified Event. |
||
20 | * 'count' measures the total number of occurrences of Event for the scope |
||
21 | * (visitor/session). 'sum' is the sum of the 'field' value. 'exit' measures |
||
22 | * the ratio of sessions with last activation occurring on the target page to |
||
23 | * the sessions that activated the target page at least once during the session. |
||
24 | * 'bounce' measures the ratio of sessions that with first and last activation |
||
25 | * occurring on the target page to the sessions with first activation on the |
||
26 | * target page. For both 'exit' and 'bounce', the eventId must be the ID of a Page. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $aggregator; |
||
31 | |||
32 | /** |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $eventId; |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $eventName; |
||
43 | |||
44 | /** |
||
45 | * |
||
46 | * @var type |
||
47 | */ |
||
48 | private $field; |
||
49 | |||
50 | /** |
||
51 | * Conversions indicate the total number of visitors or sessions where the |
||
52 | * event happened. Impressions indicate the total number of times the event |
||
53 | * happened (possibly multiple per visitor or session). Revenue indicates |
||
54 | * the sum of all revenue sent from all events in the Experiment. |
||
55 | * Can be 'conversions', 'impressions' or 'revenue'. |
||
56 | * @var string |
||
57 | */ |
||
58 | private $measure; |
||
59 | |||
60 | /** |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | private $metricId; |
||
65 | |||
66 | /** |
||
67 | * |
||
68 | * @var integer |
||
69 | */ |
||
70 | private $priority; |
||
71 | |||
72 | /** |
||
73 | * Can be 'session', 'visitor' or 'event' |
||
74 | * @var string |
||
75 | */ |
||
76 | private $unit; |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | private $name; |
||
83 | |||
84 | /** |
||
85 | * A map of results for each variation in the Experiment keyed by variation ID. |
||
86 | * For Personalization Campaigns, the special variant 'baseline' represents |
||
87 | * visitors that have been held back from any change in experience for the Experiment |
||
88 | * |
||
89 | * @var object[VariantResults] |
||
90 | */ |
||
91 | private $results; |
||
92 | |||
93 | /** |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | private $scope; |
||
98 | |||
99 | /** |
||
100 | * |
||
101 | * @var type |
||
102 | */ |
||
103 | private $winningDirection; |
||
104 | |||
105 | /** |
||
106 | * Constructor. |
||
107 | */ |
||
108 | 3 | public function __construct($options = array()) |
|
135 | |||
136 | /** |
||
137 | * Returns this object as array. |
||
138 | */ |
||
139 | public function toArray() |
||
156 | |||
157 | public function getAggregator() |
||
161 | |||
162 | public function setAggregator($aggregator) |
||
166 | |||
167 | public function getEventId() |
||
171 | |||
172 | public function setEventId($eventId) |
||
176 | |||
177 | public function getEventName() |
||
181 | |||
182 | public function setEventName($eventName) |
||
186 | |||
187 | public function getField() |
||
191 | |||
192 | public function setField($field) |
||
196 | |||
197 | public function getMeasure() |
||
201 | |||
202 | public function setMeasure($measure) |
||
206 | |||
207 | public function getMetricId() |
||
211 | |||
212 | public function setMetricId($metricId) |
||
216 | |||
217 | public function getPriority() |
||
221 | |||
222 | public function setPriority($priority) |
||
226 | |||
227 | public function getUnit() |
||
231 | |||
232 | public function setUnit($unit) |
||
236 | |||
237 | public function getResults() |
||
241 | |||
242 | public function setResults($results) |
||
246 | |||
247 | public function getName() |
||
251 | |||
252 | public function setName($name) |
||
256 | |||
257 | public function getScope() |
||
261 | |||
262 | public function setScope($scope) |
||
266 | |||
267 | public function getWinningDirection() |
||
271 | |||
272 | public function setWinningDirection($winningDirection) |
||
276 | } |
||
277 | |||
287 |
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.