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 |
||
| 12 | class WeightedAvg extends AbstractAggregation |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Set the value for this aggregation. |
||
| 16 | * |
||
| 17 | * @param mixed $missing |
||
| 18 | * |
||
| 19 | * @return $this |
||
| 20 | */ |
||
| 21 | View Code Duplication | public function setValue(string $field, $missing = null) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Set the value as a script for this aggregation. |
||
| 38 | * |
||
| 39 | * @return $this |
||
| 40 | */ |
||
| 41 | View Code Duplication | public function setValueScript(string $script) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Set the weight for this aggregation. |
||
| 52 | * |
||
| 53 | * @param mixed $missing |
||
| 54 | * |
||
| 55 | * @return $this |
||
| 56 | */ |
||
| 57 | View Code Duplication | public function setWeight(string $field, $missing = null) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Set the weight as a script for this aggregation. |
||
| 74 | * |
||
| 75 | * @return $this |
||
| 76 | */ |
||
| 77 | View Code Duplication | public function setWeightScript(string $script) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Set the format for this aggregation. |
||
| 88 | * |
||
| 89 | * @param string $format |
||
| 90 | * |
||
| 91 | * @return $this |
||
| 92 | */ |
||
| 93 | public function setFormat($format) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set the value_type for this aggregation. |
||
| 100 | * |
||
| 101 | * @param string $format |
||
| 102 | * @param mixed $valueType |
||
| 103 | * |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | public function setValueType($valueType) |
||
| 110 | } |
||
| 111 |
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.