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 Ipv4RangeAggregation extends AbstractAggregation |
||
23 | { |
||
24 | use BucketingTrait; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $ranges = []; |
||
30 | |||
31 | /** |
||
32 | * Inner aggregations container init. |
||
33 | * |
||
34 | * @param string $name |
||
35 | * @param string $field |
||
36 | * @param array $ranges |
||
37 | */ |
||
38 | View Code Duplication | public function __construct($name, $field = null, $ranges = []) |
|
53 | |||
54 | /** |
||
55 | * Add range to aggregation. |
||
56 | * |
||
57 | * @param string|null $from |
||
58 | * @param string|null $to |
||
59 | * |
||
60 | * @return Ipv4RangeAggregation |
||
61 | */ |
||
62 | View Code Duplication | public function addRange($from = null, $to = null) |
|
75 | |||
76 | /** |
||
77 | * Add ip mask to aggregation. |
||
78 | * |
||
79 | * @param string $mask |
||
80 | * |
||
81 | * @return Ipv4RangeAggregation |
||
82 | */ |
||
83 | public function addMask($mask) |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function getType() |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | public function getArray() |
||
111 | } |
||
112 |
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.