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 |
||
28 | class TwoDimensionalHashMap |
||
29 | { |
||
30 | /** |
||
31 | * @var array[] |
||
32 | */ |
||
33 | private $values = array(); |
||
34 | |||
35 | /** |
||
36 | * Sets a value in the store. |
||
37 | * |
||
38 | * @param int|string $primaryKey The primary key. |
||
39 | * @param int|string $secondaryKey The secondary key. |
||
40 | * @param mixed $value The value. |
||
41 | */ |
||
42 | 146 | public function set($primaryKey, $secondaryKey, $value) |
|
50 | |||
51 | /** |
||
52 | * Removes a value from the store. |
||
53 | * |
||
54 | * This method ignores non-existing keys. |
||
55 | * |
||
56 | * @param int|string $primaryKey The primary key. |
||
57 | * @param int|string $secondaryKey The secondary key. |
||
58 | */ |
||
59 | 31 | public function remove($primaryKey, $secondaryKey) |
|
67 | |||
68 | /** |
||
69 | * Removes all values for the given primary key. |
||
70 | * |
||
71 | * This method ignores non-existing keys. |
||
72 | * |
||
73 | * @param int|string $primaryKey The primary key. |
||
74 | */ |
||
75 | 1 | public function removeAll($primaryKey) |
|
79 | |||
80 | /** |
||
81 | * Returns a value from the store. |
||
82 | * |
||
83 | * @param int|string $primaryKey The primary key. |
||
84 | * @param int|string $secondaryKey The secondary key. |
||
85 | * |
||
86 | * @return mixed The value. |
||
87 | * |
||
88 | * @throws OutOfBoundsException If no value is set for the given keys. |
||
89 | */ |
||
90 | 44 | public function get($primaryKey, $secondaryKey) |
|
102 | |||
103 | /** |
||
104 | * Returns whether the store contains the given key(s). |
||
105 | * |
||
106 | * The secondary key is optional. If you don't pass it, this method returns |
||
107 | * `true` if the store contains the given primary key with any secondary |
||
108 | * key. |
||
109 | * |
||
110 | * @param int|string $primaryKey The primary key. |
||
111 | * @param int|string|null $secondaryKey The secondary key. |
||
112 | * |
||
113 | * @return bool Returns `true` if the store contains the given key(s). |
||
114 | */ |
||
115 | 107 | public function contains($primaryKey, $secondaryKey = null) |
|
123 | |||
124 | /** |
||
125 | * Returns the first value set for the given primary key. |
||
126 | * |
||
127 | * @param int|string $primaryKey The primary key. |
||
128 | * |
||
129 | * @return mixed The value. |
||
130 | * |
||
131 | * @throws OutOfBoundsException If the primary key does not exist. |
||
132 | */ |
||
133 | 49 | View Code Duplication | public function getFirst($primaryKey) |
144 | |||
145 | /** |
||
146 | * Returns the last value set for the given primary key. |
||
147 | * |
||
148 | * @param int|string $primaryKey The primary key. |
||
149 | * |
||
150 | * @return mixed The value. |
||
151 | * |
||
152 | * @throws OutOfBoundsException If the primary key does not exist. |
||
153 | */ |
||
154 | 2 | View Code Duplication | public function getLast($primaryKey) |
165 | |||
166 | /** |
||
167 | * Returns the number of secondary keys set for the given primary key. |
||
168 | * |
||
169 | * @param int|string $primaryKey The primary key. |
||
170 | * |
||
171 | * @return int The number of secondary keys set for the primary key. |
||
172 | * |
||
173 | * @throws OutOfBoundsException If the primary key does not exist. |
||
174 | */ |
||
175 | 3 | View Code Duplication | public function getCount($primaryKey) |
186 | |||
187 | /** |
||
188 | * Returns all values set for the given primary key. |
||
189 | * |
||
190 | * @param int|string $primaryKey The primary key. |
||
191 | * |
||
192 | * @return array The values indexed by their secondary keys. |
||
193 | * |
||
194 | * @throws OutOfBoundsException If the primary key does not exist. |
||
195 | */ |
||
196 | 78 | View Code Duplication | public function listByPrimaryKey($primaryKey) |
207 | |||
208 | /** |
||
209 | * Returns all values set for the given secondary key. |
||
210 | * |
||
211 | * @param int|string $secondaryKey The secondary key. |
||
212 | * |
||
213 | * @return array The values indexed by their primary keys. |
||
214 | * |
||
215 | * @throws OutOfBoundsException If the secondary key does not exist. |
||
216 | */ |
||
217 | 13 | public function listBySecondaryKey($secondaryKey) |
|
236 | |||
237 | /** |
||
238 | * Returns the secondary keys for the given primary key. |
||
239 | * |
||
240 | * The primary key is optional. If this argument is not provided, all secondary keys will be returned. |
||
241 | * |
||
242 | * @param int|string|null $primaryKey The primary key. |
||
243 | * |
||
244 | * @return int[]|string[] The secondary keys. |
||
245 | * |
||
246 | * @throws OutOfBoundsException If the primary key does not exist. |
||
247 | */ |
||
248 | 31 | public function getSecondaryKeys($primaryKey = null) |
|
271 | |||
272 | /** |
||
273 | * Returns all primary keys. |
||
274 | * |
||
275 | * @return int[]|string[] The primary keys. |
||
276 | */ |
||
277 | 88 | public function getPrimaryKeys() |
|
281 | |||
282 | /** |
||
283 | * Returns the contents of the store as array. |
||
284 | * |
||
285 | * @return array[] A multi-dimensional array containing all values by |
||
286 | * their primary and secondary keys. |
||
287 | */ |
||
288 | 54 | public function toArray() |
|
292 | |||
293 | /** |
||
294 | * Returns whether the map is empty. |
||
295 | * |
||
296 | * @return bool Returns `true` if the map is empty and `false` otherwise. |
||
297 | */ |
||
298 | 4 | public function isEmpty() |
|
302 | |||
303 | /** |
||
304 | * Sorts the primary keys of the map. |
||
305 | * |
||
306 | * @param int[]|string[]|null $order The keys in the desired order. |
||
307 | */ |
||
308 | 2 | public function sortPrimaryKeys(array $order = null) |
|
320 | |||
321 | /** |
||
322 | * Sorts the secondary keys of a map entry. |
||
323 | * |
||
324 | * @param int|string $primaryKey The primary key. |
||
325 | * @param int[]|string[]|null $order The keys in the desired order. |
||
326 | */ |
||
327 | 3 | public function sortSecondaryKeys($primaryKey, array $order = null) |
|
346 | } |
||
347 |
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.