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 |
||
22 | class RangeAggregation extends AbstractAggregation |
||
23 | { |
||
24 | use BucketingTrait; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $ranges = []; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | private $keyed = false; |
||
35 | |||
36 | /** |
||
37 | * Inner aggregations container init. |
||
38 | * |
||
39 | * @param string $name |
||
40 | * @param string $field |
||
41 | * @param array $ranges |
||
42 | * @param bool $keyed |
||
43 | */ |
||
44 | View Code Duplication | public function __construct($name, $field = null, $ranges = [], $keyed = false) |
|
57 | |||
58 | /** |
||
59 | * Sets if result buckets should be keyed. |
||
60 | * |
||
61 | * @param bool $keyed |
||
62 | * |
||
63 | * @return RangeAggregation |
||
64 | */ |
||
65 | public function setKeyed($keyed) |
||
71 | |||
72 | /** |
||
73 | * Add range to aggregation. |
||
74 | * |
||
75 | * @param int|float|null $from |
||
76 | * @param int|float|null $to |
||
77 | * @param string $key |
||
78 | * |
||
79 | * @return RangeAggregation |
||
80 | */ |
||
81 | public function addRange($from = null, $to = null, $key = '') |
||
101 | |||
102 | /** |
||
103 | * Remove range from aggregation. Returns true on success. |
||
104 | * |
||
105 | * @param int|float|null $from |
||
106 | * @param int|float|null $to |
||
107 | * |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function removeRange($from, $to) |
||
122 | |||
123 | /** |
||
124 | * Removes range by key. |
||
125 | * |
||
126 | * @param string $key Range key. |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function removeRangeByKey($key) |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function getArray() |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function getType() |
||
169 | } |
||
170 |
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.