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 |
||
| 15 | class ExperimentVariationReach |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Baseline count |
||
| 19 | * @var integer |
||
| 20 | */ |
||
| 21 | private $baselineCount; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Baseline reach |
||
| 25 | * @var number |
||
| 26 | */ |
||
| 27 | private $baselineReach; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Total number of visitors exposed to the experiment |
||
| 31 | * @var integer |
||
| 32 | */ |
||
| 33 | private $totalCount; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Treatment count |
||
| 37 | * @var number |
||
| 38 | */ |
||
| 39 | private $treatmentCount; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Treatment reach |
||
| 43 | * @var number |
||
| 44 | */ |
||
| 45 | private $treatmentReach; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * A map of reach for each Variation keyed by Variation ID |
||
| 49 | * @var array[VariationReach] |
||
| 50 | */ |
||
| 51 | private $variations; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor. |
||
| 55 | */ |
||
| 56 | 3 | public function __construct($options = array()) |
|
| 57 | { |
||
| 58 | 3 | foreach ($options as $name=>$value) { |
|
| 59 | switch ($name) { |
||
| 60 | 3 | case 'baseline_count': $this->setBaselineCount($value); break; |
|
| 61 | 3 | case 'baseline_reach': $this->setBaselineReach($value); break; |
|
| 62 | 3 | case 'total_count': $this->setTotalCount($value); break; |
|
| 63 | 3 | case 'treatment_count': $this->setTreatmentCount($value); break; |
|
| 64 | 3 | case 'treatment_reach': $this->setTreatmentReach($value); break; |
|
| 65 | 3 | View Code Duplication | case 'variations': { |
| 66 | 3 | $variations = array(); |
|
| 67 | 3 | foreach ($value as $name=>$variationInfo) { |
|
| 68 | 3 | $variations[$name] = new VariationReach($variationInfo); |
|
| 69 | } |
||
| 70 | 3 | $this->setVariations($variations); |
|
| 71 | 3 | break; |
|
| 72 | } |
||
| 73 | default: |
||
| 74 | 3 | throw new Exception('Unknown option: ' . $name); |
|
| 75 | } |
||
| 76 | } |
||
| 77 | 3 | } |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Returns this object as array. |
||
| 81 | */ |
||
| 82 | 1 | public function toArray() |
|
| 83 | { |
||
| 84 | $options = array( |
||
| 85 | 1 | 'baseline_count' => $this->getBaselineCount(), |
|
| 86 | 1 | 'baseline_reach' => $this->getBaselineReach(), |
|
| 87 | 1 | 'total_count' => $this->getTotalCount(), |
|
| 88 | 1 | 'treatment_count' => $this->getTreatmentCount(), |
|
| 89 | 1 | 'treatment_reach' => $this->getTreatmentReach(), |
|
| 90 | 'variations' => array(), |
||
| 91 | ); |
||
| 92 | |||
| 93 | 1 | foreach ($this->getVariations() as $name=>$variation) { |
|
| 94 | 1 | $options['variations'][$name] = $variation->toArray(); |
|
| 95 | } |
||
| 96 | |||
| 97 | // Remove options with empty values |
||
| 98 | 1 | $cleanedOptions = array(); |
|
| 99 | 1 | foreach ($options as $name=>$value) { |
|
| 100 | 1 | if ($value!==null) |
|
| 101 | 1 | $cleanedOptions[$name] = $value; |
|
| 102 | } |
||
| 103 | |||
| 104 | 1 | return $cleanedOptions; |
|
| 105 | } |
||
| 106 | |||
| 107 | 1 | public function getBaselineCount() |
|
| 111 | |||
| 112 | 3 | public function setBaselineCount($baselineCount) |
|
| 116 | |||
| 117 | 1 | public function getBaselineReach() |
|
| 121 | |||
| 122 | 3 | public function setBaselineReach($baselineReach) |
|
| 126 | |||
| 127 | 1 | public function getTotalCount() |
|
| 131 | |||
| 132 | 3 | public function setTotalCount($totalCount) |
|
| 136 | |||
| 137 | 1 | public function getTreatmentCount() |
|
| 141 | |||
| 142 | 3 | public function setTreatmentCount($treatmentCount) |
|
| 146 | |||
| 147 | 1 | public function getTreatmentReach() |
|
| 151 | |||
| 152 | 3 | public function setTreatmentReach($treatmentReach) |
|
| 156 | |||
| 157 | 1 | public function getVariations() |
|
| 161 | |||
| 162 | 3 | public function setVariations($variations) |
|
| 166 | } |
||
| 167 | |||
| 168 | |||
| 178 |
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.