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 |
||
23 | class Experiment { |
||
24 | |||
25 | /** |
||
26 | * Defines the name of the original version. |
||
27 | */ |
||
28 | const ORIGINAL_VARIATION_NAME = 'original'; |
||
29 | |||
30 | /** |
||
31 | * Instead of the word 'original', one can also set '0' to mark a variation as the original version. |
||
32 | */ |
||
33 | const ORIGINAL_VARIATION_ID = '0'; |
||
34 | |||
35 | /** |
||
36 | * Is returned by {@link getActivatedVariation()} when no variation should be activated. |
||
37 | */ |
||
38 | const DO_NOT_TRIGGER = null; |
||
39 | |||
40 | /** |
||
41 | * @var int|string |
||
42 | */ |
||
43 | private $name; |
||
44 | |||
45 | /** |
||
46 | * @var Variations |
||
47 | */ |
||
48 | private $variations; |
||
49 | |||
50 | /** |
||
51 | * @var FilterInterface |
||
52 | */ |
||
53 | private $filter; |
||
54 | |||
55 | /** |
||
56 | * @var StorageInterface |
||
57 | */ |
||
58 | private $storage; |
||
59 | |||
60 | /** |
||
61 | * Creates a new experiment |
||
62 | * |
||
63 | * @param string $experimentNameOrId Can be any experiment name or an id of the experiment (eg as given by A/B Testing for Piwik) |
||
64 | * @param array|VariationInterface[] $variations |
||
65 | * @param array $config |
||
66 | */ |
||
67 | 23 | public function __construct($experimentNameOrId, $variations, $config = []) |
|
104 | |||
105 | /** |
||
106 | * Get the name of this experiment. |
||
107 | * |
||
108 | * @return int|string |
||
109 | */ |
||
110 | 7 | public function getExperimentName() |
|
114 | |||
115 | /** |
||
116 | * Forces the activation of the given variation name. |
||
117 | * |
||
118 | * @param string $variationName |
||
119 | */ |
||
120 | 4 | public function forceVariationName($variationName) |
|
124 | |||
125 | /** |
||
126 | * Detect whether any variation, including the original version, should be activated. |
||
127 | * |
||
128 | * Returns true if a variation should and will be activated when calling eg {@link getActivatedVariation()} |
||
129 | * or {@link run()}, false if no variation will be activated. |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function shouldTrigger() |
||
137 | |||
138 | /** |
||
139 | * Get the activated variation for this experiment, or null if no variation was activated because of a set filter. |
||
140 | * For example when the user does not take part in the experiment or when a scheduled date prevents the activation |
||
141 | * of a variation. Returns the activated variation if no filter "blocked" it. On the first request, a variation |
||
142 | * will be randomly chosen unless it was forced by {@link forceVariationName()}. On all subsequent requests |
||
143 | * it will reuse the variation that was activated on the first request. |
||
144 | * |
||
145 | * @return VariationInterface|null |
||
146 | */ |
||
147 | 7 | public function getActivatedVariation() |
|
170 | |||
171 | /** |
||
172 | * Get the set filter. |
||
173 | * |
||
174 | * @return FilterInterface |
||
175 | */ |
||
176 | 3 | public function getFilter() |
|
180 | |||
181 | /** |
||
182 | * Get the set variations. |
||
183 | * |
||
184 | * @return Variations |
||
185 | */ |
||
186 | 6 | public function getVariations() |
|
190 | |||
191 | /** |
||
192 | * Get the set storage. |
||
193 | * @return Cookie|StorageInterface |
||
194 | */ |
||
195 | 4 | public function getStorage() |
|
199 | |||
200 | /** |
||
201 | * Tracks the activation of a variation using for example the Piwik Tracker. This lets Piwik know which variation |
||
202 | * was activated and should be used if you track your application using the Piwik Tracker server side. If you are |
||
203 | * usually tracking using the JavaScript Tracker, have a look at {@link getTrackingScript()}. |
||
204 | * |
||
205 | * @param \stdClass|\PiwikTracker $tracker The passed object needs to implement a `doTrackEvent` method accepting |
||
206 | * three parameters $category, $action, $name |
||
207 | */ |
||
208 | 3 | public function trackVariationActivation($tracker) |
|
225 | |||
226 | /** |
||
227 | * Returns the JavaScript tracking code that you can echo in your website to let Piwik know which variation was |
||
228 | * activated server side. |
||
229 | * |
||
230 | * Do not pass variables from $_GET or $_POST etc. Make sure to escape the variables before passing them |
||
231 | * to this method as you would otherwise risk an XSS. |
||
232 | * |
||
233 | * @param string $experimentName ExperimentName and VariationName needs to be passed cause we do not yet have a way |
||
234 | * here to properly escape it to prevent XSS. |
||
235 | * @param string $variationName |
||
236 | * @return string The Piwik tracking code including the `<script>` elements and _paq.push(). |
||
237 | */ |
||
238 | 1 | public function getTrackingScript($experimentName, $variationName) |
|
242 | |||
243 | /** |
||
244 | * Generates a random integer by using the best method available. |
||
245 | * |
||
246 | * @param int $min Minimum value |
||
247 | * @param int $max Maximum value |
||
248 | * @return int|null |
||
249 | */ |
||
250 | 18 | public static function getRandomInt($min = 0, $max = 999999) |
|
275 | } |
||
276 |
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.