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 |
||
9 | class SS_Map implements ArrayAccess, Countable, IteratorAggregate { |
||
10 | |||
11 | protected $list, $keyField, $valueField; |
||
12 | |||
13 | /** |
||
14 | * @see SS_Map::unshift() |
||
15 | * |
||
16 | * @var array $firstItems |
||
17 | */ |
||
18 | protected $firstItems = array(); |
||
19 | |||
20 | /** |
||
21 | * @see SS_Map::push() |
||
22 | * |
||
23 | * @var array $lastItems |
||
24 | */ |
||
25 | protected $lastItems = array(); |
||
26 | |||
27 | /** |
||
28 | * Construct a new map around an SS_list. |
||
29 | * |
||
30 | * @param $list The list to build a map from |
||
31 | * @param $keyField The field to use as the key of each map entry |
||
32 | * @param $valueField The field to use as the value of each map entry |
||
33 | */ |
||
34 | public function __construct(SS_List $list, $keyField = "ID", $valueField = "Title") { |
||
39 | |||
40 | /** |
||
41 | * Set the key field for this map. |
||
42 | * |
||
43 | * @var string $keyField |
||
44 | */ |
||
45 | public function setKeyField($keyField) { |
||
48 | |||
49 | /** |
||
50 | * Set the value field for this map. |
||
51 | * |
||
52 | * @var string $valueField |
||
53 | */ |
||
54 | public function setValueField($valueField) { |
||
57 | |||
58 | /** |
||
59 | * Return an array equivalent to this map. |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | public function toArray() { |
||
72 | |||
73 | /** |
||
74 | * Return all the keys of this map. |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | public function keys() { |
||
81 | |||
82 | /** |
||
83 | * Return all the values of this map. |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | public function values() { |
||
90 | |||
91 | /** |
||
92 | * Unshift an item onto the start of the map. |
||
93 | * |
||
94 | * Stores the value in addition to the {@link DataQuery} for the map. |
||
95 | * |
||
96 | * @var string $key |
||
97 | * @var mixed $value |
||
98 | */ |
||
99 | View Code Duplication | public function unshift($key, $value) { |
|
111 | |||
112 | /** |
||
113 | * Pushes an item onto the end of the map. |
||
114 | * |
||
115 | * @var string $key |
||
116 | * @var mixed $value |
||
117 | */ |
||
118 | View Code Duplication | public function push($key, $value) { |
|
131 | |||
132 | // ArrayAccess |
||
133 | |||
134 | /** |
||
135 | * @var string $key |
||
136 | * |
||
137 | * @return boolean |
||
138 | */ |
||
139 | public function offsetExists($key) { |
||
152 | |||
153 | /** |
||
154 | * @var string $key |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | public function offsetGet($key) { |
||
177 | |||
178 | /** |
||
179 | * Sets a value in the map by a given key that has been set via |
||
180 | * {@link SS_Map::push()} or {@link SS_Map::unshift()} |
||
181 | * |
||
182 | * Keys in the map cannot be set since these values are derived from a |
||
183 | * {@link DataQuery} instance. In this case, use {@link SS_Map::toArray()} |
||
184 | * and manipulate the resulting array. |
||
185 | * |
||
186 | * @var string $key |
||
187 | * @var mixed $value |
||
188 | */ |
||
189 | public function offsetSet($key, $value) { |
||
203 | |||
204 | /** |
||
205 | * Removes a value in the map by a given key which has been added to the map |
||
206 | * via {@link SS_Map::push()} or {@link SS_Map::unshift()} |
||
207 | * |
||
208 | * Keys in the map cannot be unset since these values are derived from a |
||
209 | * {@link DataQuery} instance. In this case, use {@link SS_Map::toArray()} |
||
210 | * and manipulate the resulting array. |
||
211 | * |
||
212 | * @var string $key |
||
213 | * @var mixed $value |
||
214 | */ |
||
215 | public function offsetUnset($key) { |
||
233 | |||
234 | /** |
||
235 | * Returns an SS_Map_Iterator instance for iterating over the complete set |
||
236 | * of items in the map. |
||
237 | * |
||
238 | * Satisfies the IteratorAggreagte interface. |
||
239 | * |
||
240 | * @return SS_Map_Iterator |
||
241 | */ |
||
242 | public function getIterator() { |
||
251 | |||
252 | /** |
||
253 | * Returns the count of items in the list including the additional items set |
||
254 | * through {@link SS_Map::push()} and {@link SS_Map::unshift}. |
||
255 | * |
||
256 | * @return int |
||
257 | */ |
||
258 | public function count() { |
||
263 | } |
||
264 | |||
428 |
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.