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 |
||
17 | class AggregationValue implements \ArrayAccess, \IteratorAggregate |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | private $rawData; |
||
23 | |||
24 | /** |
||
25 | * Constructor. |
||
26 | * |
||
27 | * @param array $rawData |
||
28 | */ |
||
29 | public function __construct($rawData) |
||
33 | |||
34 | /** |
||
35 | * Returns aggregation value by name. |
||
36 | * |
||
37 | * @param string $name |
||
38 | * |
||
39 | * @return array |
||
40 | */ |
||
41 | View Code Duplication | public function getValue($name = 'key') |
|
49 | |||
50 | /** |
||
51 | * Returns the document count of the aggregation |
||
52 | * |
||
53 | * @return integer |
||
54 | */ |
||
55 | public function getCount() |
||
59 | |||
60 | /** |
||
61 | * Returns array of bucket values. |
||
62 | * |
||
63 | * @return AggregationValue[]|null |
||
64 | */ |
||
65 | public function getBuckets() |
||
79 | |||
80 | /** |
||
81 | * Returns sub-aggregation. |
||
82 | * |
||
83 | * @param string $name |
||
84 | * |
||
85 | * @return AggregationValue|null |
||
86 | */ |
||
87 | View Code Duplication | public function getAggregation($name) |
|
95 | |||
96 | /** |
||
97 | * Applies path method to aggregations. |
||
98 | * |
||
99 | * @param string $path |
||
100 | * |
||
101 | * @return AggregationValue|null |
||
102 | */ |
||
103 | public function find($path) |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function offsetExists($offset) |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function offsetGet($offset) |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function offsetSet($offset, $value) |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function offsetUnset($offset) |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function getIterator() |
||
164 | } |
||
165 |
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.