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 |
||
23 | class PercentilesAggregation extends AbstractAggregation |
||
24 | { |
||
25 | use MetricTrait; |
||
26 | use ScriptAwareTrait; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $percents; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | private $compression; |
||
37 | |||
38 | /** |
||
39 | * Inner aggregations container init. |
||
40 | * |
||
41 | * @param string $name |
||
42 | * @param string $field |
||
43 | * @param array $percents |
||
44 | * @param string $script |
||
45 | * @param int $compression |
||
46 | */ |
||
47 | View Code Duplication | public function __construct($name, $field = null, $percents = null, $script = null, $compression = null) |
|
56 | |||
57 | /** |
||
58 | * @return array |
||
59 | */ |
||
60 | public function getPercents() |
||
64 | |||
65 | /** |
||
66 | * @param array $percents |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function setPercents($percents) |
||
76 | |||
77 | /** |
||
78 | * @return int |
||
79 | */ |
||
80 | public function getCompression() |
||
84 | |||
85 | /** |
||
86 | * @param int $compression |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function setCompression($compression) |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function getType() |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | View Code Duplication | public function getArray() |
|
126 | |||
127 | /** |
||
128 | * @param array $a |
||
129 | * |
||
130 | * @throws \LogicException |
||
131 | */ |
||
132 | private function isRequiredParametersSet($a) |
||
138 | } |
||
139 |
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.