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 |
||
| 16 | class Experiments |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Optimizely API Client. |
||
| 20 | * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
||
| 21 | */ |
||
| 22 | private $client; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructor. |
||
| 26 | */ |
||
| 27 | 12 | public function __construct($client) |
|
| 31 | |||
| 32 | /** |
||
| 33 | * Get a list of all the experiments by Project or Campaign |
||
| 34 | * @param integer $projectId |
||
| 35 | * @param integer $campaignId |
||
| 36 | * @param boolean $includeClassic |
||
| 37 | * @param integer $page |
||
| 38 | * @param integer $perPage |
||
| 39 | * @return Result |
||
| 40 | * @throws Exception |
||
| 41 | */ |
||
| 42 | 4 | public function listAll($projectId, $campaignId=null, $includeClassic=false, $page=1, $perPage=25) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get metadata for a single Experiment. |
||
| 77 | * @param integer $experimentId |
||
| 78 | * @return Result |
||
| 79 | * @throws Exception |
||
| 80 | */ |
||
| 81 | 3 | public function get($experimentId) |
|
| 82 | { |
||
| 83 | 3 | if (!is_int($experimentId)) { |
|
| 84 | 1 | throw new Exception("Integer experiment ID expected, while got '$experimentId'", |
|
| 85 | 1 | Exception::CODE_INVALID_ARG); |
|
| 86 | } |
||
| 87 | |||
| 88 | 2 | if ($experimentId<0) { |
|
| 89 | 1 | throw new Exception("A positive experiment ID expected"); |
|
| 90 | } |
||
| 91 | |||
| 92 | 1 | $result = $this->client->sendApiRequest("/experiments/$experimentId"); |
|
| 93 | |||
| 94 | 1 | $experiment = new Experiment($result->getDecodedJsonData()); |
|
| 95 | 1 | $result->setPayload($experiment); |
|
| 96 | |||
| 97 | 1 | return $result; |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get results for a single experiment |
||
| 102 | * @param integer $experimentId The id for the experiment you want results for |
||
| 103 | * @param integer $baselineVariationId The id of the variation to use as the baseline to compare against other variations. Defaults to the first variation if not provided. |
||
| 104 | * @param string $startTime The earliest time to count events in results. Defaults to the time that the experiment was first activated. |
||
| 105 | * @param string $endTime The latest time to count events in results. Defaults to the time the experiment was last active or the current time if the experiment is still running. |
||
| 106 | * @return Result |
||
| 107 | * @throws Exception |
||
| 108 | */ |
||
| 109 | 1 | public function getResults($experimentId, $baselineVariationId = null, $startTime = null, $endTime = null) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Create an experiment in a Project. |
||
| 131 | * @param Experiment $experiment |
||
| 132 | * @param string $action Action to change the state of the Experiment. |
||
| 133 | * @return Result |
||
| 134 | * @throw Exception |
||
| 135 | */ |
||
| 136 | 1 | public function create($experiment, $action = null) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Update an Experiment by ID |
||
| 164 | * @param integer $experimentId |
||
| 165 | * @param Experiment $experiment |
||
| 166 | * @param string $action Action to change the state of the Experiment. |
||
| 167 | * @return Result |
||
| 168 | * @throws Exception |
||
| 169 | */ |
||
| 170 | 1 | View Code Duplication | public function update($experimentId, $experiment, $action) |
| 198 | |||
| 199 | /** |
||
| 200 | * Delete Experiment by ID |
||
| 201 | * @param integer $experimentId |
||
| 202 | * @return Result |
||
| 203 | * @throws Exception |
||
| 204 | */ |
||
| 205 | 1 | public function delete($experimentId) |
|
| 212 | } |
||
| 213 | |||
| 214 |