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 DateRangeAggregation extends AbstractAggregation |
||
23 | { |
||
24 | use BucketingTrait; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $format; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $ranges = []; |
||
35 | |||
36 | /** |
||
37 | * @var bool |
||
38 | */ |
||
39 | private $keyed = false; |
||
40 | |||
41 | /** |
||
42 | * @param string $name |
||
43 | * @param string $field |
||
44 | * @param string $format |
||
45 | * @param array $ranges |
||
46 | * @param bool $keyed |
||
47 | */ |
||
48 | View Code Duplication | public function __construct($name, $field = null, $format = null, array $ranges = [], $keyed = false) |
|
62 | |||
63 | /** |
||
64 | * Sets if result buckets should be keyed. |
||
65 | * |
||
66 | * @param bool $keyed |
||
67 | * |
||
68 | * @return DateRangeAggregation |
||
69 | */ |
||
70 | public function setKeyed($keyed) |
||
76 | |||
77 | /** |
||
78 | * @return string |
||
79 | */ |
||
80 | public function getFormat() |
||
84 | |||
85 | /** |
||
86 | * @param string $format |
||
87 | */ |
||
88 | public function setFormat($format) |
||
92 | |||
93 | /** |
||
94 | * Add range to aggregation. |
||
95 | * |
||
96 | * @param string|null $from |
||
97 | * @param string|null $to |
||
98 | * @param string|null $key |
||
99 | * |
||
100 | * @return $this |
||
101 | * |
||
102 | * @throws \LogicException |
||
103 | */ |
||
104 | View Code Duplication | public function addRange($from = null, $to = null, $key = null) |
|
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function getArray() |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function getType() |
||
151 | } |
||
152 |
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.