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 |
||
19 | class ValueAggregation implements \ArrayAccess, \IteratorAggregate |
||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $rawData; |
||
25 | |||
26 | /** |
||
27 | * Constructor. |
||
28 | * |
||
29 | * @param array $rawData |
||
30 | */ |
||
31 | public function __construct($rawData) |
||
35 | |||
36 | /** |
||
37 | * Returns aggregation value by name. |
||
38 | * |
||
39 | * @param string $name |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | public function getValue($name) |
||
51 | |||
52 | /** |
||
53 | * Returns array of bucket values. |
||
54 | * |
||
55 | * @return ValueAggregation[]|null |
||
56 | */ |
||
57 | public function getBuckets() |
||
71 | |||
72 | /** |
||
73 | * Returns sub-aggregation. |
||
74 | * |
||
75 | * @param string $name |
||
76 | * |
||
77 | * @return ValueAggregation|null |
||
78 | */ |
||
79 | View Code Duplication | public function getAggregation($name) |
|
90 | |||
91 | /** |
||
92 | * Applies path method to aggregations. |
||
93 | * |
||
94 | * @param string $path |
||
95 | * |
||
96 | * @return ValueAggregation|null |
||
97 | */ |
||
98 | public function find($path) |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function offsetExists($offset) |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function offsetGet($offset) |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function offsetSet($offset, $value) |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function offsetUnset($offset) |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | public function getIterator() |
||
159 | } |
||
160 |
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.